1. 启动Rabbit队列
2. 增加Rabbit依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
<!--bus总线-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
3. yml配置
#####单机版Config Server 直连#######
#spring:
# application:
# name: config-client
# cloud:
# config:
# uri: http://localhost:9800 #服务端地址
# fail-fast: true
# profile: dev # 启用的profile
# label: release1.0 # 启用的分支
#####集群版Config Server从Eureka Server获取服务#######
spring:
application:
name: config-client
cloud:
config:
label: release1.0
discovery:
service-id: config-server
enabled: true
fail-fast: true
profile: dev
bus:
enabled: true #默认配置 可以不写
refresh:
enabled: true #默认配置 可以不写
rabbitmq:
host: localhost #默认配置 可以不写
port: 5672 #默认配置 可以不写
username: guest #默认配置 可以不写
password: guest #默认配置 可以不写
eureka:
client:
serviceUrl:
defaultZone: http://eureka-server-7001:7001/eureka/,http://eureka-server-7002:7002/eureka/,http://eureka-server-7003:7003/eureka/
instance:
instance-id: config-client-9900 # 服务名称
prefer-ip-address: true # 显示ip地址
info: # 点击注册列表未服务出现的信息
app.name: springcloud
company.name: www.honor.com
build.artifactId: @project.artifactId@
build.version: @project.version@
management:
endpoints:
web:
exposure:
include: bus-refresh # 开启刷新配置
3. 主启动类
@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope // 总线刷新
public class ConfigClient9900Application {
public static void main(String[] args) {
SpringApplication.run(ConfigClient9900Application.class, args);
}
// 配置文件注入
@Value("${foo}")
private String foo;
@Value("${label:master}")
private String label;
@GetMapping("/getconfig")
public String getConfig() {
return "Current label is " + label + ", and the foo content is " + foo + ".";
}
}
4. 测试
- 启动Eureka-Server
- 启动Config-Server
- 启动Config-client
- 访问http://localhost:9900/getconfig出现
Current label is release1.0, and the foo content is dev foo version.
- 修改git上配置文件
- 使用postman或者其它工具发送
http://localhost:9900/actuator/bus-refresh
- 再次访问http://localhost:9900/getconfig出现,配置内容发生变更,说明刷新成功
5. 配置方式
5.1 刷新在微服务上
所有的微服务应用开启Spring Cloud Bus 由其中一台服务刷新
5.2 刷新在Config Server上
所有的微服务及Config Server 开启Spring Cloud Bus,在Config Server上刷新即可
6. 刷新范围
// 刷新指定的服务及端口的微服务应用
http://localhost:9900/actuator/bus-refresh?destnation=服务名:端口
// 刷新指定的服务的所有端口微服务应用
http://localhost:9900/actuator/bus-refresh?destnation=服务名:**