一、简介
Spring cloud bus,消息总线,通过轻量消息代理连接各个分布的节点。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。
二、搭建服务
<?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">
<parent>
<artifactId>mo-cloud</artifactId>
<groupId>com.mo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bus</artifactId>
<description>消息总线服务</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 引入消息总线的依赖,此处引入rabbitmq的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
</project>
# bootstrap.yml
server:
port: 0
spring:
application:
name: bus
# rabbitmq的配置信息
rabbitmq:
addresses: 127.0.0.1
port: 5672
username: guest
password: guest
cloud:
config:
discovery:
service-id: config-cluster
enabled: true
label: master
profile: default
# 开启消息总线
bus:
enabled: true
trace:
enabled: true
# actuator 安全检查开放消息推送刷新
management:
endpoints:
web:
exposure:
include:
# 开放安全检查的端点,bus-refresh端点为推送刷新配置文件的端点
- "info"
- "health"
- "bus-refresh"
eureka:
client:
service-url:
default-zone: http://127.0.0.1:8761/eureka/
instance:
instance-id: ${spring.application.name}:${random.int}
prefer-ip-address: true
package com.mo;
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;
/**
* RefreshScope注解,spring cloud提供的一种特殊的scope实现,用来实现配置、实例热加载
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/16 22:57
*/
@RefreshScope
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class BusApplication {
public static void main(String[] args) {
SpringApplication.run(BusApplication.class, args);
}
@Value("${bus.news}")
private String news;
/**
* 修改远端配置文件,并使用健康检查的推送接口,是spring cloud进行热部署
* <p></p>
* http://127.0.0.1:58299/actuator/bus-refresh
* post请求,header为application/json
*
* @return
*/
@GetMapping("/getNews")
public String getNews() {
return news;
}
}
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
/**
* 单元测试
*
* @author x.pan
* @email px5215201314@163.com
* @date 2020/4/24 0:29
*/
public class ActuatorRefresh {
/**
* 健康检查的推送接口
* http://127.0.0.1:58299/actuator/bus-refresh
* <p>
* 运行单元测试,进行访问接口,推送消息,进行热部署
*/
@Test
public void refresh() {
// 注意修改端口
int port = 0;
String url = "http://127.0.0.1:" + port + "/actuator/bus-refresh";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> requestEntity = new HttpEntity<>(requestHeaders);
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForEntity(url, requestEntity, String.class);
}
}