目錄java
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies>
Spring Cloud爲開發人員提供了快速構建分佈式系統中的一些通用模式(例如配置管理,服務發現,斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖,領導選舉,分佈式 會話,羣集狀態)。 分佈式系統的協調引出樣板模式(boiler plate patterns),而且使用Spring Cloud開發人員能夠快速地實現這些模式來啓動服務和應用程序。 它們能夠在任何分佈式環境中正常工做,包括開發人員本身的筆記本電腦,裸機數據中心和受管平臺,如Cloud Foundry。git
Version: Brixton.SR7github
Spring Cloud專一於爲經典用例和擴展機制提供良好的開箱即用web
原生雲是應用程序開發的一種風格,鼓勵在持續交付和價值驅動領域的最佳實踐。
Spring Cloud的不少特性是基於Spring Boot的。更多的是由兩個庫實現:Spring Cloud Context and Spring Cloud Commons。正則表達式
Spring Boot關於使用Spring構建應用有硬性規定:通用的配置文件在固定的位置,通用管理終端,監控任務。創建在這個基礎上,Spring Cloud增長了一些額外的特性。spring
Spring Cloud會建立一個「bootstrap」的上下文,這是主應用程序的父上下文。對應的配置文件擁有最高優先級,而且,默認不能被本地配置文件覆蓋。對應的文件名bootstrap.yml或bootstrap.properties。shell
可經過設置spring.cloud.bootstrap.enabled=false
來禁止bootstrap進程。bootstrap
當用SpringApplication
或SpringApplicationBuilder
建立應用程序上下文時,bootstrap上下文將做爲父上下文被添加進去,子上下文將繼承父上下文的屬性。後端
子上下文的配置信息可覆蓋父上下文的配置信息。安全
spring.cloud.bootstrap.name
(默認是bootstrap),或者spring.cloud.bootstrap.location
(默認是空)
spring.cloud.config.allowOverride=true
spring.cloud.config.overrideNone=true
spring.cloud.config.overrideSystemProperties=false
在/META-INF/spring.factories
的key爲org.springframework.cloud.bootstrap.BootstrapConfiguration
,定義了Bootstrap啓動的組件。
在主應用程序啓動以前,一開始Bootstrap上下文建立在spring.factories文件中的組件,而後是@Beans
類型的bean。
關鍵點:spring.factories、PropertySourceLocator
應用程序可經過EnvironmentChangedEvent
監聽應用程序並作出響應。
Spring的bean被@RefreshScope將作特殊處理,可用於刷新bean的配置信息。
注意
/refresh
Spring Cloud可對配置文件的值進行加密。
若是有"Illegal key size"異常,那麼須要安裝JCE。
除了Spring Boot提供的服務點,Spring Cloud也提供了一些服務點用於管理,注意都是POST請求
/env
:更新Environment
、從新綁定@ConfigurationProperties
跟日誌級別/refresh
從新加載配置文件,刷新標記@RefreshScope
的bean/restart
重啓應用,默認不可用/pause
、/resume
服務發現、負載均衡、熔斷機制這種模式爲Spring Cloud客戶端提供了一個通用的抽象層。
經過@Bean
跟@LoadBalanced
指定RestTemplate
。注意URI須要使用虛擬域名(如服務名,不能用域名)。
以下:
@Configuration public class MyConfiguration { @LoadBalanced @Bean RestTemplate restTemplate() { return new RestTemplate(); } } public class MyClass { @Autowired private RestTemplate restTemplate; public String doOtherStuff() { String results = restTemplate.getForObject("http://stores/stores", String.class); return results; } }
注意@Primary
註解的使用。
@Configuration public class MyConfiguration { @LoadBalanced @Bean RestTemplate loadBalanced() { return new RestTemplate(); } @Primary @Bean RestTemplate restTemplate() { return new RestTemplate(); } } public class MyClass { @Autowired private RestTemplate restTemplate; @Autowired @LoadBalanced private RestTemplate loadBalanced; public String doOtherStuff() { return loadBalanced.getForObject("http://stores/stores", String.class); } public String doStuff() { return restTemplate.getForObject("http://example.com", String.class); } }
忽略肯定名字的服務發現註冊,支持正則表達式配置。
Spring Cloud Config提供服務端和客戶端在分佈式系統中擴展配置。支持不一樣環境的配置(開發、測試、生產)。使用Git作默認配置後端,可支持配置環境打版本標籤。
可經過IDE運行或maven運行。
默認加載property資源的策略是克隆一個git倉庫(at spring.cloud.config.server.git.uri')。
HTTP服務資源的構成:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
application是SpringApplication的spring.config.name,(通常來講'application'是一個常規的Spring Boot應用),profile是一個active的profile(或者逗號分隔的屬性列表),label是一個可選的git標籤(默認爲"master")。
建立以Spring Boot應用便可,添加依賴「org.springframework.cloud:spring-cloud-starter-config」。
配置application.properties,注意URL爲配置服務端的地址
spring.cloud.config.uri: http://myconfigserver.com
針對系統外的配置項(如name-value對或相同功能的YAML內容),該服務器提供了基於資源的HTTP接口。使用@EnableConfigServer註解,該服務器能夠很容易的被嵌入到Spring Boot 系統中。使用該註解以後該應用系統就是一個配置服務器。
@SpringBootApplication @EnableConfigServer public class ConfigApplicion { public static void main(String[] args) throws Exception { SpringApplication.run(ConfigApplicion.class, args); } }
若是配置庫是基於文件的,服務器將從application.yml和foo.yml中建立一個Environment
對象。高優先級的配置優先轉成Environment
對象中的PropertySource
。
默認的EnvironmentRepository
是用Git後端進行實現的,Git後端對於管理升級和物理環境是很方便的,對審計配置變動也很方便。也能夠file:
前綴從本地配置庫中讀取數據。
這個配置庫的實現經過映射HTTP資源的{label}
參數做爲git label(提交id,分支名稱或tag)。若是git分支或tag的名稱包含一個斜槓 ("/"),此時HTTP URL中的label須要使用特殊字符串"(_)"來替代(爲了不與其餘URL路徑相互混淆)。若是使用了命令行客戶端如 curl,請謹慎處理URL中的括號(例如:在shell下請使用引號''來轉義它們)。
Git URI佔位符
Spring Cloud Config Server支持git庫URL中包含針對{application}和 {profile}的佔位符(若是你須要,{label}也可包含佔位符, 不過要牢記的是任何狀況下label只指git的label)。因此,你能夠很容易的支持「一個應用系統一個配置庫」策略或「一個profile一個配置庫」策略。
模式匹配和多資源庫
spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo repos: simple: https://github.com/simple/config-repo special: pattern: special*/dev*,*special*/dev* uri: https://github.com/special/config-repo local: pattern: local* uri: file:/home/configsvc/config-repo
若是 {application}/{profile}不能匹配任何表達式,那麼將使用「spring.cloud.config.server.git.uri」對應的值。在上例子中,對於 "simple" 配置庫, 匹配模式是simple/* (也就說,不管profile是什麼,它只匹配application名稱爲「simple」的應用系統)。「local」庫匹配全部application名稱以「local」開頭任何應用系統,無論profiles是什麼(來實現覆蓋因沒有配置對profile的匹配規則,「/*」後綴會被自動的增長到任何的匹配表達式中)。
Git搜索路徑中的佔位符
spring.cloud.config.server.git.searchPaths
伴隨着版本控制系統做爲後端(git、svn),文件都會被check out
或clone
到本地文件系統中。默認這些文件會被放置到以config-repo-爲前綴的系統臨時目錄中。在Linux上,譬如應該是/tmp/config-repo-<randomid>
目錄。有些操做系統routinely clean out放到臨時目錄中,這會致使不可預知的問題出現。爲了不這個問題,經過設置spring.cloud.config.server.git.basedir
或spring.cloud.config.server.svn.basedir
參數值爲非系統臨時目錄。
使用本地加載配置文件。
須要配置:spring.cloud.config.server.native.searchLocations
跟spring.profiles.active=native
。
路徑配置格式:classpath:/, classpath:/config,file:./, file:./config
。
基於文件的資源庫
在基於文件的資源庫中(i.e. git, svn and native),這樣的文件名application*
命名的資源在全部的客戶端都是共享的(如 application.properties, application.yml, application-*.properties,etc.)。
屬性覆蓋
「spring.cloud.config.server.overrides」添加一個Map類型的name-value對來實現覆蓋。
例如
spring: cloud: config: server: overrides: foo: bar
會使全部的配置客戶端應用程序讀取foo=bar到他們本身配置參數中。
經過這個指示器可以檢查已經配置的EnvironmentRepository
是否正常運行。
經過設置spring.cloud.config.server.health.enabled=false
參數來禁用健康指示器。
你能夠自由選擇任何你以爲合理的方式來保護你的Config Server(從物理網絡安全到OAuth2 令牌),同時使用Spring Security和Spring Boot 能使你作更多其餘有用的事情。
爲了使用默認的Spring Boot HTTP Basic 安全,只須要把Spring Security 增長到classpath中(如org.springframework.boot.spring-boot-starter-security)。默認的用戶名是「user」,對應的會生成一個隨機密碼,這種狀況在實際使用中並無意義,通常建議配置一個密碼(經過 security.user.password屬性進行配置)並對這個密碼進行加密。
若是遠程屬性包含加密內容(以{cipher}開頭),這些值將在經過HTTP傳遞到客戶端以前被解密。
使用略
配置服務可使用對稱(共享)密鑰或者非對稱密鑰(RSA密鑰對)。
使用略
配置文件可加後綴".yml"、".yaml"、".properties"
/{name}/{profile}/{label}/{path}
通常配置服務運行在單獨的應用裏面,只要使用註解@EnableConfigServer
便可嵌入到其餘應用。
添加依賴spring-cloud-config-monitor
,激活Spring Cloud 總線,/monitor
端點便可用。
當webhook激活,針對應用程序可能已經變化了的,配置服務端將發送一個RefreshRemoteApplicationEvent
。
經過spring.cloud.config.uri
屬性配置Config Server地址
若是用的是Netflix,則用eureka.client.serviceUrl.defaultZone
進行配置。
在一些例子裏面,可能但願在沒有鏈接配置服務端時直接啓動失敗。可經過spring.cloud.config.failFast=true
進行配置。
添加依賴spring-retry
、spring-boot-starter-aop
,設置spring.cloud.config.failFast=true
。默認的是6次重試,初始補償間隔是1000ms,後續補償爲1.1指數乘數,可經過spring.cloud.config.retry.*
配置進行修改。
路徑:/{name}/{profile}/{label}
label對於回滾到以前的版本頗有用。
經過spring.cloud.config.password
、spring.cloud.config.username
進行配置。