Bus组件的作用,当我们把配置文件保存在Git当中,由Git进行管理,一旦配置字段的值进行改变
    不需要采用服务器重启的方式去更新配置。
    config-server依赖
    pom

    1. <!--配置中心的依赖-->
    2. <dependency>
    3. <groupId>org.springframework.cloud</groupId>
    4. <artifactId>spring-cloud-config-server</artifactId>
    5. </dependency>
    6. <!--增加amqp依赖-->
    7. <!--因为作者采用的是rabbitmq,所以这里需要引入amqp的依赖客户端和服务端都要引入此依赖-->
    8. <dependency>
    9. <groupId>org.springframework.cloud</groupId>
    10. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    11. </dependency>

    启动类

    package com.tg.config_server;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    
    @SpringBootApplication
    @EnableConfigServer
    @EnableEurekaClient//开启eureka的注册服务
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run( ConfigServerApplication.class, args );
        }
    }
    
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
    #git配置项
            git:
              uri: https://github.com/tanguang163/SpringCloud.git/
              username:
              password:
              search-paths: /
          name: application
          profile: dev
    server:
      port: 8888
    management:
      endpoints:
        web:
          exposure:
            include: refresh,health,info,bus-refresh #开放出去的端口  
    eureka:
      client:
        serviceUrl:
          #注册中心的地址
          defaultZone: http://localhost:8761/eureka/eureka
    

    config-client

    <!--客户端和服务端都需要引入此依赖-->
        <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
    

    controller

    package com.tg.config_client;
    
    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;
    
    @RestController
    @RefreshScope#刷新配置
    public class RestHiController {
    
        @Value("${spring.version}")
        private String port;
    
        @GetMapping(value = "/hi")
        public String getSpringVersion() {
            return "SpringVersion:" + port;
        }
    }
    

    yml文件

    spring:
      rabbitmq:
        host: localhost
        username: guest
        password: guest
        port: 5672
      cloud:
        config:
          uri: http://localhost:8888
          profile: dev
          name: application
    eureka:
      client:
        serviceUrl:
          #注册中心的地址
          defaultZone: http://localhost:8761/eureka/eureka
    server:
      port: 9999
    

    当git的仓库的文件一旦被改变
    采用post方式进行访问服务端刷新配置

     http://localhost:8888/actuator/bus-refresh
    

    SpringCloud之Bus组件 - 图1

    这时候我们也可以看到rabbitmq发送通知的记录配置文件被更改
    SpringCloud之Bus组件 - 图2

    重新访问客户端的controller验证 文件成功修改
    SpringCloud之Bus组件 - 图3