1 消息总线介绍
消息总线是一种通信工具,可以在机器之间互相传输消息、文件等。消息总线扮演着一种消息路由的角色,拥有一套完备的路由机制来决定消息传输方向。发送端只需要向消息总线发出消息而不用管消息被如何转发。Spring Cloud Bus 通过轻量消息代理连接各个分布的节点。管理和传播所有分布式项目中的消息,本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。
2 改造配置客户端(Config Client)
2.1基于上次工程进行改造,在config-client的pom文件中添加spring-cloud-starter-bus-amqp依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.laravelshao.springcloud</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 在配置文件bootstrap.yml中添加rabbitmq相关配置信息:
server:
port: 8881
management:
security:
enabled: false # 设置是否启用安全限制
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8888/ # 配置服务中心地址
label: master # 远程仓库分支
profile: dev # 指定环境
discovery:
enabled: true # 从配置中心读取文件
service-id: config-server # 配置中心服务id
# rabbitmq配置
rabbitmq:
host: localhost
port: 5672
# username:
# password:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8889/eureka/ # 配置eureka服务器地址
management.security.enabled:设置是否启用安全限制
注意事项:需要配置management.security.enabled: false不启用安全限制,否则在请求刷新bus时会显示:
{
"timestamp": 1512118086064,
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource.",
"path": "/bus/refresh"
}
后台也会提示添加安全策略或者设置不启用安全限制。
Full authentication is required to access actuator endpoints. Consider adding Spring Security or set 'management.security.enabled' to false.
2.3 在启动类添加@RefreshScope注解
@SpringBootApplication
@RestController
@RefreshScope
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${hello}")
String hello;
@RequestMapping("/hi")
public String hi() {
return hello;
}
}
2.4 依次启动eureka-server、config-server、config-client(启动两个实例,端口为8881、8882)
请求http://localhost:8881/hi 或 http://localhost:8882/hi,返回信息:
hello config server
2.5修改配置仓库中config-client-dev.properties中hello对应的值为hello config server change,使用POST请求http://localhost:8881/bus/refresh,会发现config-client重新读取了配置文件
再次请求http://localhost:8881/hi 或 http://localhost:8882/hi,返回更新后的信息:
hello config server change
本文源码下载地址:
https://github.com/laravelshao/spring-cloud-learning/tree/master/setion07-bus