Spring Cloud學習--配置中心(Config)
Spring Cloud學習--配置中心(Config)
- 一 Spring Cloud Config簡介
- 二 編寫 Config Server
- 三 編寫Config Client
- 四 使用refresh端點手動刷新配置
- 五 Spring Config Server與Eurelka配合使用
- 六 Config Server的高可用
一、 Spring Cloud Config簡介
微服務要實現集中管理微服務配置、不同環境不同配置、運行期間也可動態調整、配置修改後可以自動更新的需求,Spring Cloud Config同時滿足了以上要求。Spring Cloud Config 分為Config Server和Config Client兩部分,是一個可以橫向擴展,集中式的配置服務器, 默認使用Git存儲配置內容。
Spring Cloud Config 原理圖如圖所示:
二、 編寫 Config Server
1.先在github上新建一個倉庫,添加配置文件。
2.新建一個spring boot 項目,pom.xml中添加如下依賴:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
3.啟動類上添加@EnableConfigServer註解,表示這個類是一個Config Server
@SpringBootApplication
@EnableConfigServer
public class SpringConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringConfigServerApplication.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4.配置application.yml文件
server:
port: 8001
spring:
cloud:
config:
server:
git:
uri: https://github.com/songxiansen521/spring-cloud-config-repo.git
username: ****your git name****
password: ****your git pw****
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
5.啟動項目,訪問http://localhost:8001/microservice-foo/dev,看到如下頁面,Config Server 配置成功。
6.關於Config Server的端點。
可以使用Config Server的端點獲取配置文件的內容,映射規則如下:
/{application}/{profile}[/{label}]
- 1
本例: http://localhost:8001/microservice-foo/dev
/{application}-{profile}.yml
/{application}-{profile}.properties
- 1
- 2
本例:http://localhost:8001/microservice-foo.properties
/{label}/{application}-{profile}.yml
/{label}/{application}-{profile}.properties
- 1
- 2
本例: http://localhost:8001/config-label-v1.0/microservice-foo-dev.properties
三 編寫Config Client
1.新建spring boot 項目,添加如下依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
2.編寫配置文件application.yml
server.port=8002
- 1
3.編寫配置文件bootstrap.yml。配置在bootstrap.xml中的屬性有更高的優先級,默認情況下不會被本地覆蓋。
spring:
application:
#對應config server中配置文件的{application}
name: microservice-foo
cloud:
config:
#訪問config server的地址
uri: http://localhost:8001
#對應config server中配置文件的{profile}
profile: dev
#對應config server中配置文件的{label}
label: master
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
4.添加Controller類,
package com.swc.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by chao on 2017-11-8.
*/
@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/getProfile")
public String hello(){
return this.profile;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
4.啟動應用,訪問http://localhost:8002/getProfile,得到如下頁面,說明Client能成功通過Config Server獲取Git倉庫中對應環境的配置。
四、 使用/refresh端點手動刷新配置
很多時候,需要在運行期間動態調整配置,可以使用/refresh 實現微服務配置的刷新。
在上面Config Client項目中,我已經添加了一個依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 1
- 2
- 3
- 4
在controller上添加@RefereshScope,該註解會在配置更改時得到特殊的處理。
package com.swc.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by chao on 2017-11-8.
*/
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/getProfile")
public String hello(){
return this.profile;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
3.啟動項目,http://localhost:8002/getProfile 訪問修改Git倉庫中配置文件內容,再次訪問,發現結果沒變化。
這時,就需要手動刷新:以POST請求,訪問http://localhost:8001/refresh
第三次訪問,結果如圖所示,配置刷新
五、 Spring Config Server與Eurelka配合使用
將Config Server 和 Config Client都註冊到Eureka Server上。
修改 Config Client
spring:
application:
#對應config server中配置文件的{application}
name: microservice-foo
cloud:
config:
#訪問config server的地址
#uri: http://localhost:8001
#對應config server中配置文件的{profile}
profile: dev
#對應config server中配置文件的{label}
label: master
discovery:
#表示使用服務發現組件中提供的Config Server,默認是false
#開啟通過服務發現組件訪問Config Server的功能
enabled: true
#指定Config Server在服務發現組件中的serviceId 默認是configserver
service-id: microservice-config-server-eureka
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
修改Config Server
server:
port: 8001
spring:
appliation:
name: microservice-config-server-eureka
cloud:
config:
server:
git:
uri: https://github.com/songxiansen521/spring-cloud-config-repo.git
username: ***your git name***
password: ***your git pw***
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
啟動Eureka Server,Config Server和Config Client三個項目, 可獲取到git倉庫中配置文件的內容。
六、 Config Server的高可用
Config Server的高可用可以借助負載均衡實現,其原理圖如下:
Config Server的高可用也可借助Eureka實現。
Spring Cloud學習--配置中心(Config)