spring boot+spring cloud +rabbitmq 实现动态配置 配置动态更新
Spring Cloud Bus通过轻量消息代理连接各个分布的节点。它可用在广播状态的变化或者其他的消息指令。Spring Cloud Bus的一个核心思想是通过分布式的启动器对Spring Boot 应用进行扩展,也可以用来建立一个多个应用之间的通信频道。
Spring Cloud Bus整合java的事件处理机制和消息中间件消息的发送和接收,主要由发送端、接收端和事件组成。针对不同的业务需求,可以设置不同的事件,发送端发送事件,接收端接收相应的事件,并进行相应的处理。
本文章介绍使用Spring Cloud Bus与Spring Cloud Config的整合,并以RabbitMQ作为消息代理,实现应用配置的动态更新。
1.环境,如不是2.0以上访问方式可能不一样;
spring boot 2.0
2.动态配置原理
:
1、将最新的配置文件push到git服务器
2、利用git仓库的Webhook机制监听push事件,事件捕获后,执行配置好的Payload URL地址(此处为配置中心的/actuator/bus-refresh端点)。通过git仓库的 Settings > Webhooks > Add webhook 可以创建Webhook。
3、配置中心从git获取最新的配置信息,并发送消息到RabbitMQ
4、微服务节点订阅和接收RabbitMQ消息,接收到消息后,从配置中心获取最新的配置信息
以上引用 https://www.iteye.com/blog/chenjumin-2436941
3 .代码的实现
搭建注册中心,这里不再详述,这里只介绍配置中心;
以下依赖已添加至父工程的pom.xml中
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<!-- 引入 Spring Cloud 的依赖,管理 Spring Cloud 各组件 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
1.在父工程目录下新建一个config文件夹。在文件夹下新建一个文件 configclient.yml.并提交到git仓库;
此处的端口号为客户端访问的端口号
server:
port: 8070
spring:
application:
name: configclient
management:
security:
enabled: false
info : 2**********
首先新建一个Module,命名configserver,然后在pom.xml 中导入如下依赖:
注意服务端和客户端都有导入actuator 、 bus-amqp 依赖;
<dependency>
//spring cloud config 依赖 配置中心的服务端
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
- 在resource 目录下新建application.yml 文件,
server:
port: 8888 #端口号
spring:
application:
name: configserver #注册中心服务名称
cloud:
bus:
trace:
enable: true #配置之后,当我们动态加载获取配置文件之后访问/trace就可以跟踪总线事件,这个必须要
config:
server:
git:
uri: https://github.com/wendyqiaoguanhui/myspringclouddemo.git #git仓库地址
searchPaths: config #配置文件所在目录
username:
password:
label: master
rabbitmq: #rabbitmq 必须配置
host: localhost
port: 5672
username: guest
password: guest
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #服务发布地址
management:
endpoints:
web:
exposure:
include: bus-refresh #spring boot 2.0 这里配置 "*" 和bus-refresh 都可以
- 常见启动类:
@SpringBootApplication
@EnableConfigServer //声明是配置服务中心
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4.在父工程目录下创建model,命名configclient,在pom.xml中导入如下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
</dependencies>
5.在resource目录下创建bootstrap.yml 文件
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
cloud:
config:
name: configclient
label: master
discovery:
enabled: true #是否开启 Config 服务发现支持。
serviceId: configserver #配置中心的名称。
eureka:
client:
seriveUrl:
defaultZone: http://localhost:8761/eureka/
managent:
endpoints:
web:
exposure:
include: bus-refresh #客户端和服务端必须写
6.创建 HelloHandler类;注意必须添加@RefreshScope注解
@RequestMapping("/config")
@RestController
@RefreshScope
public class HelloHandler {
@Value("${server.port}")
private String port;
@Value("${info}")
private String info;
@RequestMapping("/index")
public String index(){
return "当前信息************:"+this.info;
}
}
7.创建启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class,args);
}
}
8.分别启动注册中心,启动configserver 、 configclient; 然后启动postman在地址栏输入:http://localhost:8070/config/index; 输出以下信息
9.然后修改 confingclient.yml 中的info 为2%%%%%%%%%;push到git 仓库,
修改后configclient.yml的内容:
server:
port: 8070
spring:
application:
name: configclient
management:
security:
enabled: false
info : 2%%%%%%%%%
10.在postman发生post请求,输入:http://localhost:8888/actuator/bus-refresh;这里是手动更新,实现自动还需在github中配置
注意:一定是post请求,8888端口号为服务端的端口号;:
11.最后输入,访问地址,发现已动态更新,输出2%%%%%%%%%: