配置中心概述
對於傳統的單體應用而言,常使用配置文件來管理所有配置,比如SpringBoot的application.yml文件,
但是在微服務架構中全部手動修改的話很麻煩而且不易維護。微服務的配置管理一般有以下需求:
集中配置管理,一個微服務架構中可能有成百上千個微服務,所以集中配置管理是很重要的。
不同環境不同配置,比如數據源配置在不同環境(開發,生產,測試)中是不同的。
運行期間可動態調整。例如,可根據各個微服務的負載情況,動態調整數據源連接池大小等
配置修改后可自動更新。如配置內容發生變化,微服務可以自動更新配置
綜上所述對於微服務架構而言,一套統一的,通用的管理配置機制是不可缺少的總要組成部分。常見的
做法就是通過配置服務器進行管理。
常見配置中心
Spring Cloud Config為分布式系統中的外部配置提供服務器和客戶端支持。
Apollo(阿波羅)是攜程框架部門研發的分布式配置中心,能夠集中化管理應用不同環境、不同集群的
配置,配置修改后能夠實時推送到應用端,並且具備規范的權限、流程治理等特性,適用於微服務配置
管理場景。
Disconf 專注於各種「分布式系統配置管理」的「通用組件」和「通用平台」, 提供統一的「配置管理
服務」包括 百度、滴滴出行、銀聯、網易、拉勾網、蘇寧易購、順豐科技 等知名互聯網公司正在使用!
「disconf」在「2015 年度新增開源軟件排名 TOP 100(OSC開源中國提供)」中排名第16強。
Spring Cloud Config簡介
Spring Cloud Config項目是一個解決分布式系統的配置管理方案。它包含了Client和Server兩個部分,
server提供配置文件的存儲、以接口的形式將配置文件的內容提供出去,client通過接口獲取數據、並
依據此數據初始化自己的應用。
Spring Cloud Config為分布式系統中的外部配置提供服務器和客戶端支持。使用Config Server,您可
以為所有環境中的應用程序管理其外部屬性。它非常適合spring應用,也可以使用在其他語言的應用
上。隨着應用程序通過從開發到測試和生產的部署流程,您可以管理這些環境之間的配置,並確定應用
程序具有遷移時需要運行的一切。服務器存儲后端的默認實現使用git,因此它輕松支持標簽版本的配置
環境,以及可以訪問用於管理內容的各種工具。
Spring Cloud Config服務端特性:
HTTP,為外部配置提供基於資源的API(鍵值對,或者等價的YAML內容)
屬性值的加密和解密(對稱加密和非對稱加密)
通過使用@EnableConfigServer在Spring boot應用中非常簡單的嵌入。
Config客戶端的特性(特指Spring應用)
綁定Config服務端,並使用遠程的屬性源初始化Spring環境。
屬性值的加密和解密(對稱加密和非對稱加密)
一 開始配置config服務
-
config-server
-
1 <dependency> 2 <groupId>org.springframework.cloud</groupId> 3 <artifactId>spring-cloud-config-server</artifactId> 4 </dependency> 5 6 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 9 </dependency>
-
application.yml
1 #服務名稱 2 spring: 3 application: 4 name: config-server 5 cloud: 6 config: 7 server: 8 git: 9 uri: http://192.168.180.112/root/test.git 10 username: root 11 password: 19920220ljyp 12 default-label: master 13 14 15 #服務的端口號 16 server: 17 port: 9100 18 19 20 #指定注冊中心地址 21 eureka: 22 client: 23 serviceUrl: 24 defaultZone: http://localhost:8761/eureka/
-
啟動類
1 @SpringBootApplication 2 @EnableConfigServer 3 public class ConfigServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigServerApplication.class, args); 7 } 8 9 }
-
-
在gitlab上配置服務的yml文件
注:其實可以指定分支,也可以指定環境,但是建議用分支去區分,因為環境的話,會可能存在沖突問題。
product-service.yml
1 # eureka: 2 # client: 3 # serviceUrl: 4 # defaultZone: http://localhost:8761/eureka/ 5 # instance: 6 # instance-id: product-service8080 7 # prefer-ip-address: true 8 9 server: 10 port: 8771 11 spring: 12 application: 13 name: product-service 14 zipkin: 15 base-url: http://192.168.180.113:9411/ 16 sleuth: 17 sampler: 18 probability: 1 19 20 info: 21 app.name: product-servic 22 company.name: www.topcheer.com
order-service.yml
1 server: 2 port: 8781 3 4 5 #指定注冊中心地址 6 eureka: 7 client: 8 serviceUrl: 9 defaultZone: http://localhost:8761/eureka/ 10 11 #服務的名稱 12 spring: 13 application: 14 name: order-service 15 redis: 16 port: 6379 17 host: 192.168.180.113 18 timeout: 2000 19 zipkin: 20 base-url: http://192.168.180.113:9411/ 21 sleuth: 22 sampler: 23 probability: 1 24 25 ###配置請求超時時間 26 hystrix: 27 command: 28 default: 29 execution: 30 isolation: 31 thread: 32 timeoutInMilliseconds: 5000 33 ribbon: 34 ##指的是建立連接所用的時間,適用於網絡狀況正常的情況下,兩端連接所用的時間。 35 ReadTimeout: 2000 36 ##指的是建立連接后從服務器讀取到可用資源所用的時間。 37 ConnectTimeout: 3000 38 feign: 39 hystrix: 40 enabled: true 41 management: 42 endpoints: 43 web: 44 exposure: 45 include: "*" 46 47 48 #自定義負載均衡策略 49 #product-service: 50 # ribbon: 51 # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
api-gateway.yml
1 server: 2 port: 9001 3 4 # spring: 5 # application: 6 # name: api-gateway 7 8 9 10 11 #指定注冊中心地址 12 # eureka: 13 # client: 14 # serviceUrl: 15 # defaultZone: http://localhost:8761/eureka/ 16 17 zuul: 18 routes: 19 order-service: /apigateway/** 20 product-service: /apigateway1/** 21 sensitive-headers: 22 #統一入口為上面的配置,其他入口忽略 23 #ignored-patterns: /*-service/** 24 25 hystrix: 26 command: 27 default: 28 execution: 29 isolation: 30 thread: 31 timeoutInMilliseconds: 5000 32 ribbon: 33 ##指的是建立連接所用的時間,適用於網絡狀況正常的情況下,兩端連接所用的時間。 34 ReadTimeout: 2000 35 ##指的是建立連接后從服務器讀取到可用資源所用的時間。 36 ConnectTimeout: 5000 37 feign: 38 hystrix: 39 enabled: true 40
測試:可以直接打開yml(改名:bootstrap.yml)
-
其他服務
1 spring: 2 application: 3 name: product-service 4 cloud: 5 config: 6 discovery: 7 service-id: CONFIG-SERVER 8 enabled: true 9 label: master 10 添加pom.xml 11 12 <dependency> 13 <groupId>org.springframework.cloud</groupId> 14 <artifactId>spring-cloud-config-client</artifactId> 15 </dependency>
可以發現通過config-server可以讀取配置,正常可以調用。