Spring Cloud Bus (1)快速应用
消息总线,本文旨在快速搭建一个Spring Cloud Bus 的应用demo;将在后续文章更深入的分析。
bus+config 加以说明当配置文件被修改时,实现bus通知客户端刷新配置。
前提: 使用Spring Cloud Bus时需要准备一个可用的RabbitMQ或Kafka 或其他的消息服务器。
Bus不需要单独的服务应用,只需在有需要用到bus功能的服务中开启bus功能
本文所用的基本版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>15</java.version>
<spring-cloud.version>2020.0.1</spring-cloud.version>
</properties>
1、单客户端配置刷新
client-dev.yml文件放置本地目录,或者远程仓库中;
#后面client读取到就会以这个端口号启动
server:
port: 8081
nickName: new world
1.1、配置中心服务器Config Server
1.1.1、依赖包 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--bus-->
<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>
1.1.2、配置文件 application.yml
server:
port: 8080
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
native:
searchLocations: D:/***/Cloud/bus/config
#开启消息追踪
bus:
trace:
enabled: true
#rabbitmq服务 默认账号密码guest
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
endpoints:
web:
exposure:
include: "*"
1.1.3、应用入口
@SpringBootApplication
@EnableConfigServer //声明为配置文件服务器 为client提供配置读取服务
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
1.2、Config Client
1.2.1、依赖包 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--从2.4.0版本后bootstrap被分离为单独一个服务应用
如果需要从bootstrap.yml或bootstrap.properties启动则需要引入此依赖
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!--bus-->
<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>
1.2.2、配置文件 bootstrap.yml
spring:
application:
name: client
cloud:
config:
uri: http://localhost:8080
name: client
profile: dev
label: master
profiles:
active: dev
#开启消息追踪
bus:
trace:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
#2.0以后的暴露接口方式
management:
endpoints:
web:
exposure:
include: "*"
1.2.3、应用入口
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
1.2.4、测试类
@RestController
@RefreshScope //获取最新配置
public class HelloController {
@Autowired
private Environment env;
@Value("${nickName}")
private String nickName;
@RequestMapping("/hello")
public String hello(){
return "Hello "+nickName;
}
}
当修改了本地配置文件时,使用工具发起一个post请求: http://localhost:8081/actuator/refresh ;即可刷新客户端读取配置文件。但如果有很多个客户端,那就要一个个请求客户端刷新,比较麻烦。因此常常与eureka一起实现只刷新服务器即可更新所有客户端的配置。
2、刷新所有客户端的配置
2.1、eureka server
2.1.1、依赖包 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.1.2、配置文件 application.yml
server:
port: 8700
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
fetch-registry: false
register-with-eureka: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false
enewal-percent-threshold: 0.5
2.1.3、应用入口
@SpringBootApplication
@EnableEurekaServer
public class BusApplication {
public static void main(String[] args) {
SpringApplication.run(BusApplication.class, args);
}
}
2.2、config server
2.2.1、依赖包 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--bus-->
<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.2.2、配置文件 application.yml
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8700/eureka/
#读取本地
spring:
application:
name: config-server
profiles:
active: native
cloud:
config:
server:
native:
searchLocations: D:/课件/社大/java/Cloud/bus/config
#开启消息追踪
bus:
trace:
enabled: true
#本地rabbitmq不需配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
management:
#1.5版本暴露接口
#security:
#enabled: false
#2.0后版本暴露接口
endpoints:
web:
exposure:
include: "*"
endpoint:
bus-refresh:
enabled: true
2.2.3、应用入口
@SpringBootApplication
@EnableConfigServer //声明为配置文件服务器 为client提供配置读取服务
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
2.3、config client
2.3.1、依赖包 pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--从2.4.0版本后bootstrap被分离为单独一个服务应用
如果需要从bootstrap.yml或bootstrap.properties启动则需要引入此依赖
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--bus-->
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
2.3.2、配置文件 bootstrap.yml
使用服务器统一刷新时,客户端不需暴露接口
server:
port: 8081
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8700/eureka/
spring:
application:
name: config-client
profiles:
active: dev
cloud:
config:
uri: http://localhost:8080
name: client
profile: dev
label: master
#开启消息追踪
bus:
trace:
enabled: true
#本地rabbitmq不需配置
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
2.3.4、测试类
@RestController
@RefreshScope //获取最新配置
public class HelloController {
@Autowired
private Environment env;
@Value("${nickName}")
private String nickName;
@RequestMapping("/hello")
public String hello(){
return "Hello "+nickName;
}
}
修改本地配置文件后,使用post请求访问: http://localhost:8080/actuator/busrefresh 进行刷新所有客户端配置。
注意:当不同版本访问的接口不相同,有/bus/refresh/、/actuator/bus-refresh、/actuator/busrefresh;需先确认对应版本接口访问刷新