spring cloud bus與spring cloud config整合實現應用配置動態重新整理
準備工作,
在碼雲上 建立 一個 專案,並在在目錄下建立 spring_cloud_in_action/config-repo 層級目錄,其中儲存了應用名為shendu的多環境配置檔案,配置檔案中有一個from引數
spring boot 版本是 1.5.14
eureka 註冊中心
配置檔案 |
server.port=1111 eureka.instance.hostname=localhost #由於該應用為註冊中心,所有設定為false,代表不向註冊中心註冊自己 eureka.client.register-with-eureka=false #由於註冊中心的職責就是維護服務例項,它並不需要去檢索服務,所以也設定為false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ |
啟動類 |
package springcloud.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer // 註解啟動一個服務註冊中心 @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } |
pom 依賴 |
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
config server
配置檔案 |
spring.application.name=config-server server.port=7001 spring.cloud.config.server.git.uri=https://gitee.com/shenduedu/spring_cloud_config_server/ # 配置倉庫路徑下的相對搜尋位置,可以配置多個 spring.cloud.config.server.git.search-paths=spring_cloud_in_action/config-repo spring.cloud.config.server.git.username=shenduedu spring.cloud.config.server.git.password=a6615925 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ management.security.enabled=false spring.rabbitmq.addresses=192.168.86.132 spring.rabbitmq.port=5672 spring.rabbitmq.username=springcloud spring.rabbitmq.password=springcloud |
啟動類 |
package springcloud.config_server_bus; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerBusApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerBusApplication.class, args); } } |
pom 依賴 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
config client
spring-boot-starter-actuator,spring-cloud-starter-bus-amqp 必須要加
建立 bootstrap.properties,並寫出 |
spring.application.name=shendu spring.cloud.config.profile=dev spring.cloud.config.label=master #spring.cloud.config.uri=http://localhost:7001/ server.port=7002 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ # 開啟通過服務來訪問config server的功能 spring.cloud.config.discovery.enabled=true # 指定註冊的服務名 spring.cloud.config.discovery.service-id=config-server management.security.enabled=false spring.rabbitmq.addresses=192.168.86.132 spring.rabbitmq.port=5672 spring.rabbitmq.username=springcloud spring.rabbitmq.password=springcloud |
啟動類 |
package springcloud.config_client_bus; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class ConfigClientBusApplication { public static void main(String[] args) { new SpringApplicationBuilder(ConfigClientBusApplication.class).web(true).run(args); } } |
controller |
package springcloud.config_client_bus; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope @RestController public class TestController { @Value("${from}") private String from; @RequestMapping("/from") public String from(){ return this.from; } } |
pom 依賴 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> |
先返回 config_client的from請求,接著修改 dev配置檔案的內容
,然後用postman 傳送一個請求 到 config_server的 /bus/refresh/
這時再訪問 config_client 的from 時已經更新了