SpringCloud-消息总线(Bus)

Bus

概述

在分布式微服务系统中,通常会使用 消息中间件 构建一个共用的消息topic,并 将系统中所有的微服务实例连接上去;

由于 该topic中产生的消息会被所有的微服务实例监听/消费,所以称为消息总线;

在总线上的各个微服务实例,都可以方便地广播一些 订阅该topic上的微服务实例的消息;

 

what

 

 

SpringCloud Bus 将 分布式系统的节点 与 消息中间件 衔接起来的框架;

SpringCloud Bus 整合了Java的Event、消息中间件;

SpringCloud Bus 目前只支持RabbitMQ、Kafka;

功能

  管理/传播 分布式系统间的消息;

  广播状态更改、事件推送等;

  微服务间的通信通道;

基本原理 

ConfigClient实例 监听MQ中同一个topic,当一个服务刷新数据时,该服务会将消息放到topic中,这样监听该topic的微服务就可以得到通知,更新自身配置;

 

How

2种方案:

  利用消息总线触发一个ConfigClient,再刷新所有的客户端(不可用,服务的单一性破坏);

  利用消息总线触发一个ConfigServer,再刷新所有的客户端;

全局广播

ConfigServer 

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
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
 
 
server:
  port: 3344
 
eureka:
  client:
    register-with-eureka: true #是否向注册中心注册自己
    fetchRegistry: true #是否从注册中心抓取已有的注册信息 默认true,集群必须设置为true
    service-url:
      defaultZone: http://localhost:7001/eureka/   #单机版
 
spring:
  application:
    name: config
  profiles:
    active: native
  cloud:
    config:
      server:
#        git:
#          uri:    #github地址
#          search-paths: #搜索github的路径
#            - sprin
#      label: master #读取github分支
 
        native:   #本地获取
          search-locations: classpath:/config
 
#消息总线bus的rabbitmq配置
rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest
 
#消息总线bus的rabbitmq配置、暴露bus刷新配置端点
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'
 
 
 
config:
  info: dev5

  

ConfigClient

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
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
 
 
applicaton.yml -> bootstrap.yml
 
server:
  port: 8001
 
eureka:
  client:
    register-with-eureka: true #是否向注册中心注册自己
    fetchRegistry: true #是否从注册中心抓取已有的注册信息 默认true,集群必须设置为true
    service-url:
      defaultZone: http://localhost:7001/eureka/   #单机版
  instance:
    instance-id: payment8001
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 1  # EurekaClient向EurekaServer发送心跳的时间间隔(默认30s)
    lease-expiration-duration-in-seconds: 2 # EurekaServer在收到EurekaClient最后一次心跳后,等待的时间上限(默认90s)
 
debug:
  false
 
mybatis:
  mapperLocations: classpath:mapper/*.xml
 
spring:
  application:
    name: eureka-payment-service
 
  cloud:
    config:
#      label:  #分支
      name: application   #配置文件名称
      profile: dev    #环境
      uri: http://localhost:3344  #配置中心地址
 
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: an314159
 
  #消息总线bus的rabbitmq配置
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
 
#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
 
 
 
@RefreshScope      // 必须加@RefreshScope,否则不会更新
@RestController
public class PaymentController {
@Value("${config.info}")
    private String config;
 
 
@GetMapping(value = "/getFromConfigServer")
    public String getFromConfigServer(){
        return config;
    }
}

 

手动刷新ConfigServer  curl -X POST "http://localhost:3344/actuator/bus-refresh"

 

定点通知 

请求将发送到ConfigServer,并通过destination参数 指定需要更新配置的服务;

How 

http://localhost:ConfigServer端口号/actuator/bus-refresh/{destination}

eg:  curl -X POST "http://localhost:3344/actuator/bus-refresh/eureka-payment-service:8002"

 

posted on   anpeiyong  阅读(491)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~

导航

< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示