Spring Cloud Bus学习笔记

Spring Cloud Bus (消息总线)将分布式系统的节点与轻量级消息代理连接起来。可用于广播状态更改(例如配置热更新)或其他管理指令。消息中间件目前只支持RabbitMQ和Kafka。可与Spring Cloud Stream结合使用。

基于Config与RabbitMQ实现。

一、引入依赖

在配置中心以及其他所有相关服务加入:

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

二、修改配置文件

1、配置中心

server:
  port: 3344

spring:
  application:
    name: config-center-service
  cloud:
    config:
      label: main  # 分支
      server:
        git:
          uri: https://github.com/xxx/spring-cloud-config.git  # github仓库路径
          username: xxx  # github用户名
          password: xxx  # github密码
          search-paths:
            - spring-cloud-config  # 项目名
  rabbitmq:
    host: 192.168.10.128
    port: 5672
    username: admin
    password: admin

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka

# 暴露bus刷新的监控端点
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"

2、其他服务

spring:
  cloud:
    config:
      # 找到分支为main的配置文件config-dev.yml
      label: main  # 分支
      name: config  # 配置文件名称
      profile: dev  # 环境
      uri: http://localhost:3344  # 配置中心地址
  rabbitmq:
    host: 192.168.10.128
    port: 5672
    username: admin
    password: admin

# 暴露监控端口
management:
  endpoints:
    web:
      exposure:
        include: "*"

三、刷新配置中心服务

1、刷新全部

curl -X POST http://localhost:3344/actuator/bus-refresh

2、刷新某一个

curl -X POST http://localhost:3344/actuator/bus-refresh/payment-service:8001

后面跟服务名与端口号。

3、刷新某一个集群

curl -X POST http://localhost:3344/actuator/bus-refresh/payment-service:**

注:用到了配置文件内的属性的类上要加上热刷新注解:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值