一,構建配置中心
1.在pom.xml文件中添加相關依賴
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
</dependencies>
2.在SpringBoot程序主類上添加@EnableConfigServer注解, 開啟SpringCloud Config的服務端功能
@SpringBootApplication @EnableConfigServer public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3.在application.properties中添加相關配置信息
spring.application.name=config-server server.port=7001
#Git倉庫位置 spring.cloud.config.server.git.uri=https://gitee.com/*****/config-properties.git #指定Git倉庫路徑下的文件夾 spring.cloud.config.server.git.search-paths=myconfig spring.cloud.config.server.git.username=username spring.cloud.config.server.git.password=password #指定Git分支 spring.cloud.config.label=dev
4.URL調用查看配置是否生效
(1) /{application}/{profile}[/{label}]
(2) /{application}-{profile}.yml
上面的url會映射{application}-{profile}.properties對應的配置文件,其中{label}對應Git不同的分支, 默認為master.
查看Git項目中的application-myproject.properties文件中的內容, 調用http://localhost:7001/application/myproject或http://localhost:7001/application/myproject/master
{ "name": "application", "profiles": ["myproject"], "label": null, "version": "", "state": null, "propertySources": [{ "name": "https://gitee.com/****/config-properties.git/master/application-myproject.properties", "source": { "server.servlet.context-path": "/myproject", "server.port": "7002", "spring.application.name": "myproject", "spring.cloud.consul.host": "localhost", "spring.cloud.consul.port": "8500", "spring.cloud.consul.discovery.health-check-path": "${server.servlet.context-path}/healthcheck", "spring.cloud.consul.discovery.register": "true" } }] }
配置服務器在從Git中獲取配置信息后, 會存儲一份在 config-server 的文件系統中, 實質上 config-server 是通過 git clone 命令將配置內容復制了一份在本地存儲, 然后讀取這些內容並返回給微服務應用進行加載.
config-server 通過 Git 在本地倉庫暫存,可以有效防止 Git 倉庫出現故障而引起無法加載配置信息的情況.
二.客戶端配置映射
1.在pom.xml文件中添加相關依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
2.創建SpringBoot的應用主類
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3.創建boostrap.properties配置,來指定獲取配置文件的config-server位置
spring.cloud.config.enabled=true #配置中心config-server的地址 spring.cloud.config.uri=http://127.0.0.1:23001/ #對應配置文件規則中的{application}部分 spring.cloud.config.name=application #對應配置文件規則中的{profile}部分 spring.cloud.config.profile=order #Git分支 spring.cloud.config.label=dev spring.cloud.config.username=q741622318@163.com spring.cloud.config.password=bananas66424
SpringBoot對配置文件的加載順序, 本應用jar報之外的配置文件加載會優先於應用jar包內的配置內容, 而通過bootstrap.properties對config-server的配置, 使得該應用從config-server中獲取一些外部配置信息, 這些信息的優先級比本地的內容要高, 從而實現外部化配置.
三.服務端詳解
(1)Git倉庫的Hook功能可以幫助我們實時監控配置內容的修改.
(2)占位符配置URI
{application}, {profile}, {label}這些占位符除了用於標識配置文件的規則之外, 還可以用於 Config Server 中對 Git 倉庫地址的URI配置.
比如通過{application}占位符來實現同時匹配多個不同服務的配置倉庫
spring.cloud.config.server.git.uri=https://gitee.com/*****/{application}-properties.git
又比如通過{application}占位符實現一個應用一個目錄的效果
spring.cloud.config.server.git.search-paths={application}
(3)屬性覆蓋
Config Server 有"屬性覆蓋"的特性, 它可以讓開發人員為所有的應用提供配置屬性, 只需要通過 spring.cloud.config.server.overrides 屬性來設置鍵值對的參數, 這些參數會以 Map 的方式加載到客戶端的配置中. 比如:
spring.cloud.config.server.overrides.name=userstring
(4)安全保護
由於我們的微服務應用和配置中心都構建於 Spring Boot 基礎上,所以與 Spring Security 結合使用會更加方便.
在 pom.xml 中引入依賴后,不需要任何其他改動就能實現對配置中心訪問的安全保護.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId>
</dependency>
在配置文件中指定用戶和密碼, 比如:
security.user.name=user security.user.password=37CC5635-559b-4e6f-b633-7e932b813f73
由於我們已經為 config-server 設置了安全保護, 需要在客戶端中加入安全信息才可以正常訪問服務端.
spring.cloud.config.username=user
spring.cloud.config.password=37CC5635-559b-4e6f-b633-7e932b813f73
(5)加密解密
使用加密解密功能時, 需要在配置中心的運行環境中安裝不限長度的JCE版本. 我們可以從Oracle 的官方網站下載, 它是一個壓縮包,解壓后可以看到下面三個文件:
README.TXT
local_policy.jar
US_export_policy.jar
之后將 local_policy.jar 和 US_export_policy.jar 兩個文件復制到$JAVA_HOME/jre/lib/security 目錄下.
再配置加密數據, 如下:
spring.datasource.username=user
spring.datasource.password={cipher}dba650baa81d78bd08799d8d4429de499bd4c2053c05f029e7cfbf143695f5b
通過在屬性值前使用{cipher}前綴來標注該內容為加密值, 當微服務客戶端加載配置時, 配置中心會自動為帶有{cipher}前綴的值進行解密.