SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
来源:互联网 发布:模拟退火算法的应用 编辑:程序博客网 时间:2024/04/27 04:41
Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。本文要讲述的是用Spring Cloud Bus实现通知微服务架构的配置文件的更改。
一、准备工作
本文还是基于上一篇文章来实现。按照官方文档,我们只需要在配置文件中配置 spring-cloud-starter-bus-amqp ;这就是说我们需要装rabbitMq,点击rabbitmq下载。至于怎么使用 rabbitmq,搜索引擎下。
二、改造config-client
在pom文件加上起步依赖spring-cloud-starter-bus-amqp,完整的配置文件如下:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
在配置文件application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码,代码如下:
- 1
- 2
- 3
- 4
- 5
- 6
如果rabbitmq有用户名密码,输入即可。
依次启动eureka-server、confg-cserver,启动两个config-client,端口为:8881、8882。
访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:
foo version 3
这时我们去代码仓库将foo的值改为“foo version 4”,即改变配置文件foo的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。此时,我们只需要发送post请求:http://localhost:8881/bus/refresh,你会发现config-client会重现肚脐配置文件
重新读取配置文件:
这时我们再访问http://localhost:8881/hi 或者http://localhost:8882/hi 浏览器显示:
foo version 4
另外,/bus/refresh接口可以指定服务,即使用”destination”参数,比如 “/bus/refresh?destination=customers:**” 即刷新服务名为customers的所有服务,不管ip。
三、分析
此时的架构图:
当git文件更改的时候,通过pc端用post 向端口为8882的config-client发送请求/bus/refresh/;此时8882端口会发送一个消息,由消息总线向其他服务传递,从而使整个微服务集群都达到更新配置文件。
四、其他扩展(可忽视)
可以用作自定义的Message Broker,只需要spring-cloud-starter-bus-amqp, 然后再配置文件写上配置即可,同上。
Tracing Bus Events:
需要设置:spring.cloud.bus.trace.enabled=true,如果那样做的话,那么Spring Boot TraceRepository(如果存在)将显示每个服务实例发送的所有事件和所有的ack,比如:(来自官网)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter8
五、参考资料
spring_cloud_bus
http://blog.csdn.net/forezp/article/details/70148235
本文出自方志朋的博客- SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- 第八篇: 消息总线(Spring Cloud Bus)
- SpringCloud教程八:消息总线(SpringCloud Bus)
- Spring cloud-Bus (消息总线)
- Spring Cloud Bus 消息总线集成Kafka
- Spring Cloud (3) | spring cloud bus 消息总线kafka应用
- SpringCloud第八篇-Spring Cloud Config集群
- spring cloud 中消息总线(bus)使用
- Spring Cloud Bus 教程
- 初识Nginx
- 《软件工程思想》学习笔记
- Unity官方手册翻译之旅---Documentation editorial review
- python
- 【名师课堂】Java面向对象开发
- SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)
- spark<实现SQL中的GroupBY后取组内TopN>
- Java
- Leetcode:27.Remove Element
- 获取随机字符串
- SpringMVC数据格式转换
- 新国金挖矿宝追溯比特币价格的由来与发展
- C#封装、继承和多态
- MySQL性能优化的最佳20+条经验