Spring Cloud Bus消息总线

准备工做

环境以及版本:
tools vision
IDEA 2018.3
SpringBoot 2.1.3 RELEASE
Gradle 5.2.1+
JDK 1.8
Spring Cloud Greenwich.RELEASE
说明

如下改造均在子项目进行操做,父项目的github地址,下述需下载父项目用来管理公共依赖:
github.com/cuifuan/spr…java

1.改造你的 config-server

1.1 构建配置文件 build.gradle
dependencies {
    implementation "org.springframework.cloud:spring-cloud-config-server"
    implementation "org.springframework.cloud:spring-cloud-bus"
    implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}
复制代码
1.2 配置文件 application.yml
server:
 port: 7001
spring:
 application:
 name: config-server
 cloud:
 config:
 server:
 git:
 uri: git地址 #例如https://github.com/cuifuan/springcloud-configuration.git
 search-paths: 仓库文件下文件夹
 default-label: master
 username: git帐号
 password: git密码
 bus:
 trace:
 enabled: true
 rabbitmq:
 host: rabbitmq地址
 port: 5672
 username: rabbit帐号【默认:guest】
 password: rabbit密码【默认:guest】
 virtual-host: /
eureka:
# 修改在服务中心的地址status为 ip+端口 【例如:10.0.0.100:88】
 instance:
 prefer-ip-address: true
 instance-id: ${spring.cloud.client.ip-address}:${server.port}
 client:
 serviceUrl:
 defaultZone: http://springcloud-tools:8761/eureka/
info:
 app:
 description: This is Spring Cloud remote Registration center.
 name: tools-config-server
 version: 0.0
management:
 endpoint:
 bus-refresh:
 enabled: true
 endpoints:
 web:
 exposure:
 include: refresh,bus-refresh
复制代码
1.3 启动类 ToolsConfigServerAppliaction.java
package store.zabbix.config;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableConfigServer
@SpringCloudApplication
@RestController
public class ToolsConfigServerAppliaction {

    public static void main(String[] args) {
        SpringApplication.run(ToolsConfigServerAppliaction.class, args);
    }

    @RequestMapping("/")
    public String home() {
        return "Hello World! My name is configserver.";
    }

}
复制代码

2. 改造你的 config-client (客户端)

用来读取配置文件的git

2.1 构建配置文件 build.gradle
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation "org.springframework.cloud:spring-cloud-bus"
    implementation "org.springframework.cloud:spring-cloud-starter-bus-amqp"
}
复制代码
2.2 配置文件 bootstrap.yml
spring:
 cloud:
 config:
 name: tools-config-client     #对应{application}部分
 profile: dev                  #对应{profile}部分
      #uri: http://localhost:8888/ #配置中心的具体地址
 label: master                 #对应git的分支。若是配置中心使用的是本地存储,则该参数无用
 discovery:
 enabled: true                 #开启Config服务发现支持
 service-id: config-server   #指定配置中心的service-id,便于扩展为高可用配置集群。
eureka:
 instance:
 prefer-ip-address: true
 instance-id: ${spring.cloud.client.ip-address}:${server.port}
 client:
 service-url:
 defaultZone: http://springcloud-tools:8761/eureka/
复制代码
2.2 配置文件 application.yml
server:
 port: 7003
spring:
 application:
 name: tools-config-client
 cloud:
 config:
  #配置重试机制
 retry:
 initial-interval: 2000
 max-attempts: 2000
 max-interval: 2000
 multiplier: 1.2
 fail-fast: true
 bus:
      #动态刷新配置
 refresh:
 enabled: true
      #跟踪总线事件
 trace:
 enabled: true
 rabbitmq:
 host: rabbitmq地址
 port: 5672
 username: rabbit帐号【默认:guest】
 password: rabbit密码【默认:guest】
#配置actuator
# 1.X版本的springboot 配置: management.security.enabled=false 已经做废
#关闭安全认证
management:
 endpoint:
 bus-refresh:
 enabled: true
  #refresh接入点显式暴露出来
 endpoints:
 web:
 exposure:
 include: refresh,bus-refresh
复制代码
2.3 启动类 ConfigClientApplication.java

注意:必定不要忘了加@RefreshScop注解github

package store.zabbix.configreader;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@RefreshScope
@EnableDiscoveryClient
public class ConfigClientApplication {

    /** * http://localhost:8881/actuator/bus-refresh */

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${message}")
    String message;

    @GetMapping("/hello")
    public String getMessage(){
        return message;
    }

}

复制代码

3.远程配置文件

4.操做流程

  • 1 启动config-server
  • 2 启动config-client
  • 3 访问http://127.0.0.1:7003/hello
  • 4 以后更改远程仓库配置文件,提交
  • 5 访问http://127.0.0.1:7003/hello发现无变化
  • 6 用postman作post请求http://10.0.0.82:7001/actuator/bus-refresh

或者用web

curl -X POST http://10.0.0.82:7001/actuator/bus-refresh
复制代码
  • 7 再次访问http://127.0.0.1:7003/hello

这个时候的读取的配置文件已发生变化spring

5.须要注意的坑

  • 请肯定你的rabbitmq是通的,guest帐户没法经过远程ip访问
  • 注意上面的ip地址,我是这边测试使用,请更改为我的正确或者本地
  • springboot2.x以后bus刷新地址更改成 /actuator/bus-refresh
  • springboot和springcloud版本要一致,其余版本没试过
相关文章
相关标签/搜索