好久沒寫了,首先依舊是相關(guān)資料以及注意事項(xiàng):
- 工程GitHub
- Config遠(yuǎn)程倉庫地址
- 需要配置Eureka注冊中心,方法見SpringBoot-Eureka的配置
- 環(huán)境:SpringBoot 2.0.4.RELEASE + SpringCloud Finchley.SR1
簡介:
分布式系統(tǒng)中,為了方便服務(wù)配置文件統(tǒng)一管理,實(shí)時更新,所以需要分布式配置中心組件。在Spring Cloud中,有分布式配置中心組件springCloud Config ,它支持從遠(yuǎn)程Git倉庫中讀取配置文件并存放到本地Git倉庫。接下來我們來看一下服務(wù)端和客戶端分別應(yīng)該如何配置。
服務(wù)端
一.Maven配置
在Maven中添加eureka-client,spring-cloud-config-server
<dependencies>
<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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二.開啟配置
還是老套路,添加@EnableConfigServer,@EnableDiscoveryClient注解來分別開啟eureka客戶端以及配置中心的服務(wù)端。
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
三.配置文件
主要就是三個設(shè)置
- 設(shè)置服務(wù)的名稱
- 設(shè)置遠(yuǎn)程倉庫的地址用戶名密碼
- 設(shè)置注冊中心地址
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/BzCoder/config-repo
username:
password:
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8761/eureka
四.創(chuàng)建配置文件并訪問
我們在遠(yuǎn)程倉庫中創(chuàng)建了三個對應(yīng)不同環(huán)境的配置文件order-dev.yml,order-test.yml,order-prod.yml,假如在不同環(huán)境下有很多共用的配置,我們可以再創(chuàng)建一個order.yml用來存放共同配置,springcloud-config取配置時,會將你的對應(yīng)環(huán)境配置與order.yml合并后再返回。在遠(yuǎn)程倉庫文件配置完畢后,我們就可以直接訪問http://localhost:8080/order-dev.yml來獲取配置信息。當(dāng)然我們可以更改文件后綴名來修改配置文件的展現(xiàn)方式,如.json,.properites。
配置文件的路徑規(guī)則:
/{label}/{name}-{profiles}.{type}
- label 分支名稱 如:master dev ,不寫就是master。
- name 配置文件名稱
- profiles 環(huán)境名稱,不可省略,假如我們的倉庫中配置文件命名沒有環(huán)境名稱,可以profile可以寫為-a
在啟動后我們可以看到日志
Adding property source: file:/C:/Users/Administrator/AppData/Local/Temp/config-repo-9115656602637453229/order-dev.yml
這樣就把遠(yuǎn)程倉庫的配置文件保存在本地路徑Git了。假如需要設(shè)置保存到指定路徑的話,可以在配置文件中加入basedir。
server:
git:
uri: https://github.com/BzCoder/config-repo
username:
password:
basedir: D:/Config/basedir
至此服務(wù)端就配置完畢了,當(dāng)然如果你需要保持他高可用性,可以配置ConfigSever集群,只需要配置到不同地址即可。
客戶端
一.Maven配置
在Maven中添加eureka-client,config-server
<dependencies>
<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.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>
二.開啟服務(wù)發(fā)現(xiàn)
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
三.配置文件
在配置文件上就比較講究了,我們需要創(chuàng)建一個bootstrap.yml.bootstrap的意思是引導(dǎo)程序,我們需要將Eureka的配置信息寫到bootstrap中。這里也提一下配置加載的順序是:
- bootstrap 2.遠(yuǎn)程config的配置 3.application
bootstrap.yml文件
spring:
application:
name: client
cloud:
config:
discovery:
enabled: true
service-id: CONFIG
profile: dev
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8762/eureka/
配置文件中name對應(yīng)配置文件的名稱,profile對應(yīng)環(huán)境,service-id對應(yīng)我們配置中心的服務(wù)名稱。
四.測試
最后我們寫一個測試接口,查看是否能正確的讀取配置中心的數(shù)據(jù):
@RestController
public class ConfigController {
@Value("${env}")
private String env;
@GetMapping("/env")
public String getEnv()
{
return env;
}

到此對于SpringCloud Config的初步學(xué)習(xí)就到這里了