SpringCloud之Bus组件

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

<!--配置中心的依赖-->
  <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--增加amqp依赖-->
 <!--因为作者采用的是rabbitmq,所以这里需要引入amqp的依赖客户端和服务端都要引入此依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </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
image.png

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


image.png

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


image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前言 在微服务架构的系统中,我们通常会使用轻量级的消息代理来构建一个共用的消息主题让系统中所有微服务实例都能连接上...
    二月_春风阅读 10,545评论 0 14
  • 上一篇:《Spring Cloud入门教程(七):分布式链路跟踪(Sleuth)》 本人和同事撰写的《Spring...
    CD826阅读 16,179评论 6 28
  • 统一配置中心概述 如果微服务架构中没有使用统一配置中心时,所存在的问题: 配置文件分散在各个项目里,不方便维护 配...
    爱情小傻蛋阅读 772评论 0 0
  • 吊白居易 唐·李忱 缀玉连珠六十年,谁教冥路作诗仙。 浮云不系名居易,造化无为字乐天。 童子解吟长恨曲,胡儿能唱琵...
    于无阅读 690评论 0 1
  • 文化看不见,摸不着,人们对它却非常执着。 一亲戚家的小弟准备结婚了。新时代新思想,准备男女双方家庭各自聚集亲戚举办...
    路舒阅读 222评论 0 1