0
点赞
收藏
分享

微信扫一扫

SpringCloud+Bus动态刷新

1.设计思想:利用消息机制来进行来进行动态刷新

(1)利用消息总线触发一公客户端/bus/refresh,而刷新所有客户端的配置

SpringCloud+Bus动态刷新_spring

SpringCloud+Bus动态刷新_spring_02

(2)利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置

本案例推荐使用(2).具体的操作步骤如下:

2.创建配置中心

  1. 创建配置中心cloud-config-center-3344
  2. 添加坐标

<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>

  1. 添加配置类

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刷新端点的配置

  1. 编写启动类

/**
* 分布式的配置中心
*/
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args) ;
}
}

  1. 测试

3.编写配置客户端3355

  1. 创建模块cloud-config-client-3355
  2. 添加坐标

<!--这是分布式配置中心的客户端-->
<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>

  1. 编写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: "*" # 暴露当前端口共别人调用

  1. 编写controller

@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo ;

@RequestMapping("/configInfo")
public String getConfigInfo() {
return configInfo ;
}
}

  1. 编写启动类

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args) ;
}
}

4.编写配置客户端3366

  1. 创建模块cloud-config-client-3366
  2. 添加坐标

<!--这是分布式配置中心的客户端-->
<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>

  1. 创建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: "*" # 暴露当前端口共别人调用

  1. 编写controller

@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo ;

@RequestMapping("/configInfo")
public String getConfigInfo() {
return configInfo ;
}
}

  1. 编写启动类

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class,args) ;
}
}

  1. 测试

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为端口号。

举报

相关推荐

0 条评论