资讯专栏INFORMATION COLUMN

SpringCloud(第 037 篇)通过bus/refresh半自动刷新ConfigClient

CloudwiseAPM / 2759人阅读

摘要:添加应用启动类通过半自动刷新配置。配置客户端服务想要实现自动刷新配置的话,一端是不要做任何处理,只需要在一端处理即可。

SpringCloud(第 037 篇)通过bus/refresh半自动刷新ConfigClient配置

-

一、大致介绍
1、上章节我们讲到了手动刷新配置,但是我们假设如果微服务一多的话,那么我们是不是需要对每台服务进行手动刷新呢?
2、答案肯定是不需要的,我们也可以采用 rabbitmq 消息中间件产品来增强刷新机制;

3、这里还顺便列举下配置路径的规则:
/****************************************************************************************
 * 配置服务的路劲规则:
 *
 * /{application}/{profile}[/{label}]
 * /{application}-{profile}.yml
 * /{label}/{application}-{profile}.yml
 * /{application}-{profile}.properties
 * /{label}/{application}-{profile}.properties
 ****************************************************************************************/
二、实现步骤 2.1 添加 maven 引用包


    4.0.0

    springms-config-client-refresh-bus
    1.0-SNAPSHOT
    jar

    
        com.springms.cloud
        springms-spring-cloud
        1.0-SNAPSHOT
    

    
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        

    

2.2 添加应用配置文件(springms-config-client-refresh-bus/src/main/resources/application.yml)
server:
  port: 8300


#####################################################################################################
# 配置服务客户端Client应用入口(正常测试 ConfigClient )
# profile: profile-dev
#####################################################################################################





#####################################################################################################
# 配置服务客户端Client应用入口(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?)
# profile: profile-local-dev
#####################################################################################################
2.3 添加 bootstrap.yml 应用配置文件(springms-config-client-refresh-bus/src/main/resources/bootstrap.yml)
#####################################################################################################
# 配置服务客户端Client应用入口(链接 ClientServer 测试)
spring:
  cloud:
    config:
      uri: http://localhost:8220
      profile: refreshbus
      label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master

    bus:
      trace:
        enabled: true   # 设置节点状态跟踪,也可以通过网页 http://localhost:8300/trace 可以看到相关发送事件的数据内容

  application:
    name: foobar  #取 foobar-refreshbus.yml 这个文件的 application 名字,即为 foobar 名称
#####################################################################################################

#####################################################################################################
# rabbitmq 配置:
  rabbitmq:
    host: localhost   # 登录 Rabbitmq 后台管理页面地址为:http://localhost:15672
    port: 5672
    username: guest   # 默认账户
    password: guest   # 默认密码
#####################################################################################################
2.4 添加Web控制层类(springms-config-client-refresh-bus/src/main/java/com/springms/cloud/controller/ConfigClientRefreshBusController.java)
package com.springms.cloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 配置客户端Controller。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
@RestController
@RefreshScope
public class ConfigClientRefreshBusController {

    @Value("${profile}")
    private String profile;

    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}
2.5 添加应用启动类(springms-config-client-refresh-bus/src/main/java/com/springms/cloud/MsConfigClientRefreshBusApplication.java)
package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 通过bus/refresh半自动刷新ConfigClient配置。
* * ConfigClient 配置客户端服务想要实现自动刷新配置的话,ConfigServer 一端是不要做任何处理,只需要在 ConfigClient 一端处理即可。 * * @author hmilyylimh * * @version 0.0.1 * * @date 17/10/18 * */ @SpringBootApplication public class MsConfigClientRefreshBusApplication { public static void main(String[] args) { SpringApplication.run(MsConfigClientRefreshBusApplication.class, args); System.out.println("【【【【【【 ConfigClientRefreshBus微服务 】】】】】】已启动."); } }
三、测试
/****************************************************************************************
 application.yml 涉及到的链接文件内容展示如下:

 修改内容前:
 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refreshbus.yml
 profile: profile-refreshbus

 修改内容后:
 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refreshbus.yml
 profile: profile-refreshbus-refresh
 ****************************************************************************************/





/****************************************************************************************
 Rabbitmq 安装步骤(进入 Rabbitmq 官网:http://www.rabbitmq.com):

 1、下载 rabbitmq-server-3.6.11.exe、otp_win64_20.0-rc2.exe 两个 windows 安装软件;
 2、双击安装 otp_win64_20.0-rc2.exe;
 3、双击安装 rabbitmq-server-3.6.11.exe;
 4、两个都安装完后会发现服务中多了一个 Rabbitmq 的服务,服务名称为:RabbitMQ;
 5、如果想查看管理界面的话,执行命令:rabbitmq-plugins enable rabbitmq_management,然后重启 RabbitMQ 服务;
 6、通过windows命令 netstat -aon|findstr "5672" 查看该端口是否被占用,占用的话,说明安装基本上一切正常;
 7、通过 http://localhost:15672 地址可以进入服务端的管理页面;

 总结:到此为止,Rabbitmq 已经安装完成,接下来准备接入 SpringCloud 生态圈。
 ****************************************************************************************/






