首先需要在GitHub上面創建一個項目.
然后創建3個模塊:
Cloud-Center 為服務發現中心.
Cloud-Cnofig-Center 為配置中心
Cloud-User 為要分布式配置的模塊
首先創建 Cloud-Center , 引入POM依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
application.yml 文件配置:
eureka: client: service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ register-with-eureka: false fetch-registry: false instance: hostname: localhost spring: application: name: cloud-center server: port: 9001
在啟動類加上 @EnableEurekaServer 注解 , 然后啟動該模塊.
創建第二個模塊 Cloud-Config-Center . 引入POM依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</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-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency>
application.yml 文件配置:
spring: application: name: config-center cloud: config: server: git: uri: git地址(地址后面是否以".git"結尾都可以) username: git賬號 password: git密碼 basedir: e:/cloud-config-git/my-config #默認保存位置.項目啟動后會自動同步git到本地的這個位置 eureka: client: service-url: defaultZone: http://${eureka.instance.hostname}:9001/eureka/ instance: hostname: localhost
management:
endpoints:
web:
exposure:
include: "*" #暴露出所有的url,比如/actuator/bus-refresh 等
server: port: 9050
啟動類 加上 @EnableDiscoveryClient 和 @EnableConfigServer 注解,然后啟動.
引入POM依賴
<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-client</artifactId> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
此時配置文件不再是 application.yml 了 , 應該改名成 bootstrap.yml , 配置如下:
spring: application: name: goods-service # cloud: config: discovery: enabled: true service-id: config-center profile: dev eureka: client: service-url: defaultZone: http://${eureka.instance.hostname}:9001/eureka/ instance: hostname: localhost
在遠端的Git項目創建一個名為 user-service-dev.yml 的文件.然后做測試,如果返回文件內容,說明配置成功
不過此時還只是靜態配置(即更改遠端Git的配置不會自動刷新其他項目的配置)
編寫測試類並打斷點查看配置是否生效
從日志也可以看出是否成功獲取到配置信息.
現在開始動態配置的實現(Spring Cloud Bus).
只需要在要更新的配置上面加上 @RefreshScope 注解即可.
@Data @Component @RefreshScope @ConfigurationProperties("main") public class MainValue { private String version; }
或者:
@RestController @RefreshScope public class ValueController { @Value("${main.version}") private String version; @GetMapping("/value") public String fetchValue() { return version; } }
然后使用POST請求 該URL : http://localhost:9050/actuator/bus-refresh
即可半自動刷新配置.
要實現修改完遠程git文件自動刷新配置的話,可以在git上面添加一個WebHooks, URL需要把本地的 http://localhost:9050/actuator/bus-refresh 映射成公網IP.可以使用第三方內網穿透實現.