微服務架構 基礎(五)
持續更新…
繼續前面的基礎四工程進行擴展
分布式配置中心
微服務意味著要將單體應用中的業務拆分成一個個子服務,每個服務的粒度相對較小,因此系統中會出現大量的服務,由于每個服務都需要必要的配置資訊才能運行,所以需要一套集中式、動態的配置管理設施是必不可少的,
SpringCloud Config為微服務架構中的微服務提供集中化的外部配置支持,配置服務器為各個不同微服務應用的所有環境提供了一個中心化的外部配置,
配置中心具有什么作用?
- 集中管理組態檔
- 不同環境不同配置,動態化配置更新,分環境部署比如dev/test/prod/bete/release
- 運行期間動態調整配置,不再需要在每個服務部署的機器上撰寫組態檔,服務會向配置中心統一獲取配置自己的資訊
- 但配置發生變動時,服務不需要重啟即可感知到配置的變化并應用新的配置
- 將配置資訊以Rest介面的形式暴露
大致流程:

服務端搭建
新建配置中心服務端子模塊(config-center-10002):

添加主要依賴:
<!-- eureka客戶端依賴 -->
<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-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
添加application.yml檔案:
server:
port: 10002
spring:
application:
name: config-center-10002
cloud:
config:
server:
git:
uri: https://gitee.com/yiyexingchen/spring-cloud-config.git
default-label: master
search-paths:
- SpringCloud[這里是代碼倉庫目錄]
username: ***[這里是gitee賬號]
password: ***[這里是gitee密碼]
eureka:
client:
service-url:
defaultZone: http://eureka7001.cn:7001/eureka,http://eureka7002.cn:7002/eureka
register-with-eureka: true # 注冊到Eureka中心
fetch-registry: true # 可以獲取注冊中心串列資訊
主啟動類:
package cn.wu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterApplication10002 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication10002.class,args);
}
}
首先啟動Eureka服務器(埠分別為7001和7002)測驗結果如下(此時可以直接訪問到相應倉庫下的特定檔案,這里git基礎不再過多贅述):

通過以上的搭建程序,可以漸漸理解隨時通過git管理yml檔案達到所謂的集中式、動態化的配置管理…
配置讀取匹配規則
application:服務名,label:分支名,profile:環境名
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
客戶端搭建
新建配置中心客戶端子模塊(config-client-11001):

添加核心依賴:
<!-- eureka客戶端依賴 -->
<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-config</artifactId>
</dependency>
新建bootstrap.yml(application.yml是用戶級的資源配配置項,而bootstrap.yml是系統級資源配置項,優先等級更高):
server:
port: 11001
spring:
application:
name: config-client-11001
cloud:
config:
label: master # 分支名稱 會自動拼接
name: config # 組態檔名稱 會自動拼接
profile: dev # 開發環境的后綴 會自動拼接
uri: http://localhost:10002 # 配置中心地址
eureka:
client:
register-with-eureka: true # 注冊
fetch-registry: true # 獲取注冊串列資訊
service-url:
defaultZone: http://eureka7001.cn:7001/eureka,http://eureka7002.cn:7002/eureka
主啟動類:
package cn.wu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication11001 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication11001.class,args);
}
}
控制層:
package cn.wu.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo(){
return configInfo;
}
}
首先動兩個Eureka服務注冊中心(埠分別為7001/7002),然后啟動服務配置中心服務端(埠為10002),最后啟動配置中心客戶端,結果測驗:

成功實作了客戶端11001訪問配置中心的服務端10002通過Gitee獲取配置資訊…
但是,簡單分析一下,可以發現客戶端11001的配置資訊是運行一次只加載一次的,所以再次修改Gitee上的倉庫中的檔案資訊,是不會動態重繪的…
客戶端動態重繪
添加監控依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
bootstrap.yml檔案添加:
# 暴露檢控端點
management:
endpoints:
web:
exposure:
include: "*"
修改主啟動類:
@SpringBootApplication
@EnableEurekaClient
@RefreshScope // 添加熱加載注解
public class ConfigClientApplication11001 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication11001.class,args);
}
}
修改Gitee中的yml檔案,再次訪問相應URL…

測驗結果為(在這這之前應該利用POSt請求激活一下該埠… ):


從以上還是能發現一些問題,即當有多個微服務需要重繪時通過腳本也能是實作,但是還是不夠完善,需要進一步優化,這個問題也就是下一篇文章需要介紹的:SpringCloud Bus 訊息總線實作…
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/355291.html
標籤:其他
