微服务中的配置中心 Config 和 消息总线 Bus

一、Config基础

1、架构:

(1)Config Server:配置中心,负责去 配置仓库拉取配置
(2)Config Client:客户端启动时从 配置中心获取相应配置

2、优势

  • 集中管理配置
  • 配置分不同环境
  • 动态获取,无需重启服务

二、Config使用

1、服务端配置

  • 增加相应的依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • 项目启动类添加注解 @EnableConfigServer

  • 增加配置
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/linzhuzai/Lin-start-main.git
          search-paths:
            - Lin-start-main  # 得放仓库根目录,不能有文件夹
      label: master

2、客户端配置

  • 增加客户端依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • 项目启动类添加注解 @EnableConfigServer
  • 增加 bootstrap.yml配置(bootstrap.yml 优先级比 application.yml 高)
spring:
  application:
    name: start-main-gateway #影响 github上的文件名
  cloud:
    config:
      uri: http://localhost:9001  #config服务中心地址
      profile: dev
      label: master

3、配置规则

4、动态刷新

(1)客户端引入依赖 actuator

(2)增加 yml配置

management:
  endpoints:
     web:
      exposure:
       include: *

(3)指定 Controller 加上 @RefreshScope

(4)post 请求该服务:http://localhost:9002/actuator/refresh

三、Bus

1、架构:

消息总线:各个微服务实例对其进行监听和消费

四、Bus & Config 动态刷新

注:基于 rabbitmq,所以需先装好

1、Config服务端配置

  • 增加相应的依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
  • 增加配置
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
     web:
      exposure:
       include: bus-refresh

2、Config客户端配置

  • 增加客户端依赖
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
  • 增加 bootstrap.yml配置
spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: guest
    password: guest

3、刷新服务

post 请求 Config服务端:http://localhost:9002/actuator/bus-refresh

4、指定刷新服务(请求中带上要刷新的服务名)

post 请求 Config服务端:http://localhost:9002/actuator/bus-refresh/<service-name>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值