javascript
Spring Cloud学习系列第六篇【分布式配置中心】
今天來學習如何使用Spring Cloud Config在微服務中搭建一個分布式配置中心,使用了分布式配置中心之后,我們就可以通過它來管理應用資源與業務資源配置內容。Spring Cloud Config分為服務端和客戶端兩部分。其中服務端用于管理我們的配置內容,它從配置源獲取配置內容并緩存在本地,然后提供給客戶端,它的配置源可以是從Git、SVN這些版本管理工具中獲取,也可以從本地中獲取。而客戶端集成在我們每個微服務應用中,當微服務啟動的時候客戶端會自動從服務端獲取配置信息。Spring Cloud Config還提供了很多配置,使得我們可以對配置內容進行一些加密/解密操作、安全保護、高可用、動態刷新等靈活的功能。
今天學習目標:
1.以Git作為配置源搭建分布式配置中心
2.每個應用以應用名稱單獨一個配置目錄,并且根據環境名請求獲取配置信息
3.具備動態刷新能力
4.整合Spring Security實現對配置中心的安全訪問
一、Git目錄結構
首先在Git創建一個config-repo目錄用于存儲我們配置內容,接著創建子目錄config-client,該目錄專門存儲應用名為config-client的配置,最后添加幾個不同profile的配置文件,每個配置文件有一個prop.name屬性,該屬性值為pumpkin-{profile}.properties,用于驗證我們之后步驟是否成功。
eg:pumpkin-dev.properties文件內容為 prop.name=pumpkin-dev.properties
二、 服務端搭建
服務端搭建涉及文件有pom.xml、Application.java、application.properties。當然有賴于Spring Cloud簡易性設計,所有服務端的功能都集中在application.properties配置文件里。接下來我們創建我們的服務端了,應用名稱為ConfigService.
?
pom.xml主要配置
spring-boot-starter-security是為了集成Spring Security做安全認證需要的,下面在會講到如何在application.properties配置它的安全認證賬號,當配置好之后,客戶端獲取配置時也將需要帶上該安全認證賬號,否則是獲取不到的。
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><!--增加安全保護--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>?
application.properties
spring.application.name=config-server server.port=7003#spring.cloud.config.server.git.uri=https://gitee.com/pumpkingg/Spring-Cloud-Study.git spring.cloud.config.server.git.uri=git@gitee.com:pumpkingg/Spring-Cloud-Study.git spring.cloud.config.server.git.searchPaths=config-repo/{application} #spring.cloud.config.server.git.username=<your username> #spring.cloud.config.server.git.password=<your password>#增加spring security安全保護,客戶端訪問需配置該賬號 security.user.name=pumpkingg security.user.password=pumpkingg?
逐一對配置內容講解下:
spring.cloud.config.server.git.uri設置遠程Git地址,這里我是使用SSH方式訪問的,如何需要HTTPS方式,uri就需要改成https開頭了,并且配置好Git賬號、密碼。在這里我遇到了第一個坑,采用SSH方式直接把uri改成git開頭是不行的(前提是你已經配過ssh key到你Git倉庫里了),先看個官網的介紹。
If you do not use HTTPS and user credentials, SSH should also work out of the box when you store keys in the default directories (~/.ssh) and the URI points to an SSH location, such as git@github.com:configuration/cloud-configuration. It is important that an entry for the Git server be present in the ~/.ssh/known_hosts file and that it is in ssh-rsa format. Other formats (such as ecdsa-sha2-nistp256) are not supported. To avoid surprises, you should ensure that only one entry is present in the known_hosts file for the Git server and that it matches the URL you provided to the config server. If you use a hostname in the URL, you want to have exactly that (not the IP) in the known_hosts file. The repository is accessed by using JGit, so any documentation you find on that should be applicable. HTTPS proxy settings can be set in ~/.git/config or (in the same way as for any other JVM process) with system properties (-Dhttps.proxyHost and -Dhttps.proxyPort).?如果看不懂也沒關系,萬能的網友已經有踩過坑的了,貼下這個網友正確操作吧。https://javablog.net/page/221/spring-cloud-config-read-the-ssh-protocol-git.html
https可以使用,ssh不能訪問 ssh協議下springboot訪問gitlab報錯Spring Boot Config Server git@xxx.git:UnknownHostKey: gitlab 先在本地修改 ~/.ssh/configHost xxxxUser zhushaolongIdentityFile ~/.ssh/id_rsaHashKnownHosts noHostKeyAlgorithms ssh-rsa,ssh-dss 再 ssh xxxx 連接一次gitlab所在的服務.為了生成known_hosts或者不校驗Host xxxxUser zhushaolongIdentityFile ~/.ssh/id_rsaStrictHostKeyChecking no? spring.cloud.config.server.git.searchPaths 配置Git倉庫的搜索路徑,也就是指定配置目錄的意思。這里我配置的是config-repo/{application},因為我的配置內容是在Git倉庫config-repo目錄下的,而{application}是個占位符,它的取值是我們的應用名稱,這里要對應一個應用一個目錄,所以這樣配置。
security.user.name=pumpkingg和security.user.password=pumpkingg就是配置Spring Security認證賬號的。
Application.java
package com.pumpkin;import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer @SpringBootApplication public class Application {public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);}}
三、配置規則介紹
Spring Cloud Config是通過一定的配置規則是Git倉庫找到我們需要的配置文件的。當我們訪問服務端的URL與Git配置文件存在如下關系。其中application為應用名稱、profile是你配置環境、label代表Git分支。根據上面我們服務端的配置,當我們訪問http://localhost:7003/config-client/default和http://localhost:7003/config-client/dev就能得到對應Git倉庫conf-repo/config-client目錄下application.properties和application-dev.properties是配置內容了
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties四、客戶端搭建
客戶端的搭建和服務端差不多,按照上面的套路繼續看。
pom.xml
spring-boot-starter-actuator這個依賴包含/refresh端點的實現。通過該端點,每次Git上的配置有更新的時候,我們就可以通過/refresh端點令客戶端獲取到新的配置值。對的,你沒看錯,是每次你都需要這么操作才能獲取到新的配置值,如果想做到自動化一定的話可以通過Git的Hook技術,每次Git有更新就主動觸發對應客戶端的/refresh端點,除此之外還可以通過Spring Cloud Bus來處理得更加合理的,關于Spring Cloud Bus內容下周再學習下。
<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.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>?
application.properities
這里我把{application}配置成了config-client、{profile}為dev、{lable}我沒有進行配置,默認是master。如無意外的話我們啟動客戶端就能把屬性prop.name=xxx替換成Git倉庫的prop.name=pumpkin-dev.properties了
spring.application.name=config-clientspring.cloud.config.profile=dev spring.cloud.config.uri=http://localhost:7003/ #服務端添加安全保護后,需添加對應檢驗賬號 spring.cloud.config.username=pumpkingg spring.cloud.config.password=pumpkinggserver.port=7004 prop.name=xxx?
TestController.java
提供個http接口,驗證我們的結果
package com.pumpkin.web;import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RefreshScope @RestController public class TestController {@Value("${prop.name}")private String propName;@RequestMapping("/config-client")public String test() {return this.propName;} }?
測試
五、總結
Spring Cloud Config搭建完成,并且能達到今天的學習目標要求了,更多的內容就留著之后去學習了,相信這套搭起來之后,后面的學習應該是比較順暢的,加油~去吃感冒藥了。。。
六、參考資料
Spring Cloud微服務實戰-翟永超。本系列的學習都是參考該書籍學習的,同時源碼使用的Spring Boot和Spring Cloud的版本也與該書保持一致。
七、源碼
碼云地址:git@gitee.com:pumpkingg/Spring-Cloud-Study.git 該篇隨筆對應的代碼是master分支下命名為blog5的Tag
轉載于:https://www.cnblogs.com/yipaihushuo/p/9314754.html
總結
以上是生活随笔為你收集整理的Spring Cloud学习系列第六篇【分布式配置中心】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷 P3539 [POI2012]RO
- 下一篇: 正则表达式手册