一、实现原理

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会发布更新消息到消息总线的消息队列中,其他服务订阅到该消息就会信息刷新,从而实现整个微服务进行自动刷新。

二:实现方式

实现方式一:某个微服务承担配置刷新的职责

1、提交配置触发post调用客户端A的bus/refresh接口

2、客户端A接收到请求从Server端更新配置并且发送给Spring Cloud Bus总线

3、Spring Cloud bus接到消息并通知给其它连接在总线上的客户端,所有总线上的客户端均能收到消息

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

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

存在问题:

1、打破了微服务的职责单一性。微服务本身是业务模块,它本不应该承担配置刷新的职责。2、破坏了微服务各节点的对等性。3、有一定的局限性。WebHook的配置随着承担刷新配置的微服务节点发生改变。

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

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

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

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

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

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

第一种配置:

启动rabbitmq

rabbitmq: docker run -d -p 5672:5672 -p 15672:15672 rabbitmq:management
  • product-service

    • pom.xml

                     <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: 192.168.180.112
      port: 5672
      username: guest
      password: guest #暴露全部的监控信息
      management:
      endpoints:
      web:
      exposure:
      include: "*"
    • web层

      @RestController
      @RequestScope
      @RequestMapping("/api/v1/product")
      public class ProductController {



      @Value("${server.port}")
      private String port;

      @Autowired
      private ProductService productService;

      /**
      * 获取所有商品列表
      * @return
      */
      @RequestMapping("list")
      public Object list(){
      return productService.listProduct();
      }


      /**
      * 根据id查找商品详情
      * @param id
      * @return
      */
      @RequestMapping("find")
      public Object findById(int id){

      // try {
      // TimeUnit.SECONDS.sleep(2);
      // } catch (InterruptedException e) {
      // e.printStackTrace();
      // }
      Product product = productService.findById(id);

      Product result = new Product();
      BeanUtils.copyProperties(product,result);
      result.setName( result.getName() + " data from port="+port );
      return result;
      }

      }
    • 测试,要手动发送POST http://localhost:8773/actuator/bus-refresh

      就会发现控制台会重新加载配置信息

第二种配置:

其实也是把config-server连到Bus和mq中,然后去请求它,其他服务才进行重新加载。

  • config-server

    • pom.xml

        <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>
      <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
    • application.yml

      #服务名称
      spring:
      application:
      name: config-server
      cloud:
      config:
      server:
      git:
      uri: http://192.168.180.112/root/test.git
      username: root
      password: ***********
      default-label: master
      rabbitmq:
      host: 192.168.180.112
      username: guest
      password: guest
      port: 5672
      management:
      endpoints:
      web:
      exposure:
      include: "*"


      #服务的端口号
      server:
      port: 9100


      #指定注册中心地址
      eureka:
      client:
      serviceUrl:
      defaultZone: http://localhost:8761/eureka/

都会加进队列中。

SpringCloud Config(配置中心)实现配置自动刷新(十六)的更多相关文章

  1. spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.config 简介 spring cloud config 分为服务端 ...

  2. Webpack 2 视频教程 007 - 配置 WDS 进行浏览器自动刷新

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  3. .Net Core 自定义配置源从配置中心读取配置

    配置,几乎所有的应用程序都离不开它..Net Framework时代我们使用App.config.Web.config,到了.Net Core的时代我们使用appsettings.json,这些我们再 ...

  4. Spring Cloud学习笔记【十】配置中心(消息驱动刷新配置)

    上一篇中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用 Webhook 的机制每次提交代码发送请求来刷新客户端,当客户端越来越多的时候,需要每个客户端都执行一遍,这种方案 ...

  5. springcloud eureka注册中心分布式配置

    最近在学习springcloud,做下笔记以及记下遇到的坑. 1.建立maven工程,结构很简单,一个启动类和一个配置文件,结构如下图所示 2.启动类代码如下,需要添加注册中心注解:EnableEur ...

  6. springcloud配置中心客户端配置遇到的坑

    1. 出错信息如下: 在启动配置中心的客户端时,报以下错误信息: Caused by: java.lang.IllegalArgumentException: Could not resolve pl ...

  7. Consul作为配置中心,配置Asp.Net Core应用程序

    前言 最近项目逐步转向基于.Net Core,目前dotnet core 虽然已出3.0了但还没有特别成熟的框架,要实现微服务,必须要解决配置中心的问题 .不管是不是微服务,节点多了配置文件一个个更改 ...

  8. 微服务从nacos配置中心获得配置信息

    一,安装nacos, 略 二,创建父工程和微服务工程 service1, service2,以idea为例 1, new -> project -> Maven -> 填写group ...

  9. SpringBoot整合nacos实现配置中心(配置动态更新)

    官方教程:https://nacos.io/zh-cn/docs/quick-start-spring-boot.html Linux使用docker部署nacos:https://www.cnblo ...

随机推荐

  1. C语言入门-函数

    一.初见函数 求出1到10.20到30和35到45的三个的和 #include <stdio.h> // 定义一个函数 void sum(int begin, int end) { int ...

  2. Spring MVC-从零开始-view-直接返回页面不传data

    1.applicationContext配置 <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  3. if [ $# -ne 1 ] 作用

    在shell脚本中经常会使用if [ $# -ne 1 ];then...这类脚本 ];then 这段命令是用于判断参数的个数是否为1,不是则进行then的逻辑处理,其中$#表示参数个数,-ne是不等 ...

  4. Java基础学习笔记(一) - 基础语法

    1.Java程序开发过程 编译: 是指将我们编写的Java源文件翻译成JVM认识的class文件,javac编译器会检查我们所写的程序是否有错误,有错误就会提示出来,如果没有错误就会编译成功. 运行: ...

  5. Terminal MultipleXer---终端复用器tmux基本使用

    Terminal MultipleXer---终端复用器tmux 使用场景:1.scp大文件 2:编译大文件 3:多窗口对比文件 1.安装tmux [root@localhost ~]# yum in ...

  6. linux脚本入门之终端显示输出

    主要基本命令为 echo 与 printf. 关于echo: 其语法结构为:echo -选项参数 字符串: 例如:echo hello,world   echo 'hello,world'  echo ...

  7. python爬虫——爬取B站用户在线人数

    国庆期间想要统计一下bilibili网站的在线人数变化,写了一个简单的爬虫程序.主要是对https://api.bilibili.com/x/web-interface/online返回的参数进行分析 ...

  8. Rust入坑指南:坑主驾到

    欢迎大家和我一起入坑Rust,以后我就是坑主,我主要负责在前面挖坑,各位可以在上面看,有手痒的也可以和我一起挖.这个坑到底有多深?我也不知道,我是抱着有多深就挖多深的心态来的,下面我先跳了,各位请随意 ...

  9. eclipse中Tomcat version 9.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5, 6, 7, and 8 Web modules

    eclipse中导入了一个别人的项目,运行时提示没有可以使用的服务器,如下: 查看了下项目属性设置中的服务器,还是提示没有可用服务器: 尝试对部署在已有服务器下的项目Add and Remove... ...

  10. oracle表空间不足:ORA-01653: unable to extend table

    问题背景: oracle表空间不足报错是比较常见的故障,尤其是没有对剩余表空间做定期巡检的系统: 报错代码如下: oracle表空间不足错误代码:ORA-01653: unable to extend ...