SpringCloud使用集中配置組件Config規(guī)避信息泄露
Spring Cloud Config簡介
在分布式系統(tǒng)中,由于應用被拆分成數(shù)量巨多的小服務,另外應用也部署在不同的環(huán)境之中,如dev、int、uat、prod等,各個環(huán)境的配置不盡相同,為了方便配置文件統(tǒng)一管理,所以需要分布式配置中心組件。
配置文件統(tǒng)一管理之后,各個環(huán)境只能獲取對應環(huán)境的配置信息,開發(fā)人員也只能獲取到開發(fā)環(huán)境的配置信息,就能在一定程度上避免敏感信息的泄露。
Spring Cloud Config作為分布式配置中心組件 ,包括Config 服務端,和Config 客戶端。
- Config Server是一個可橫向擴展、集中式的配置服務器,它用于集中管理應用程序各個環(huán)境下的配置,默認使用Git存儲配置文件內(nèi)容,也可以使用SVN存儲,或者是本地文件存儲。
- Config Client是Config Server的客戶端,用于操作存儲在Config Server中的配置內(nèi)容。微服務在啟動時會請求Config Server獲取配置文件的內(nèi)容,請求到后再啟動容器。
Config實戰(zhàn)
1、創(chuàng)建項目config服務端
創(chuàng)建子模塊config-server,pom.xml引入eureka-client 和config-server的依賴
<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>
2、創(chuàng)建配置文件
新建config-server自身的配置文件application.yml
server:
port: 8005
spring:
application:
name: config-server
profiles:
active: native #使用本地文件
cloud:
config:
server:
native:
search-locations: classpath:/repo #本地配置倉庫地址
# git:
# uri: https://gitee.com/xxxx/xxxxx.git
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
這里我們以使用本地配置倉庫地址為例,spring.profiles.active設置為native,配置倉庫路徑為repo文件夾,所以我們在resources文件下創(chuàng)建repo文件夾,并創(chuàng)建新的一個configclient-dev.yml的文件,內(nèi)容如下:
server:
port: 8007eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
feign:
hystrix:
enabled: true
logging:
pattern:
console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
3、新建啟動類
@EnableConfigServer //開啟配置服務 @EnableEurekaClient @SpringBootApplication public class ConfitServerApplication { public static void main(String[] args) { SpringApplication.run(ConfitServerApplication.class, args); } }
注意增加@EnableConfigServer注解,表示這是個配置中心服務端。
4、創(chuàng)建配置中心客戶端
服務端開發(fā)完成后,我們再新建一個客戶端config-client項目,引入如下依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
與服務端不同的是,客戶端的配置文件我們創(chuàng)建bootstrap.yml文件
spring:
cloud:
config:
name: configclient
profile: dev
label: master
discovery:
enabled: true
service‐id: config-servereureka:
client:
service‐url:
defaultZone: http://localhost:8001/eureka/
注意spring.cloud.config.name與服務端中的文件名稱對應,spring.cloud.config.profile與文件名-后面的環(huán)境代碼對應,配置文件的命名規(guī)則是 {application}/{profile}[/{label}] 。
當 Config Client 去訪問 Config Server 時,spring.cloud.config.name 、spring.cloud.config.profile 以及 、spring.cloud.config.label 的值分別對應上面三個占位符,如果配置了spring.cloud.config.name,那么就取spring.cloud.config.name,如果沒有配置就取 spring.application.name,通過靈活使用 {application} 、{profile} 、{label} 三個占位符,就可以來動態(tài)地控制 client 從 server 所訪問的倉庫!
然后編寫客戶端啟動類:
@EnableDiscoveryClient @EnableFeignClients @EnableEurekaClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
5、驗證
我們分別啟動registry項目以及config-server,config-client兩個服務,這時,就會發(fā)現(xiàn),config-client服務拉取了config-server中對應的配置文件。
總結
這篇文章我們介紹了一下 Spring Cloud Config 的一個基本使用,包括 Spring Cloud Config Server 和 Spring Cloud Config Client 的項目搭建。通過環(huán)境的配置隔離,避免了敏感配置信息的泄露。
有人可能就說了,我本地把拉取dev的配置改成拉取prod不一樣也能拿到其他環(huán)境的信息嗎?下一篇文章我們介紹如何通過 Config Server 的安全管理、配置文件的加密等機制真正做到這一點,一起期待吧!
到此這篇關于SpringCloud使用集中配置組件Config規(guī)避信息泄露的文章就介紹到這了,更多相關SpringCloud集中配置組件Config內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!