Springcloud-Bus消息总线

官网

Bus消息总线概述

Spring Cloud Bus配合Spring Cloud Config使用可以实现配置的动态刷新

什么是总线

在分布式系统中通常使用消息代理来实现共用的消息主题,并让所有的微服务都连接上,所产生的的消息会被所有的消费者消费,所以叫消息总线。

基本原理

ConfigClient实例都会监听MQ中同一个topic(默认是springcloudbus)。当一个服务刷新数据时会把这个信息放入topic中,这样监听他的服务就会得到通知,然后更新配置。

bus的两种代理模式

Bus支持两种消息代理:RabbitMQ和Kafka

RabbitMQ安装配置

博客

消息总线测试使用

configServer端搭建

pom依赖

<dependencies>
        <!--bus消息总线-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml配置

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri:  git@github.com:paulfuke/springcloud-config.git          # github的地址
          search-paths:
            - springcloud-config # github仓库名称
      label: master  #分支
      # 访问方式见文档
#rebbitmq配置
  rabbitmq:
    host: 39.101.182.222
    port: 5672
    username: admin
    password: admin
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka,http://localhost:7002/eureka

#配置消息总线请求刷新的url
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

启动类配置

@SpringBootApplication
@EnableConfigServer
public class ConfigMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigMain3344.class,args);
    }
}

configClientA搭建

pom依赖

<dependencies>
        <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

yml配置

server:
  port: 3355
spring:
  application:
    name: config-client-3355
  cloud:
    config:
      label: master #分支名
      name: config #application名称
      profile: dev #profile名称  综合就是根据url进行访问github上的master/config-dev.yml
      uri: http://localhost:3344
  #rabbitmq配置
  rabbitmq:
    host: 39.101.182.222
    port: 5672
    username: admin
    password: admin
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

启动类配置

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class, args);
    }
}

核心业务类

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigClientController {
    @Value("${config.info}")
    public String configInfo;

    @GetMapping("/info")
    public String configInfo(){
        return configInfo;
    }
}

configClientB搭建

略(和A类似)更改端口为3366即可

工程截图
在这里插入图片描述

访问测试

全局通知,访问测试
服务端

http://localhost:3344/master/config-dev.yml

客户端1

http://localhost:3355/config/info

客户端2

http://localhost:3366/config/info

当配置信息发生变化时,全局更新

http://localhost:3344/actuator/bus-refresh //注意必须post请求

定点通知
当配置更新的时候定点通知某个微服务单独刷新配置

http://localhost:3344/actuator/bus-refresh/application-name:端口号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值