九.Bus消息总线
1.知识点
Bus支持两种消息代理:RabbitMQ和Kafka
2.安装RabbitMQ
①下载和安装erlang
安装时常规下一步就行,选D盘
②下载和安装RabbitMQ
选D盘其他下一步。
安装好了之后进入RabbitMQ下的sbin目录,打开cmd输入
rabbitmq-plugins enable rabbitmq_management
这样就行了
点击start启动
访问15672,默认用户名密码是guest
登录进来
3.SpringCloud Bus动态刷新全局广播
①创建module
②编写pom文件
<dependencies>
<!-- config -->
<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通用包 可以使用公用的entities-->
<dependency>
<groupId>com.hry.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</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>
③修改yml文件
bootstrap.yml
server:
port: 3366
spring:
application:
name: config-client
cloud:
config:
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称 三者合一master分支上config-dev.yml的配置文件被读取
uri: http://localhost:3344
#注册到Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
④创建主启动类
package com.hry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3366 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3366.class, args);
}
}
⑤业务代码
controller
package com.hry.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class ConfigClientController {
/**
* 读取github上的yml文件的config.info
*/
@Value("${config.info}")
private String configInfo;
@Value("${server.port}")
private String serverPort;
@GetMapping("/configInfo")
public String getConfigInfo(){
return "serverPort: "+serverPort+"\t\t"+configInfo;
}
}
⑥修改3344的pom文件
添加坐标
<!--消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
⑦修改3344的yml文件
在spring中添加配置
spring:
#15672是web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
添加配置
#暴露bus刷新配置的端点
management:
endpoints:
web:
exposure:
include: "bus-refresh"
⑧修改3355和3366的pom和yml
(1)pom
3355和3366都添加如下坐标
<!--消息总线RabbitMQ支持-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
(2)yml
3355和3366都在spring下添加配置
spring:
#15672是web管理界面的端口,5672是MQ访问的端口
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
⑨测试
查看当前GitHub上版本为3
由于都重启过 所以目前44 55 66都是3
现在GitHub改成4并提交
发送POST
curl -X POST "http://localhost:3344/actuator/bus-refresh"
实现了一次修改,广播通知,处处生效,比之前Config好的一点是不用55 66分别发送POST
4.SpringCloud Bus动态刷新定点通知
按需通知,并不是全部都通知。
比如只通知3355不通知3366
①修改版本号
修改成5并提交
②使用curl发送POST
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"
③测试