springCloud config(集成一个config实现自动刷新)(入门系列)

前提 

安装rabbitmq

在下面连接的基础上

https://blog.csdn.net/z446981439/article/details/103628088

一、实现原理(出自 https://blog.csdn.net/wtdm_160604/article/details/83720391

1、ConfigServer(配置中心服务端)从远端git拉取配置文件并在本地git一份,ConfigClient(微服务)从ConfigServer端获取自己对应 配置文件;

2、当远端git仓库配置文件发生改变,ConfigServer如何通知到ConfigClient端,即ConfigClient如何感知到配置发生更新?

Spring Cloud Bus会向外提供一个http接口,即图中的/bus/refresh。我们将这个接口配置到远程的git的webhook上,当git上的文件内容发生变动时,就会自动调用/bus-refresh接口。Bus就会通知config-server,config-server会发布更新消息到消息总线的消息队列中,其他服务订阅到该消息就会信息刷新,从而实现整个微服务进行自动刷新。

二:实现方式

配置中心Server端承担起配置刷新的职责,原理图如下:

1、提交配置触发post请求给server端的bus/refresh接口

2、server端接收到请求并发送给Spring Cloud Bus总线

3、Spring Cloud bus接到消息并通知给其它连接到总线的客户端

4、其它客户端接收到通知,请求Server端获取最新配置

5、全部客户端均获取到最新的配置
三、新增pom jar

       <!-- springcloud-bus依赖实现配置自动更新,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>
    </dependencies>

四、添加相关配置 .yml

server:
  port: 8201
spring:
  rabbitmq:
    host: 172.18.0.88
    port: 5672
    username: guest
    password: guest
    listener:
      simple:
        concurrency: 10
        max-concurrency: 20
        prefetch: 5
    virtual-host: /
  application:
    name: config-service
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/shanghai_wuji/config-repo.git
          search-paths: '{application}'   #占位符  应用名
          # 每次都从仓库拉取文件,防止本地脏读
          force-pull: true
          #username:  公共开源可不要账号密码
          #password:
          default-label: master #git分支
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8100/eureka/
  instance:
    prefer-ip-address: true
management:
  endpoints:
    web:
      exposure:
        include: "*"  #SpringCloud 2.0.0版本以后暴露接口方式
  endpoint:
    bus-refresh:
      enabled: true

至此  config-service配置完毕

五、config客户端配置

1、pom文件添加 jar

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <!--监控系统健康情况的工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2、使用 新建一个控制层

package com.zzh.elite.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
@Api(description = "配置中心自动刷新测试")
public class TestConfigController {
    @Value("${logging.config}")
    private String profile;
    @RequestMapping("/getConfig")
    @ApiOperation("配置中心自动刷新测试")
    public String getConfig(){
        return "读取配置中心"+profile;
    }
}

3、访问该路径 http://localhost:8000/elite-service/swagger-ui.html#/test-config-controller/getConfigUsingGET

4、修改相应的配置文件属性 post请求 http://localhost:8201/actuator/bus-refresh (刷新)

应该在码云配置请求路径 本地测试可手动调用

码云配置

5、再次访问 http://localhost:8000/elite-service/swagger-ui.html#/test-config-controller/getConfigUsingGET

对比两次结果是否发生改变 

如改变则成功

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值