Config配置中心--Bus消息总线

搭建配置总控中心

  1. github上新建仓库
  2. 建模块添加依赖
  3. 修改yml
cloud:
  config:
    server:
      git:
        username: #####
        password: #####
        uri: https://github.com/illfg/Config-Center.git 
        #根目录
        search-paths:
          - Config-Center
    label: master

4.添加映射(可选 host文件中 127.0.0.1 config-2333.com )
5.启动类上添加注解@EnableConfigServer

客户端搭建

bootstrap.yml 配置文件 优先级高于application.yml 可以将控制中心的配置文件放入bootstrap中,用户自己的放入application中
1.新建bootstrap.yml

cloud:
  config:
    label: master
    name: config
    profile: dev
    uri: http://localhost:2333


  consul:
    port: 8500
    host: localhost
    discovery:
      service-name: ${spring.application.name}

自测

@Value("${config}")
String config;
@RequestMapping("getConfig")
@ResponseBody
public String test(){
    return config;
}
  

客户端动态刷新问题
1.添加actuator依赖
2.暴露监控端口

management:
  endpoints:
    web:
      exposure:
        include: refresh,health

3.controller上添加@RefreshScope

@Controller
@RefreshScope
public class TestHandler {


    @Value("${config}")
    String config;
    @RequestMapping("getConfig")
    @ResponseBody
    public String test(){
        return config;
    }
}

4.当配置文件修改时,需要发送一个post请求给客户端,使其刷新

http://localhost:2333/actuator/refresh

但是通过请求每一个微服务获得刷新过于麻烦,故我们可以借助Bus消息总线

Bus消息总线

安装环境
1.下载安装Erlang https://www.erlang.org/downloads
2.下载安装rabbitMQ https://www.rabbitmq.com/install-windows.html#installer
3.进入RabbitMQ安装目录下的sbin,执行命令rabbitmq-plugins enable rabbitmq_management 安装
4.访问local host:15672查看是否安装成功 --要先启动服务
5.默认账号密码:guest guest

使用
服务端:
1.添加消息总线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>

2.yml配置

rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
     
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"

客户端:
1.添加依赖

<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>

2.yml配置

rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

当GitHub配置文件刷新后,给配置中心发送一个post请求即可

http://localhost:2333/actuator/bus-refresh

通知指定的客户端

http://localhost:2333/actuator/bus-refresh/服务名:端口号

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值