前言
在微服務(wù)中眾多服務(wù)的配置必然會出現(xiàn)相同的配置,如果配置發(fā)生變化需要修改,一個(gè)個(gè)去修改然后重啟項(xiàng)目的方案是絕對不可取的羹蚣。而 SpringCloud Config 就是一個(gè)可以幫助你實(shí)現(xiàn)統(tǒng)一配置選擇之一袁翁。
如果你不懂 SpringCloud Config 環(huán)境搭建,那么該篇博客將會幫助到你秒拔,文中通過具體操作帶你了解 SpringCloud Config 環(huán)境搭建的入門操作乓土。
閱讀本文需要你熟悉 SpringBoot 項(xiàng)目的基本使用即可,還有一點(diǎn)需要注意的是在操作過程中盡量和我本地環(huán)境一致溯警,因?yàn)榄h(huán)境不一致可能會帶來一些問題趣苏。我本地環(huán)境如下:
- SpringBoot Version: 2.1.0.RELEASE
- SpringCloud Version: Greenwich.RELEASE
- Apache Maven Version: 3.6.0
- Java Version: 1.8.0_144
- IDEA:Spring Tools Suite (STS)
接下來就開始 SpringCloud Config 環(huán)境搭建操作介紹!
搭建 SpringCloud Config 環(huán)境
SpringCloud Config 環(huán)境搭建最小環(huán)境需要 3個(gè) SpringCloud 項(xiàng)目:一臺 Eureka Server 端梯轻、一臺 Config Server 端(也是Eureka Client 端)食磕、一臺普通服務(wù)端(即是 Config Client 也是 Eureka Client)。
關(guān)于Eureka環(huán)境的搭建請參考我的另一篇博客 帶你入門SpringCloud服務(wù)發(fā)現(xiàn) | Eurka搭建和使用
Config Server 端搭建
在 SpringBoot 項(xiàng)目中引入 spring-cloud-config-server 和 spring-cloud-starter-netflix-eureka-client 依賴喳挑,具體代碼如下:
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在 SpringBoot Application 上聲明 @EnableDiscoveryClient 和 @EnableConfigServer彬伦,具體代碼如下:
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
在 GitHub上創(chuàng)建私有倉庫
然后添加配置信息到 GitHub 上。
product.properties 配置可以添加一些公共的配置他會覆蓋到 product-dev.properties上
在 application.properties 添加配置信息
spring.application.name=CONFIGSERVER
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
spring.cloud.config.server.git.uri=https://github.com/zhuoqianmingyue/config-repo
spring.cloud.config.server.git.username=github帳號
spring.cloud.config.server.git.password=github帳號密碼
spring.cloud.config.server.git.basedir=config-repo/config-repo
- spring.application.name:服務(wù)的名稱
- eureka.client.service-url.defaultZone:Eureka Server 地址
- spring.cloud.config.server.git.uri:配置GitHub 私有倉庫 HTTP 克隆地址
- spring.cloud.config.server.git.username:配置你的 github帳號
- spring.cloud.config.server.git.password:配置你的github帳號密碼
- spring.cloud.config.server.git.basedir:克隆配置文件存儲地址
Config Server 端高可用只需要配置多臺Config Server注冊到Eureka 服務(wù)端即可伊诵。
測試
第一步啟動(dòng) Eurka Server 端(具體代碼請從我的GitHub項(xiàng)目獲取单绑,GitHub地址下面有介紹)
將 spring-cloud-config-eureka-service 進(jìn)行打包,通過 mvn clean package -Dmaven.test.skip=true
曹宴。
進(jìn)入 target 目錄 通過 java -jar 方式啟動(dòng)搂橙。
第二步啟動(dòng) Config Server 端
第三步最后訪問 Eurka Server 端,如下圖所示:
CONFIGSERVER 已經(jīng)注冊到 Eurka Server 服務(wù)端笛坦。
訪問 Config Server 端獲取配置信息区转,具體訪問地址:http://localhost:8080/product-dev.properties苔巨。訪問結(jié)果如下圖所示:
到這里 Config Server 端搭建介紹完畢!接下來開始 Config Client 端搭建介紹废离。
Config Client 端搭建(商品服務(wù))
在商品服務(wù) SpringBoot 項(xiàng)目中引入 spring-cloud-config-client 和 spring-cloud-starter-netflix-eureka-client 依賴侄泽。具體代碼如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-config-client</artifactId>
</dependency>
在 SpringBoot Application上聲明 @EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudConfigProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigProductServiceApplication.class, args);
}
}
在 src\main\resources 目錄下創(chuàng)建 bootstrap.properties, 具體代碼如下:
spring.cloud.config.discovery.service-id=CONFIGSERVER
spring.cloud.config.discovery.enabled=true
spring.cloud.config.profile=dev
spring.application.name=product
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
- spring.cloud.config.discovery.service-id:Config Server 服務(wù)名
- spring.cloud.config.discovery.enabled:是否開啟配置發(fā)現(xiàn)
- spring.cloud.config.profile:啟用那個(gè)后綴的配置文件
- eureka.client.service-url.defaultZone:Eureka Server 地址
- spring.application.name:服務(wù)的名稱
連接接Config Server配置 spring.cloud.config.xx 和 eureka Server 端配置eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ 一定要配置到bootstrap.properties中蜻韭,否則根本獲取不到 Config Server 的配置信息悼尾。
測試
第一步啟動(dòng) Eurka Server端 和 Config Server 端。
第二步啟動(dòng) Config Client (商品服務(wù)) 端
第三步訪問 Eurka Server 端肖方,如下圖所示:
PRODUCT 已經(jīng)注冊到 Eurka Server 服務(wù)端诀豁。
第四步:編寫獲取 Config Server 上配置信息的 Controller,具體代碼如下:
獲取的就是紅色框的 env 配置項(xiàng)的值窥妇。
@RestController
public class EvnController {
@Value("${env}")
private String env;
@RequestMapping("/env")
public String evn() {
return this.env;
}
}
游覽器訪問 localhost:8763/product/env 進(jìn)行測試舷胜,具體結(jié)果如下:
小結(jié)
SpringCloud Config 執(zhí)行流程是通用的配置添加配置倉庫中(默認(rèn)使用Git),在由 Config Server 讀取配置倉庫配置并對外提供接口活翩。其他服務(wù)(Config Client)可以通過調(diào)用Config Server 提供接口來獲取配置信息烹骨。
搭建過程也并不復(fù)雜還是SpringCloud 添加starter 依賴、添加EnableXX 注解材泄、最后在添加相關(guān)配置即可沮焕。沒有操作的最好操作一篇哈!
代碼示例
如果你按照上述方式搭建并未成功拉宗,可以參考我在GitHub 項(xiàng)目 spring-cloud-get-started 倉庫中模塊名為:
- spring-cloud-config-eureka-service
- spring-cloud-config-server
- spring-cloud-config-product-service
進(jìn)行對比查看是否配置有誤峦树。
spring-cloud-get-started 項(xiàng)目地址:https://github.com/zhuoqianmingyue/spring-cloud-get-started