Spring Cloud Config与Eureka配合使用

一 介绍
在微服务中指定Config Server地址,这种方式无法利用服务发现组件的优势。
本篇将讨论Config Server注册到Eureka Server上时,如何使用Spring Cloud config。
二 新建项目microservice-config-server-eureka
1 启动类代码
package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}
2 application.yml
server:
  port: 8080
spring:
  application:
    name: microservice-config-server-eureka
  cloud:
    config:
      server:
        git:
          uri:https://git.oschina.net/itmuch/spring-cloud-config-repo# 配置Git仓库的地址
          username:                                                         # Git仓库的账号
          password:                                                         # Git仓库的密码
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8761/eureka/
3 pom依赖
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
  </dependencies>
三 新建项目microservice-config-client-eureka
1 启动类
package com.itmuch.cloud.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClientApplication.class, args);
  }
}
2 application.yml
server:
  port: 8081
3 bootstrap.yml
spring:
  application:
    name: microservice-foo    # 对应config server所获取的配置文件的{application}
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true                                  # 表示使用服务发现组件中的Config Server,而不自己指定Config Server的uri,默认false
        service-id: microservice-config-server-eureka  # 指定Config Server在服务发现中的serviceId,默认是configserver
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8761/eureka/
4 pom
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
  </dependencies>
四 测试
1 启动Eureka
2 启动项目microservice-config-server-eureka
3 启动项目microservice-config-client-eureka

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/80888335