08.SpringCloud之Bus

时间:2020-9-19 作者:admin

一.简介

SpringCloudBus是SpringCloud的消息总线,通过消息组件代理各个连接分布点,当配置文件发生变化后可以通过SpringCloudBus实现配置信息动态抓取,实现配置项的动态更新。

二.实现

1.SpringCloudConfigServer和需要动态抓取配置的服务都增加依赖库

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

2.配置SpringCloudConfigServer

server:
  port: 8501


spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/vincent-java-test/spring-cloud-profiles.git
          username: xxxxxx@163.com
          password: xxxxxx
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/service-eureka/eureka

management:
  security:
    #禁用actuator安全校验
    enabled: false

3.启动SpringCloudConfigServer,并访问http://localhost:15672
08.SpringCloud之Bus
SpringCloudConfig服务启动后将会在RabbitMQ中新建springCloudBus名称的topic Exechange。
08.SpringCloud之Bus
查看SpringCloudConfig 队列详情可知routing key 为#。

4.配置service-user微服务的bootstrap.yml

spring:
  cloud:
    config:
      label: master
      name: service-user
      profile: prod
      discovery:
        enabled: true
        service-id: config-server

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/service-eureka/eureka

management:
  security:
    enabled: false

5.配置service-user微服务的application.yml,并推送到git

server:
  port: 8001
  context-path: /service-user

spring:
  application:
    name: service-user

  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: admin


application:
  version: 1.0.0

08.SpringCloud之Bus
6.把service-user微服务动态更新内容注入容器

package com.vincent.component;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;


@Data
@Component
@RefreshScope
public class RefreshComponent {
    @Value("${application.version}")
    private String version;
}

@RefreshScope 标注的类表示需要动态更新。

7.编写service-user微服务Controller返回RefreshCompnent信息

package com.vincent.controller;

import com.alibaba.fastjson.JSONObject;
import com.vincent.component.RefreshComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
public class UserController {
    @Autowired
    private RefreshComponent refreshComponent;


    @GetMapping("/detail")
    public Object detail(Integer id, HttpServletRequest request){
        JSONObject rst = new JSONObject();
        rst.put("date",new Date());
        rst.put("version",this.refreshComponent.getVersion());
        return rst;
    }
}

8.启动service-user微服务,查看RabbitMQ中Queues将可以看到多了一个队列
08.SpringCloud之Bus
9.访问service-user微服务,http://localhost:8001/service-user/detail?id=1
08.SpringCloud之Bus
10.修改service-user-prod.yml配置文件,并提交到git服务器
08.SpringCloud之Bus

11.通过POST方式访问http://localhost:8501/bus/refresh,刷新配置服务SpringCloudConfigServer

12.再次访问service-user微服务,http://localhost:8001/service-user/detail?id=1
08.SpringCloud之Bus
动态抓取配置信息成功。

三.总结

SpringCloud通过消息组件代理(如RabbitMQ) 连接各个分布点,通过Topic方式实现配置文件动态抓取更新。

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。