SpringCloud消息总线Bus配置中心实现过程解析
作者:玉天恒 时间:2023-02-14 10:05:18
介绍和使用场景
1)什么是消息
一个事件,需要广播或者单独传递给某个接口
2)为什么使用这个
配置更新了,但是其他系统不知道是否更新
SpringCloud配置中心可参考:
https://www.jb51.net/article/182888.htm
1.安装RabbitMQ
下载地址:
http://erlang.org/download/
Erlang版本:Erlang OTP 21:10.0.1
下载地址:
https://www.rabbitmq.com/install-windows.html
RabbitMQ版本:Rabbitmq: 3.7.15
2.访问地址
http://127.0.0.1:15672/
用户名:guest
密码:guest
3.添加依赖
例如:
商品服务添加下面依赖,配置中心服务不用加
<!-- bus,rabbitmq -->
<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>
</dependency>
4.修改Controller
添加@RefreshScope注解
@RestController
@RequestMapping("/api/v1/product")
@RefreshScope
public class ProductController {
@Value("${server.port}")
private String port;
//这是自己加的配置
@Value("${env}")
private String env;
@RequestMapping("/list")
public Object getAllList() {
String msg = "data from port: " + port + ",env: " + env);
return ;
}
}
5.添加项目中的bootstrap..yml配置
spring:
#添加RabbitMQ配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#暴露全部的监控信息
management:
endpoints:
web:
exposure:
include: "*"
6.启动项目后,修改git中的yml配置env,再用Postman调用下面接口
步骤:
修改git的application.yml配置
1)修改端口号为:8771,启动项目
)修改端口号为:8772,启动项目
修改env为:test1234
注意:
使用SpringCloud配置中心后,在idea中修改配置-Dserver.port=8772启动项目是不生效的
7.访问接口,查看返回的env是否改变,evn值均变为test123456
http://127.0.0.1:8771/api/v1/product/find?id=1
http://127.0.0.1:8772/api/v1/product/find?id=1
建议:
动态刷新配置,在开发和测试环境使用,尽量少在生产环境使用
来源:https://www.cnblogs.com/tianhengblogs/p/12514553.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android编程ProgressBar自定义样式之动画模式实现方法
![](https://img.aspxhome.com/file/2023/1/117061_0s.png)
springboot整合mybatis-plus基于注解实现一对一(一对多)查询功能
Kotlin协程Channel特点及使用细节详解
![](https://img.aspxhome.com/file/2023/8/117108_0s.jpg)
C# 根据表格偶数、奇数加载不同颜色
![](https://img.aspxhome.com/file/2023/1/111071_0s.png)
Java 中ThreadLocal类详解
JAVA格式化时间日期的简单实例
Java实现定时任务的方法详解
Android中的Intent Filter匹配规则简介
SpringBoot创建WebService方法详解
![](https://img.aspxhome.com/file/2023/1/79821_0s.png)
C# Winform实现石头剪刀布游戏
C# params可变参数的使用注意详析
简单实现Android验证码
![](https://img.aspxhome.com/file/2023/8/116038_0s.jpg)
springMVC实现图形验证码(kaptcha)代码实例
java实现网站微信扫码支付
C++编程中用put输出单个字符和cin输入流的用法
深入理解Java责任链模式实现灵活的请求处理流程
![](https://img.aspxhome.com/file/2023/7/71167_0s.png)
详解java 中泛型中的类型擦除和桥方法
用C语言实现五子棋小游戏
![](https://img.aspxhome.com/file/2023/9/117779_0s.jpg)