目录
🧂1.Bus是什么??????
🌭2.什么是总线??????
🥓3.rabbitmq??????
🥞4.新建模块3366??????
🍳5.设计思想???????
🍿6.添加消息总线的支持??????
🥚7.定点通知??????
- SpringCloud Bus是将分布式系统的节点与轻量级消息系统链接起来的框架
- 它整合了Java的事件处理机制和消息中间件的功能。
- 目前支持RabbitMQ和Kafka。
- Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新。
在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。
虚拟机上安装好rabbitmq
安装详细请看小张的—>从入门到精通RabbitMQ
<dependencies>
<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.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>
<dependency>
<groupId>org.example</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka的Client端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--configClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
server:
port: 3366
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
name: config
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class);
}
}
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${server.port}")
private String serverPort;
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return "serverPort:" + serverPort + "\t\t" +",configInfo:"+configInfo;
}
}
利用消息总线触发一个服务端ConfigServer的/bus/refresh端点,而刷新所有客户端的配置
添加消息总线依赖
<!--消息总线RabbitMq-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
添加rabbitmq配置,并暴露刷新端点
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
#gitee上面的仓库地址
uri: git@gitee.com:hqdmdxz/springcould-config.git
#搜索目录
search-paths:
- sprongcloud-config
#gitee的账号
username: 18337062987
#gitee的密码
password: love4.29
#读取分支
lable: master
#rabbitmq配置
rabbitmq:
host: 192.168.20.129
port: 5672
username: root
password: 123456
#注册到eureka
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
#暴露bus刷新配置的端点
management:
endpoints:
web:
exposure:
include: 'bus-refresh'
?添加消息总线依赖
<!--消息总线RabbitMq-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
server:
port: 3355
spring:
application:
name: config-client
cloud:
#客户端配置
config:
#分支名称
label: master
#配置文件名称
name: config
#读取后缀名称
profile: dev
#配置中心地址
uri: http://localhost:3344
rabbitmq:
host: 192.168.20.129
port: 5672
username: root
password: 123456
#服务注册到eureka
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
?添加消息总线依赖
<!--消息总线RabbitMq-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
server:
port: 3366
spring:
application:
name: config-client
cloud:
#客户端配置
config:
#分支名称
label: master
#配置文件名称
name: config
#读取后缀名称
profile: dev
#配置中心地址
uri: http://localhost:3344
rabbitmq:
host: 192.168.20.129
port: 5672
username: root
password: 123456
#服务注册到eureka
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
1.启动eureka集群,config配置中心3344,服务3355,服务3366。
2.手动刷新服务3344,一刷新处处生效
2.在gitee上修改版本号,浏览器查看3344,3355,3366.?
公式:
http://localhost:配置中心的端口号/actuator/bus-refresh/{destination)
?至此消息总线基本拿捏~
?