spring cloud bus AMQP学习笔记

spring cloud bus AMQP

三种jar包

  • spring-cloud-bus 源包
  • spring-cloud-starter-bus-amqp 整合了RabbitMQ
  • spring-cloud-starter-bus-kafka 整合了kafka

pom.xml

我这里使用的是RabbitMQ做消息队列,所以我使用的是spring-cloud-starter-bus-amqp

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bus-amqp -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

application.yml配置

spring:
  application:
    name: cloud-config9001
  rabbitmq:          #要用rabbitmq做消息中间件
    host: localhost
    port: 5672
    username: guest
    password: guest


#暴露监控端口
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refres'

配合spring cloud config动态刷新配置

spring cloud config+spring cloud Bus是制作微服务配置中心的经典组合。Spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。实本质是利用了MQ的广播机制在分布式的系统中传播消息。

Stream通过对消息中间件进行抽象封装,提供一个统一的接口供我们发送和监听消息,而Bus则是在Stream基础之上再次进行抽象封装,使得我们可以在不用理解消息发送、监听等概念的基础上使用消息来完成业务逻辑的处理。

添加spring cloud config
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
配置中心的主启动类

添加@EnableConfigServer注解,开启配置服务中心功能。

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
  public static void main(String[] args) {
    SpringApplication.run(ConfigServer.class, args);
  }
}
跟新配置中心的application.yml配置
spring:
  application:
    name: cloud-config9001
  rabbitmq:          #要用rabbitmq做消息中间件
    host: localhost
    port: 5672
    username: guest
    password: guest
  cloud:
    config:
      server:
        git:
          uri: xxx.git         #仓库地址
          search-paths: springcloud-config #扫描路径
      label: master

#暴露监控端口
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refres'
其他微服务配置信息

bootstrap.yml配置

spring:
  application:
    name: cloud-config-client
  cloud:
    config:
      label: master     #分支
      name: config      #配置名
      profile: dev      #信息后缀(对应上面的profile)
      uri: http://localhost:9001   #配置中心

添加监控器依赖和bus依赖

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bus-amqp -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

暴露监控端点

management:
  endpoints:
    web:
      exposure:
        include: *
通过网络接口发起post请求刷新

对配置中心发生刷新请求http://localhost:8888/actuator/refresh 发起post请求,以刷新微服务的配置信息。发生post请求之后,就会触发bus的广播效应,Bus本质还是利用了消息中间件做发布订阅,从而实现广播效果。

结果就像下面的图一样(网络图片):

请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Cloud Bus是一个用于在分布式系统中传播状态变化的消息总线。它基于Spring Cloud Stream和Spring Cloud Config构建,可以将消息广播到整个系统中的所有服务实例。通过使用Spring Cloud Bus,可以实现配置的动态刷新、事件的传播和集群中的状态同步。 下面是使用Spring Cloud Bus自定义消息总线的步骤: 1. 添加依赖:在项目的pom.xml文件中添加Spring Cloud Bus的依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> ``` 2. 配置消息代理:在应用的配置文件中配置消息代理,例如使用RabbitMQ作为消息代理: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest ``` 3. 发送自定义消息:在需要发送自定义消息的地方,使用Spring Cloud Bus提供的API发送消息。例如,可以使用`/actuator/bus-refresh`端点发送刷新配置的消息: ```shell curl -X POST http://localhost:8080/actuator/bus-refresh ``` 4. 接收自定义消息:在需要接收自定义消息的地方,使用Spring Cloud Bus提供的注解和监听器来接收消息。例如,可以使用`@RefreshScope`注解来刷新配置: ```java @RefreshScope @RestController public class ConfigController { // ... } ``` 通过以上步骤,您可以使用Spring Cloud Bus自定义消息总线来实现配置的动态刷新、事件的传播和集群中的状态同步。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值