当使用config当着配置中心的实现需要使用Bus进行实现动态刷新 但必须结合RabbitMQ
Bus支持两种消息代理 RabbitMQ 和KafkaBus 配合 config使用可以实现配置的动态刷新 省略了手动向http://localhosyt8201/actuator/refresh 发送POST请求是用来将分布式系统的节点与轻量级消息系统连接起来的框架 整合了java的事件处理机制和消息中间件功能springcloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器 可用于广播状态更改,事件推送也可以当做微服务间通信通道处什么是总线:在微服务架构当中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中的所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费 所以称为消息总线,在总线上的各个实例,都可以方便的广播一些需要让其他连接在该主题上的实例都知道的消息原理:configClient实例都监听MQ中同一个topic(默认是SpringcloudBus).当一个服务刷新数据的时候,它会把这个消息放入到Topic中 这样监听同一个Topic的服务就能得到通知,然后去更新自身的配置
windows下安装rabbitmq
1.https://www.erlang.org/downloads 下载erlang
下载mq
2.http://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/:rabbitmq-server-3.7.14.exe
3.进去安装mq目录的sbin目录下 命令行输入:rabbitmq-plugins enable rabbitmq_management
4可以在开始菜单中找到RabbitMQ Server 目录 下有可视化插件
5.启动 浏览器访问http://localhost:15672/ 可查看是否安装成功 默认账号密码为guest
15672是管理界面端口 5672是连接端口
作用:更改github上的文件 不在对每个使用端的/actuator/refresh地址依次进行post请求
全局广播通知
通过POST对获取配置的服务端/actuator/bus-refresh地址的一次请求 自动对使用端广播自动更改配置
基本实现
以使用rabbitmq为例
获取配置端
pom
<dependencies>
<!--bus依赖-->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
bootstrap.yml
server:
port: 3344
spring:
application:
name: springcloud-config
cloud:
config:
server:
git:
uri: https://github.com/SkyGlad/springcloud.git
#rabbitmq相关配置 注意在Spring下
rabbitmq:
host: localhost
port: 5672 #rabbitmq端口号 15672是浏览器管理界面的端口 5672是连接端口
username: guest #默认的账号 密码
password: guest
# 暴露bus刷新配置的端点
management:
endpoints: #暴露bus刷新配置的端点
web:
exposure:
include: "bus-refresh" #获取配置端暴露bus-refresh 用于刷新配置使用
#通过post请求:http://ip:获取配置端端口号/actuator/bus-refresh 进行对使用端的广播 让使用端与github上修改之后的配置同步
启动类
@SpringBootApplication
@EnableConfigServer
public class ApplicarionConfig3344 {
public static void main(String[] args) {
SpringApplication.run(ApplicarionConfig3344.class, args);
}
}
使用端
pom
<dependencies>
<!--bus依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--api-->
<dependency>
<groupId>org.hdxy</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
bootstrap.yml
spring:
cloud:
#从获取配置端获取配置 注意:在github上已经把需要的配置都设置上了 所有不用再在配置文件中设置如果需要单独的配置可以在配置文件中在设置
config:
uri: http://localhost:3344
label: master
name: server-client
profile: dev
#rabbitMQ配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*" # * 表示暴露所有端点
在命令窗口 通过 curl -X POST http://localhost:3344/actuator/bus-refresh 对获取配置端发送post请求 使用端会自动同步github的修改
定点广播通知
curl -X POST http://localhost:3344/actuator/bus-refresh/通知的微服务的名字:端口号
