Environment
和PropertySource
抽象相同,所以它們與Spring應用<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-servers</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <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> --> </dependencies> </project>
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
配置文件nginx
server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter/ # 配置git倉庫的地址 # search-paths: config-repo # git倉庫地址下的相對地址,能夠配置多個,用,分割。 # username: # git倉庫的帳號 # password: # git倉庫的密碼 #security: # basic: # enabled: true # user: # name: user # password: 123456 #encrypt: # key: foo
profile: profile-default
http://localhost:8001/abc-default.propertiesgit
profile: cyjfoorbar
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
SpringApplication
中的spring.config.name
注入(即常規的Spring Boot應用程序中一般是「應用程序」),<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-clients</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
application.ymlweb
server: port: 8002
bootstrap.ymlspring
spring: cloud: config: uri: http://localhost:8001 profile: dev label: master #當configserver的後端存儲是git時,默認就是master application: name: foobak #profile: abc
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClientApplication { public static void main( String[] args ) { SpringApplication.run(ConfigClientApplication.class, args); } }
package com.itmuch.cloud; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile(){ return this.profile; } }
{application}
和{profile}
(以及{label}
)的佔位符,server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://gitee.com/cyj930307/{application}
模式格式是帶有通配符的{application}/{profile}
名稱的逗號分隔列表(可能須要引用以通配符開頭的模式)。
apache
server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter repos: simple: https://gitee.com/cyj930307/simple special: pattern: special*/dev*,special*/test* uri: https://gitee.com/cyj930307/special
{application}/{profile}
不匹配任何模式,它將使用在「spring.cloud.config.server.git.uri」下定義的默認uri。simple/*
(即全部配置文件中只匹配一個名爲「簡單」的應用程序)。/*
後綴自動添加到任何沒有配置文件匹配器的模式)。
server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter search-paths: - foo #路徑 - bar #路徑
要使用加密和解密功能,您須要在JVM中安裝全面的JCE(默認狀況下不存在)。bootstrap
您能夠從Oracle下載「Java加密擴展(JCE)無限強度管理策略文件」,並按照安裝說明後端
(實際上將JRE lib / security目錄中的2個策略文件替換爲您下載的文件)。安全
server: port: 8003 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter username: password: encrypt: key: foo
spring: datasource: username: dbuser password: '{cipher}FKSAJDFGYOS8F7GLHAKERGFHLSAJ'
.properties文件中的加密值不能用引號括起來,不然不會解密該值:服務器
spring.datasource.username: dbuser
spring.datasource.password: {cipher}FKSAJDFGYOS8F7GLHAKERGFHLSAJ
keytool -genkeypair -alias mytestkey -keyalg RSA \ -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" \ -keypass changeme -keystore server.jks -storepass letmein
keytool -genkeypair -alias mytestkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass changeme -keystore server.jks -storepass letmein
server.jks
文件放在類路徑(例如)中,而後在您的application.yml
中配置服務器:encrypt: keyStore: location: classpath:/server.jks password: letmein alias: mytestkey secret: changeme
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-servers-authc</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <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> </dependencies> </project>
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
security: basic: enabled: true user: name: user password: password123 server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter username: password: encrypt: key: foo
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-clients-authc</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
application.yml架構
server: port: 8002
bootstrap.yml
spring: cloud: config: uri: http://user:password123@localhost:8001 profile: dev label: master #當configserver的後端存儲是git時,默認就是master application: name: foobak #profile: abc
啓動類
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClientApplication { public static void main( String[] args ) { SpringApplication.run(ConfigClientApplication.class, args); } }
Controller類
package com.itmuch.cloud; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile(){ return this.profile; } }
問題:
配置的優先級高於路徑
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-servers</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <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> --> </dependencies> </project>
server: port: 8001 spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter username: password:
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-clients-refresh</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
server: port: 8002 spring: cloud: config: uri: http://localhost:8001 profile: dev label: master #當configserver的後端存儲是git時,默認就是master application: name: foobak #profile: abc management: security: enabled: false
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClientApplication { public static void main( String[] args ) { SpringApplication.run(ConfigClientApplication.class, args); } }
package com.itmuch.cloud; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope class HelloController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile(){ return this.profile; } }
Spring Cloud Bus將分佈式系統的節點與輕量級消息代理連接。
這能夠用於廣播狀態更改(例如配置更改)或其餘管理指令。一個關鍵
的想法是,總線就像一個分佈式執行器,用於擴展的Spring Boot應用
程序,但也能夠用做應用程序之間的通訊通道。目前惟一的實現是使用AMQP
代理做爲傳輸,可是相同的基本功能集(還有一些取決於傳輸)在其餘傳輸的
路線圖上。
環境的配置:
下載:RabbitMQ 和erlang
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-clients-refresh-bus</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies> </project>
server: port: 8002 spring: cloud: config: uri: http://localhost:8001 profile: dev label: master #當configserver的後端存儲是git時,默認就是master application: name: foobak rabbitmq: host: localhost port: 5672 username: guest password: guest management: security: enabled: false
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigClientApplication { public static void main( String[] args ) { SpringApplication.run(ConfigClientApplication.class, args); } }
package com.itmuch.cloud; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope class HelloController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getProfile(){ return this.profile; } }
/bus/refresh
端點的destination參數來定位要刷新的應用程序。/bus/refresh?destination=customers:8000
,這樣消息總線上的微服務實例就會根據destination參數的值來判斷是否須要要刷新。customers:8000
指的是各個微服務的ApplicationContext ID。/bus/refresh?destination=customers:**
,這樣就能夠觸發customers微服務全部實例的配置刷新。spring.cloud.bus.trace.enabled=true
,這樣在/bus/refresh
端點被請求後,訪問/trace
端點就可得到相似以下的結果:
1.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-eureka</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
配置文件
server: port: 8761 eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:8761/eureka
啓動類
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main( String[] args ) { SpringApplication.run(EurekaApplication.class, args); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-servers-eureka</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>
配置文件
server: port: 8050 spring: application: name: spring-cloud-config-server-eureka cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter/ # search-paths: config-repo username: password: eureka: client: service-url: defaultZone: http://localhost:8761/eureka
啓動類
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication @EnableDiscoveryClient public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.itmuch.clod</groupId> <artifactId>spring-cloud-config</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>spring-cloud-config-clients-eureka</artifactId> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> </project>
配置文件
application.yml
server: port: 8051
bootstrap.yml
spring: application: name: foobak cloud: config: profile: dev label: master discovery: enabled: true # 默認false,設爲true表示使用註冊中心中的configserver配置而不本身配置configserver的uri service-id: spring-cloud-config-server-eureka # 指定config server在服務發現中的serviceId,默認爲:configserver eureka: client: service-url: defaultZone: http://user:123456@localhost:8761/eureka
啓動類
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ConfigClientApplication { public static void main( String[] args ) { SpringApplication.run(ConfigClientApplication.class, args); } }
Controller文件
package com.itmuch.cloud; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * 這邊的@RefreshScope註解不能少,不然即便調用/refresh,配置也不會刷新 * @author eacdy */ @RestController @RefreshScope class ConfigClientController { @Value("${profile}") private String profile; @GetMapping("/profile") public String getprofile(){ return this.profile; } }
訪問路徑
項目名稱:spring-cloud-config-servers-authc
配置文件:
server: port: 8001 spring: application: name: spring-cloud-config-server-basic cloud: config: server: git: uri: https://gitee.com/cyj930307/spring-cloud-starter username: password: eureka: client: service-url: defaultZone: http://localhost:8761/eureka security: basic: enabled: true # 開啓基於HTTP basic的認證 user: name: user # 配置登陸的帳號是user password: password123 # 配置登陸的密碼是password123
啓動該服務,並輸入http://localhost:8001/microservice-foo/dev
Config Server的高可用也分爲2種:
1.Config Server未註冊到Eureka Server上
這種狀況在Spring Cloud中稱之爲「Config First Bootstrap」。可使用一個負載均衡軟件(如nginx)來作高可用,Cloud Config Client URI指向Nginx。
2.Config Server註冊到了Eureka Server上
這種狀況,在Spring Cloud中稱之爲「Discovery First Bootstrap」。實現Config Server的高可用很簡單,只須要將多個Config Server註冊到Eureka server便可。
將Config Server做爲一個普通的微服務應用,歸入Eureka的服務治理體系中。
這樣咱們的微服務應用就能夠經過配置中心的服務名來獲取配置信息,這種方式比起傳統的實現模式來講更加有利於維護,
由於對於服務端的負載均衡配置和客戶端的配置中心指定都經過服務治理機制一併解決了,既實現了高可用,也實現了自維護。