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的服务治理体系中。
这样咱们的微服务应用就能够经过配置中心的服务名来获取配置信息,这种方式比起传统的实现模式来讲更加有利于维护,
由于对于服务端的负载均衡配置和客户端的配置中心指定都经过服务治理机制一并解决了,既实现了高可用,也实现了自维护。