/****************************************************************************************
 一、配置刷新服务客户端Client应用入口(通过 bus/refresh 实现半自动动态刷新配置服务客户端配置):

 1、添加注解 RefreshScope,然后添加引用模块 spring-boot-starter-actuator 监控和管理生产环境的模块;
 2、编辑 application.yml、bootstrap.yml 文件,添加相关客户端配置;
 3、启动 springms-config-server 模块服务,启动1个端口;
 4、启动 springms-config-client-refresh-bus 模块服务,启动3个端口(8300、8301、8302);
 5、在浏览器输入地址 http://localhost:8300/profile 正常情况下会输出远端服务的配置内容(内容为:profile: profile-refreshbus);
 6、在浏览器输入地址 http://localhost:8301/profile 正常情况下会输出远端服务的配置内容(内容为:profile: profile-refreshbus);
 7、在浏览器输入地址 http://localhost:8302/profile 正常情况下会输出远端服务的配置内容(内容为:profile: profile-refreshbus);

 8、修改 http://git.oschina.net/ylimhhmily/OpenSource_CustomCircleLineProgressBar/blob/master/foobar-refreshbus.yml 内容,修改后为 profile: profile-refreshbus-refresh;
 9、打开windows命令窗口,执行命令: >curl.exe -X POST http://localhost:8300/bus/refresh 或者端口选择 8301、8302 都可以生效;

 10、然后刷新 http://localhost:8300/profile 网页,正常情况下会输出远端服务的配置内容(内容为:profile: profile-refreshbus-refresh);
 11、然后刷新 http://localhost:8301/profile 网页,正常情况下会输出远端服务的配置内容(内容为:profile: profile-refreshbus-refresh);
 12、然后刷新 http://localhost:8302/profile 网页,正常情况下会输出远端服务的配置内容(内容为:profile: profile-refreshbus-refresh);

 总结:这里通过执行刷新命令,然后将多台 ConfigClient 客户端刷新,来达到获取最新的远端服务器配置。
      但是这里终究还是得靠手动执行一条刷新命令,但总比每台服务器执行刷新命令要好很多;
 ****************************************************************************************/

/****************************************************************************************
 二、配置刷新服务客户端Client应用入口(设置 Git 的 WebHooks 属性,通过 Git 提交代码来实现全自动动态刷新配置服务客户端配置):

 总结:这里我就不做过多的测试,WebHooks 可以设置 POST 的地址,并附上密码,提交代码后动态通知相应服务来实现全自动动态刷新。
 ****************************************************************************************/

/****************************************************************************************
 三、思考问题:凭什么 8300、8301、8302 三台服务器其中一台要承受刷新配置服务的任务?不应该三台服务的角色等级应该相同么?

 基于这种角色等同考虑,可以在 ConfigServer 也配上 Rabbitmq 链接上,然后我们在用命令刷新 ConfigServer 即可,这样就实现了三台 ConfigClient 服务器的角色又等同了;
 ****************************************************************************************/
四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67804.html

相关文章

  • SpringCloud 036 )单点手动动态刷新ConfigClient配置

    摘要:添加应用启动类单点手动动态刷新配置。配置客户端服务想要实现自动刷新配置的话,一端是不要做任何处理,只需要在一端处理即可。 SpringCloud(第 036 篇)单点手动动态刷新ConfigClient配置 - 一、大致介绍 1、当ConfigServer启动后,假如我们新增配置内容的话,是不是要重新启动一下ConfigServer呢? 2、答案肯定是不需要重新启动的,因为 Sprin...

    wanglu1209 评论0 收藏0
  • SpringCloud 029 )配置客户端 ConfigClient 接入配置服务端

    SpringCloud(第 029 篇)配置客户端 ConfigClient 接入配置服务端 - 一、大致介绍 1、有配置服务端,那么势必就会有与之对应的客户端,SpringCloud 文档中集成也非常简单; 2、但是这里有点需要注意,就是 bootstrap 配置文件,官方建议我们在bootstrap中放置不更改的属性,我们同样也需要在这里做一些简单不易于改变的配置; 3、这里还顺便列举下配置...

    YFan 评论0 收藏0
  • SpringCloud 035 )配置服务客户端ConfigClient链接经过认证的配置服务

    SpringCloud(第 035 篇)配置服务客户端ConfigClient链接经过认证的配置服务端 - 一、大致介绍 1、前面一章节讲解了服务端配置安全认证,那么本章节就讲解如何链接上服务端的认证; 2、这里还顺便列举下配置路径的规则: /*****************************************************************************...

    klivitamJ 评论0 收藏0
  • SpringCloud 031 )配置客户端ConfigClient链接经过对称加解密的配置微

    摘要:添加应用启动类配置客户端链接经过对称加解密的配置微服务专门为测试经过对称加解密的配置微服务微服务模块。 SpringCloud(第 031 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务 - 一、大致介绍 1、Git服务端的文件内容进行了加密处理,那么是不是配置客户端拿到内容之后需要解密呢? 2、答案显然不是的,因为这样解密的话,先不说实现起来的难易程度,单从表面...

    DDreach 评论0 收藏0
  • SpringCloud 033 )配置客户端ConfigClient链接经过对称加解密的配置微

    SpringCloud(第 033 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务 - 一、大致介绍 1、在(第 031 篇)讲解了如何链接对称加密的配置服务端,而链接对称非对称加密的配置微服务也是同样的; 2、配置客户端不需要做什么加解密的配置,加解密的配置在服务端做就好了; 3、这里还顺便列举下配置路径的规则: /****************************...

    ARGUS 评论0 收藏0

发表评论

0条评论

CloudwiseAPM

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<