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=服務名:**