前面兩篇介紹了Spring Cloud Config服務端和客戶端的簡單配置,本篇介紹Spring Cloud Config與Eureka配合使用
前言
默認情況下,配置客戶端啟動時,都是通過配置屬性 spring.cloud.config.uri 綁定到配置服務器,並使用遠程屬性初始化 Spring Environment。這樣做的最終結果是所有想要使用Config Server的客戶端必須在bootstrap.yml中配置 spring.cloud.config.uri (默認是"http://localhost:8888")。
如果您正在使用DiscoveryClient實現,可將ConfigServer與Eureka等注冊中心聯合使用(目前Spring Cloud只支持與Eureka及Consul聯合使用,不支持與Zookeeper聯合使用)。但是如果配置了 spring.cloud.config.uri ,客戶端將無法利用注冊。
使用服務發現的壞處是啟動時額外的網絡往返,以定位服務注冊。好處是配置服務器可以更改其坐標,只要發現服務是一個固定點(如項目名稱不變)。
准備工作
1、啟動Eureka服務器(很簡單,這里就不演示了)。啟動成功后訪問http://localhost:8761,如下圖所示:
配置服務器代碼示例
在pom文件中增加依賴:
<dependency> <!-- 配置中心服務端 --> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <!-- eureka客戶端 --> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
啟動類開啟服務發現:
@SpringBootApplication @EnableConfigServer // 通過@EnableConfigServer注解激活配置服務 @EnableDiscoveryClient // 開啟服務發現 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
配置文件,application.yml
server: port: 18083 spring: application: name: config-server #應用程序名稱 cloud: config: server: git: uri: https://github.com/xuwenjin/config-repo-xwj #git上配置中心地址 eureka: client: serviceUrl: defaultZone: http://admin:admin@localhost:8761/eureka instance: prefer-ip-address: true #當猜測主機名時,服務器的IP地址應該在操作系統報告的主機名中使用
配置客戶端代碼示例
在pom中增加依賴:
<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.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <!-- eureka客戶端 --> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
啟動類增加服務發現:
@SpringBootApplication @EnableDiscoveryClient // 開啟服務發現 public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
增加一個Controller,用於測試請求:
@RestController @RefreshScope public class IndexController { @Value("${profile}") private String profile; @RequestMapping("/profile") public String getProfile() { return profile; } }
配置文件,bootstrap.yml
spring: application: name: config-client cloud: config: profile: test #對應spring.profiles.active label: master #分支名。當使用配置服務器是git時,默認是master username: user #配置服務器的用戶名密碼,此配置會覆蓋uri中的配置 password: password123 discovery: enabled: true #默認false,設為true表示使用注冊中心中的configserver配置,而不是自己配置configserver的uri service-id: CONFIG-SERVER #指定config server在服務發現中的serviceId,默認為:configserver eureka: client: serviceUrl: defaultZone: http://admin:admin@localhost:8761/eureka instance: prefer-ip-address: true #當猜測主機名時,服務器的IP地址應該在操作系統報告的主機名中使用
配置文件,application.yml
server: port: 18084
從示例代碼可以看到,想要將Config Server與注冊中心聯合使用,只需要在客戶端配置 spring.cloud.config.discovery.enabled:true 和 spring.cloud.config.discovery.serviceId 兩個配置項即可(serviceId是注冊到Eureka中的Application)。
測試工作
1、啟動配置服務器,會發現Eureka中增加了該示例
2、啟動配置客戶端,在日志中可以看到客戶端發現了服務器的地址:
3、訪問http://localhost:18084/profile,返回配置信息。至此配置完成~