spring cloud cofing client/server 接入 eureka,client动态刷新配置'actuator/refresh'

首先改造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控制面板观察到已经注册。

spring cloud cofing client/server 接入 eureka,client动态刷新配置'actuator/refresh'
 

下面对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内容修改如下:

spring cloud cofing client/server 接入 eureka,client动态刷新配置'actuator/refresh'获取配置,还是显示"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那里重新获取配置:

spring cloud cofing client/server 接入 eureka,client动态刷新配置'actuator/refresh'

以下是config-server的日志,发现也的确从github重新拉取配置:

spring cloud cofing client/server 接入 eureka,client动态刷新配置'actuator/refresh'

这时候,再一次获取配置内容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$