SpringCloud之Bus组件

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


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

启动类

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


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

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
image.png

这时候我们也可以看到rabbitmq发送通知的记录配置文件被更改


image.png

重新访问客户端的controller验证 文件成功修改


image.png

你可能感兴趣的:(SpringCloud之Bus组件)