温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud消息总线Bus配置中心实现的示例分析

发布时间:2021-05-12 10:58:30 来源:亿速云 阅读:179 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关SpringCloud消息总线Bus配置中心实现的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

介绍和使用场景

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启动项目是不生效的

SpringCloud消息总线Bus配置中心实现的示例分析

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

建议:

动态刷新配置,在开发和测试环境使用,尽量少在生产环境使用

感谢各位的阅读!关于“SpringCloud消息总线Bus配置中心实现的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI