zoukankan      html  css  js  c++  java
  • SpringCloudConfig + CloudBus + WebHooks +RibbitMQ,实现配置集中管理和自动刷新

    一、概述

    springCloudConfig同一配置服务,其实就是单独创建一个服务,专门用来管理其它所有服务的配置文件,其它微服务通过与这个配置服务建立连接,拉取配置到各自的服务环境中,springCloudConfig与git仓库关联,相当于先将所有微服务的配置文件放在git远程仓库上,springCloudConfig服务从git拉取配置,其它微服务从springCloudConfig服务上拉取各自的配置。从而就实现了多个微服务配置文件的集中管理。

    二、实现步骤和注意点:

    1.创建服务端(用来从git仓库拉取配置文件)
      microservice-config-server:

    (1)、pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.base</groupId>
        <artifactId>microservice-config-server</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>microservice-config-server</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Dalston.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</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-starter-eureka</artifactId>
            </dependency>
            <!-- 实现通过端点refresh手动刷新 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-monitor</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

     (2)、启动类:

    @SpringBootApplication
    @EnableConfigServer
    @EnableDiscoveryClient
    public class MicroserviceConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MicroserviceConfigServerApplication.class, args);
        }
    
    }
    

     主要是加上@EnableConfigServer,表示当前服务是个配置服务。

    (3)、yml配置

    server:
      port: 9009    #本服务的端口
    spring:
      application:
        name: microservice-config-server   #本服务名称
      cloud:
        config:
          server:
            git:                           #git地址
              uri: https://github.com/47Gamer-github/spring-cloud-config.git
              username: 47Gamer-github
              password: ????
    #          clone-on-start: true
      rabbitmq:
        addresses: 127.0.0.1:5672   #mq服务器地址
        username: 47Gamer        #账号
        password: ????????          #密码
    management:                     #开放所有端口
      endpoints:
        web:
          exposure:
            include: "*"
      security:                    #关闭安全验证
        enabled: false
    eureka:                        #注册到eureka设置
      client:
        serviceUrl:
          defaultZone: http://localhost:1001/eureka/
      instance:
        instance-id: ${spring.cloud.client.ipAddress}:${server.port}  #设置服务在注册中心的实例id
        prefer-ip-address: true 

    这个yml文件主要就是:

    • 配置了git地址(因为此配置服务需要从git仓库上拉取所有的配置文件)
    • 配置了rabbitMq仓库地址,因为此配置服务需要建立一个消息队列,用于自动刷新配置到各个与之关联的微服务中去(后面会说到的springCloudBus)
    • 配置eureka注册中心地址,用于将此服务注册上去。

    2.创建客户端(用来从配置服务端仓库拉取配置文件)

    (1)、pom:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.4.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.base</groupId>
        <artifactId>microservice-config-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>microservice-config-client</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Dalston.SR1</spring-cloud.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- 实现Config的客户端配置 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <!-- 实现通过端点refresh手动刷新 -->
            <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-starter-eureka</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    (2)、bootstrap.yml:

    spring:
            application:
              name: microservice-foo                            #这里对应的是git仓库里面的配置文件application-profile中的application部分
            cloud:
              config:
                #      uri: http://localhost:9009
                profile: dev                                    #这里对应的是git仓库里面的配置文件application-profile中的profile部分
                label: master
                discovery:
                  enabled: true                                  # 表示使用服务发现组件中的Config Server,而不自己指定Config Server的uri,默认false
                  service-id: microservice-config-server         # 指定Config Server在服务发现中的serviceId,默认是configserver
              bus:
                trace:
                  enabled: true
              rabbitmq:
                addresses: 127.0.0.1:5672   #mq服务器地址
                username: 47Gamer        #账号
                password: ????????          #密码
    

     主要是配置了

    <1>、从那个配置服务端拉取配置文件(service-id: microservice-config-server)
    <2>、拉取仓库里面对应的文件中的属性(spring.application.name和config.profile和config.label)

    (3)、application.yml:

    server:
      port: 9008
    management:                    #开放所有端点
      endpoints:
        web:
          exposure:
            include: "*"
      security:                     #关闭安全验证
        enabled: false
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:1001/eureka/
      instance:
        instance-id: ${spring.cloud.client.ipAddress}:${server.port}  #设置服务在注册中心的实例id
        prefer-ip-address: true
    

    (4)、创建一个配置类

    @Component
    @RefreshScope
    public class ConfigProperties {
    
        @Value("${encrypt.key}")
        private String key;
    
        public String getKey() {
            return key;
        }
    
        public void setKey(String key) {
            this.key = key;
        }
    }
    

     @RefreshScope这个注解表示,git仓库的文件更新之后,也会更新被@RefreshScope标注的属性值(可以直接标注在类上,也可以直接标注到具体的属性值上)。

    手动更新: post方式请求配置服务端的/bus/refresh端点,就可以更新所有与这个服务端想关联的微服务的配置属性。

    自动更新:

    也就是在git仓库修改文件属性后,不需要手动请求服务端的/bus/refresh端点,自动更新。比如是gitHub远程仓库,那么需要设置仓库的webhook地址为服务端的/bus/refresh端点地址,设置好了之后,修改git文件,直接触发 服务端ip:端口/bus/refresh这个请求.从而实现自动更新。

    注意:webhook地址只能设置为公网地址,设置为内网地址是不能生效的。

  • 相关阅读:
    100道MySQL数据库经典面试题解析(收藏版)
    input()函数的进阶用法
    MySQL数据库面试题(2020最新版)
    mysql 1418错误_MySQL 错误1418 的原因分析及解决方法
    使用pymysql循环删除重复数据,并修改自增字段偏移值
    字典get方法和setdesault方法,统计message中各元素的出现频次
    Python中字典get方法的使用技巧
    collections模块
    python的30个编程技巧
    SQL中where与having的区别
  • 原文地址:https://www.cnblogs.com/47Gamer/p/13440285.html
Copyright © 2011-2022 走看看