Spring Cloud入门-Bus消息总线(Hoxton版本)

| 9 | Spring Cloud入门-Bus消息总线(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103753372 |

| 10 | Spring Cloud入门-Sleuth服务链路跟踪(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103753896 |

| 11 | Spring Cloud入门-Consul服务注册发现与配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103756139 |

| 12 | Spring Cloud入门-Gateway服务网关(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103757927 |

| 13 | Spring Cloud入门-Admin服务监控中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103758697 |

| 14 | Spring Cloud入门-Oauth2授权的使用(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103761687 |

| 15 | Spring Cloud入门-Oauth2授权之JWT集成(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103763364 |

| 16 | Spring Cloud入门-Oauth2授权之基于JWT完成单点登录(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103766368 |

| 17 | Spring Cloud入门-Nacos实现注册和配置中心(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103769680 |

| 18 | Spring Cloud入门-Sentinel实现服务限流、熔断与降级(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103770879 |

| 19 | Spring Cloud入门-Seata处理分布式事务问题(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103786102 |

| 20 | Spring Cloud入门-汇总篇(Hoxton版本) | https://blog.csdn.net/ThinkWon/article/details/103786588 |

摘要


Spring Cloud Bus 使用轻量级的消息代理来连接微服务架构中的各个服务,可以将其用于广播状态更改(例如配置中心配置更改)或其他管理指令,本文将对其用法进行详细介绍。

Spring Cloud Bus 简介


我们通常会使用消息代理来构建一个主题,然后把微服务架构中的所有服务都连接到这个主题上去,当我们向该主题发送消息时,所有订阅该主题的服务都会收到消息并进行消费。使用 Spring Cloud Bus 可以方便地构建起这套机制,所以 Spring Cloud Bus 又被称为消息总线。Spring Cloud Bus 配合 Spring Cloud Config 使用可以实现配置的动态刷新。目前 Spring Cloud Bus 支持两种消息代理:RabbitMQ 和 Kafka,下面以 RabbitMQ 为例来演示下使用Spring Cloud Bus 动态刷新配置的功能。

RabbitMQ的安装


安装Erlang,下载地址:http://erlang.org/download…

在这里插入图片描述

安装RabbitMQ,下载地址:https://dl.bintray.com/rabbitmq…

在这里插入图片描述

安装完成后,进入RabbitMQ安装目录下的sbin目录:

在这里插入图片描述

在RabbitMQ安装目录下的sbin目录的地址栏输入cmd并回车启动命令行,然后输入以下命令启动管理功能:

在这里插入图片描述

rabbitmq-plugins enable rabbitmq_management

在这里插入图片描述

访问地址查看是否安装成功:http://localhost:15672/

在这里插入图片描述

输入账号密码并登录:guest guest

动态刷新配置


使用 Spring Cloud Bus 动态刷新配置需要配合 Spring Cloud Config 一起使用,我们使用上一节中的config-server、config-client模块来演示下该功能。

给config-server添加消息总线支持

在pom.xml中添加相关依赖:

org.springframework.cloud

spring-cloud-starter-bus-amqp

org.springframework.boot

spring-boot-starter-actuator

添加配置文件application-amqp.yml,主要是添加了RabbitMQ的配置及暴露了刷新配置的Actuator端点;

server:

port: 8904

spring:

application:

name: config-server

cloud:

config:

server:

git:

配置存储配置信息的Git仓库

uri: https://gitee.com/JourWon/springcloud-config.git

username: JourWon

password: 123456

开启启动时直接从git获取配置

clone-on-start: true

获取子目录下的配置

search-paths: ‘{application}’

rabbitmq相干配置

rabbitmq:

host: localhost

port: 5672

username: guest

password: guest

eureka:

client:

register-with-eureka: true

fetch-registry: true

service-url:

defaultZone: http://localhost:8001/eureka/

暴露bus刷新配置的端点

management:

endpoints:

web:

exposure:

include: ‘bus-refresh’

给config-client添加消息总线支持

在pom.xml中添加相关依赖:

org.springframework.cloud

spring-cloud-starter-bus-amqp

添加配置文件bootstrap-amqp1.yml及bootstrap-amqp2.yml用于启动两个不同的config-client,两个配置文件只有端口号不同;

server:

port: 9004

spring:

application:

name: config-client

cloud:

config客户端配置

config:

分支名称

label: master

启用配置后缀名称

profile: dev

配置文件名称

name: config

discovery:

enabled: true

service-id: config-server

rabbitmq配置

rabbitmq:

host: localhost

port: 5672

username: guest

password: guest

eureka:

client:

register-with-eureka: true

fetch-registry: true

service-url:

defaultZone: http://localhost:8001/eureka/

management:

endpoints:

web:

exposure:

include: ‘refresh’

动态刷新配置演示

我们先启动相关服务,启动eureka-server,以application-amqp.yml为配置启动config-server,以bootstrap-amqp1.yml为配置启动config-client,以bootstrap-amqp2.yml为配置再启动一个config-client,启动后注册中心显示如下:

在这里插入图片描述

启动所有服务后,我们登录RabbitMQ的控制台可以发现 Spring Cloud Bus 创建了一个叫springCloudBus的交换机及三个以 springCloudBus.anonymous开头的队列:

在这里插入图片描述

在这里插入图片描述

文末

我将这三次阿里面试的题目全部分专题整理出来,并附带上详细的答案解析,生成了一份PDF文档

  • 第一个要分享给大家的就是算法和数据结构

网易严选Java开发三面面经:HashMap+JVM+索引+消息队列

  • 第二个就是数据库的高频知识点与性能优化

网易严选Java开发三面面经:HashMap+JVM+索引+消息队列

  • 第三个则是并发编程(72个知识点学习)

网易严选Java开发三面面经:HashMap+JVM+索引+消息队列

  • 最后一个是各大JAVA架构专题的面试点+解析+我的一些学习的书籍资料

网易严选Java开发三面面经:HashMap+JVM+索引+消息队列

还有更多的Redis、MySQL、JVM、Kafka、微服务、Spring全家桶等学习笔记这里就不一一列举出来

详细的答案解析,生成了一份PDF文档

  • 第一个要分享给大家的就是算法和数据结构

[外链图片转存中…(img-Ak5tFq6M-1723296551216)]

  • 第二个就是数据库的高频知识点与性能优化

[外链图片转存中…(img-s7zDGdqn-1723296551217)]

  • 第三个则是并发编程(72个知识点学习)

[外链图片转存中…(img-AeOdViHi-1723296551217)]

  • 最后一个是各大JAVA架构专题的面试点+解析+我的一些学习的书籍资料

[外链图片转存中…(img-foSzYxVl-1723296551218)]

还有更多的Redis、MySQL、JVM、Kafka、微服务、Spring全家桶等学习笔记这里就不一一列举出来

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值