首先改造config-server, pom裏引入spring-cloud-starter-eureka:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
在application.yml裏配置註冊中心地址:
eureka:
client:
service-url:
defaultZone: http://peer1:12451/eureka,http://peer1:12452/eureka,
接着,在應用層面啓動EnableDiscoveryClient
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
啓動程序config-server,可以在eureka控制面板觀察到config-server已經註冊。
改造config-client的步驟和上面一樣,這裏不在重複,改造後同樣可以在eureka控制面板觀察到已經註冊。
下面對config-client進行改造,實現動態刷新配置,首先引入spring-boot-starter-actuator:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
接着,在application.properteis配置:management.endpoints.web.exposure.include
management.endpoints.web.exposure.include=refresh,health,info
這時候把倉庫分支config-label-test下面的spring_cloud_in_action/config-repo/didispace-prod.properties內容修改如下:
獲取配置,還是顯示"git-prod-2.0":
RdeMacBook-Pro:config-server r$ curl http://127.0.0.1:7002/fromEnv/
git-prod-2.0
RdeMacBook-Pro:config-server r$
動態刷新配置:curl http://127.0.0.1:7002/actuator/refresh -d '' -H 'content-type:application/json',發現提示配置內容from被更改了:
RdeMacBook-Pro:config-server r$ curl http://127.0.0.1:7002/actuator/refresh -d '' -H 'content-type:application/json'
["config.client.version","from"]
RdeMacBook-Pro:config-server r$
從日誌中可以發現,config-client從config-server那裏重新獲取配置:
以下是config-server的日誌,發現也的確從github重新拉取配置:
這時候,再一次獲取配置內容curl http://127.0.0.1:7002/fromEnv/ ,發現返回的值從原來的"git-prod-2.0" 變成 "git-prod-3.0":
RdeMacBook-Pro:config-server r$ curl http://127.0.0.1:7002/fromEnv/
git-prod-3.0
RdeMacBook-Pro:config-server r$