zoukankan      html  css  js  c++  java
  • SpringCloud系列之五---集中配置组件Config+消息总线Bus

    前言

    本篇文章主要介绍的是集中配置组件SpringCloudConfig和消息总线SpringCloudBus这两个springcloud组件。

    GitHub源码链接位于文章底部。

    集中配置组件SpringCloudConfig

    1.SpringCloudConfig 简介

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在 Spring Cloud 中,有分布式配置中心组件 spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程 Git 仓库中。在 spring cloudconfig 组件中,分两个角色,一是 config server,二是 config client。

    Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用 Git 存储配置文件内容,也可以使用 SVN 存储,或者是本地文件存储。

    Config Client 是 Config Server 的客户端,用于操作存储在 Config Server 中的配置内容。微服务在启动时会请求 Config Server 获取配置文件的内容,请求到后再启动容器。

    2.配置客户端

    创建springcloud-config-bus-client客户端项目
    2.1 pom中添加依赖

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    

    2.2 在resource资中添加一个bootstrap.yml配置文件,这个配置是用来连接配置中心服务端,以及开放刷新接口的
    这里的配置和下一步要上传到码云的配置文件名是有关联的。

    spring:
      cloud:
        config:
          #仓库中的文件名是由name-profile组成的,例如 config-dev
          name: config_client
          profile: dev
          #仓库中的分支
          label: master
          #config服务端的url
          uri: http://127.0.0.1:8101
    
    #启动配置刷新接口,访问http://本微服务ip+端口/actuator/refresh
    management:
      endpoints:
        web:
          exposure:
            include: refresh
    

    2.3 创建client_config-dev.yml配置文件

    server:
      port: 8102
    
    spring:
      application:
        name: config_client
    
    my:
      config: configBusTestMsg
    

    使用 GitHub 时,国内的用户经常遇到的问题是访问速度太慢,有时候还会出现无法连接的情况。因此这里使用国内的 Git 托管服务——码云(gitee.com)。

    在码云创建一个仓库config-bus,将client_config-dev.yml配置文件上传到该仓库。复制该仓库的地址,填写到下一步的服务端配置中。

    2.4 创建一个测试的controller,类上添加RefreshScope注解实现刷新功能。
    这里使用Value注解获取配置中的值

    @RestController
    @RefreshScope
    public class ConfigBusController {
        @Value("${my.config}")
        private String myConfig;
    
        @GetMapping("/getConfig")
        public String configBusTest() {
            return myConfig;
        }
    }
    
    3.配置服务端

    创建springcloud-config-bus-server服务端项目

    3.1 pom中添加依赖

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    

    3.2 application.yml配置文件中添加配置

    地址由步骤2.3中获取

    server:
      port: 8101
    spring:
      application:
        name: config-bus
      cloud:
        config:
          server:
            git:
    		  #码云仓库地址
              uri: https://gitee.com/lixianguo/config-bus.git
    

    3.3启动类

    @EnableConfigServer
    @SpringBootApplication
    public class ConfigBusServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigBusServerApplication.class, args);
        }
    }
    
    4.测试

    依次启动服务端、客户端,浏览器访问客户端的接口 localhost:8102/getConfig ,能获取到码云上配置文件中的配置

    修改码云上配置文件

    my:
      config: configBusTestMsg!!!!!!!!!
    
    

    再次访问该接口,发现没有刷新配置,这里需要使用Postman等接口测试工具,使用post请求方式访问localhost:8102/actuator/refresh ,也就是本微服务ip+端口/actuator/refresh

    浏览器再次访问客户端的接口 localhost:8102/getConfig 会发现已经发生改变。

    这样就能实现刷新了。但是分布式项目中的微服务是由很多的,每个微服务都有自己的配置文件,这些配置文件都需要托管到码云上,如果大量的配置文件修改完以后需要刷新,就要访问不同的ip+端口,这样无疑是很麻烦的。如果只刷新一个接口就能刷新所有微服务的配置就好了。 消息总线 SpringCloudBus就解决了这个问题,他将刷新配置的接口从客户端转移到配置中心服务端了,客户端微服务可能有很多个,但是配置中心服务端只有一个。

    消息总线 SpringCloudBus

    Bus需要用到消息中间件来存储消息,如RabbitMq,kafka,本文使用RabbitMq作为示例。RabbitMq的安装与使用参考另一篇文章。

    1.配置客户端

    1.1 pom文件新添加依赖,上面Config的依赖不能删除

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-bus</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    </dependency>
    

    1.2 修改项目中bootstrap.yml配置文件,将刷新接口的配置删除

    spring:
      cloud:
        config:
          #仓库中的文件名是由name-profile组成的,例如 config-dev
          name: config_client
          profile: dev
          #仓库中的分支
          label: master
          #config服务端的url
          uri: http://127.0.0.1:8101
    
    

    1.3 修改码云上的client——config-dev.yml配置文件的配置,添加一个rabbitmq的配置

    2.配置服务端

    2.1 pom文件新添加依赖,上面Config的依赖不能删除

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-bus</artifactId>
    </dependency>
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
    </dependency>
    

    2.2 修改application.yml配置文件,添加RabbitMq和刷新接口的配置

    server:
      port: 8101
    spring:
      application:
        name: config-bus
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/lixianguo/config-bus.git
      rabbitmq:
          host: 127.0.0.1
    management:
      endpoints:
        web:
          exposure:
            include: bus-refresh
    
    3.测试

    依次启动服务端、客户端,浏览器访问客户端的接口 localhost:8102/getConfig ,能获取到码云上配置文件中的配置

    修改码云上配置文件

    my:
      config: configBusTestMsg!!!!!!!!!
    
    

    再次访问该接口,发现没有刷新配置,这里需要使用Postman等接口测试工具,使用post请求方式访问localhost:8101/actuator/refresh ,也就是配置中心微服务ip+端口/actuator/bus-refresh

    再次访问该接口会发现已经刷新配置。

    本文GitHub源码:https://github.com/lixianguo5097/springcloud/tree/master/springcloud-config-bus

    CSDN:https://blog.csdn.net/qq_27682773
    简书:https://www.jianshu.com/u/e99381e6886e
    博客园:https://www.cnblogs.com/lixianguo
    个人博客:https://www.lxgblog.com

  • 相关阅读:
    Rotate to Attend: Convolutional Triplet Attention Module
    论文阅读:《Automatic Change Detection in Synthetic Aperture Radar Images Based on PCANet》
    论文阅读:Change Detection From Synthetic Aperture Radar Images Based on Channel Weighting-Based Deep Cascade Network
    第二次作业:卷积神经网络 part 2
    CGAN和DCGAN
    GAN
    About me
    极大似然估计与贝叶斯估计
    支持向量机(一)
    非参数估计——Parzen窗与k近邻估计
  • 原文地址:https://www.cnblogs.com/lixianguo/p/12525589.html
Copyright © 2011-2022 走看看