1.设计思想:利用消息机制来进行来进行动态刷新
(1)利用消息总线触发一公客户端/bus/refresh,而刷新所有客户端的配置
(2)利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置
本案例推荐使用(2).具体的操作步骤如下:
2.创建配置中心
- 创建配置中心cloud-config-center-3344
- 添加坐标
<dependencies>
<!--添加消息总线amqp的支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--需要引入配置中心的坐标-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--引入eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
- 添加配置类
server:
port: 3344
spring:
application:
name: config-center
cloud:
config:
server:
git:
uri: 你github中配置文件的地址
search-paths:
- springcloud-config
skip-ssl-validation: true
label: master
rabbitmq:
host: 你主机的地址
username: guest
password: guest
port: 5672
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
instance:
prefer-ip-address: true
instance-id: config-center
management:
endpoints:
web:
exposure:
include: "bus-refresh" #暴露bus刷新端点的配置
- 编写启动类
/**
* 分布式的配置中心
*/
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args) ;
}
}
- 测试
3.编写配置客户端3355
- 创建模块cloud-config-client-3355
- 添加坐标
<!--这是分布式配置中心的客户端-->
<dependencies>
<!--添加消息总线amqp的支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--导入spring-boot的web模块的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入spring-boot的测试模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--引入eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--客户端的配置信息-->
<!--读取bootstrap配置文件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
- 编写bootstrap.yml配置
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
uri: http://config-3344.com:3344
label: master
profile: dev
name: config
rabbitmq:
host: 你主机的名字
username: guest
password: guest
port: 5672
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
instance:
prefer-ip-address: true
instance-id: config-client3355
management:
endpoints:
web:
exposure:
include: "*" # 暴露当前端口共别人调用
- 编写controller
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo ;
@RequestMapping("/configInfo")
public String getConfigInfo() {
return configInfo ;
}
}
- 编写启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args) ;
}
}
4.编写配置客户端3366
- 创建模块cloud-config-client-3366
- 添加坐标
<!--这是分布式配置中心的客户端-->
<dependencies>
<!--添加消息总线amqp的支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--导入spring-boot的web模块的支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入spring-boot的测试模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--引入eureka的客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--客户端的配置信息-->
<!--读取bootstrap配置文件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
- 创建bootstrap.yml配置文件
server:
port: 3366
spring:
application:
name: config-client
cloud:
config:
uri: http://config-3344.com:3344
label: master
profile: dev
name: config
rabbitmq:
host: 你主机的名字
username: guest
password: guest
port: 5672
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
instance:
prefer-ip-address: true
instance-id: config-client3366
management:
endpoints:
web:
exposure:
include: "*" # 暴露当前端口共别人调用
- 编写controller
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo ;
@RequestMapping("/configInfo")
public String getConfigInfo() {
return configInfo ;
}
}
- 编写启动类
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class,args) ;
}
}
- 测试
5.实现动态刷新(全局广播)
需要手动的向配置中心发送如下的请求:
curl -X POST "http://config-3344.com:3344/actuator/bus-refresh"
6.实现动态刷新(定点广播)
curl -X POST "http://config-3344.com:3344/actuator/bus-refresh/config-client:3355"
其中config-client当前要刷新的客户端的应用的名字,3355为端口号。
标签:spring,SpringCloud,springframework,starter,刷新,org,config,Bus,cloud From: https://blog.51cto.com/u_14613614/5984019