本文涉及要点:
SpringBoot版本:2.0.4.RELEASE
SpringCloud版本:Finchley.SR1
application.name:
服务端:config-center
客户端:static-configer
服务端:
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Finchley.SR1version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
客户端:
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
dependencies>
服务端:
spring:
application:
name: config-center
profiles:
#native表示本地方式
active: native
cloud:
config:
server:
native:
# 配置文件存放路径
search-locations: classpath:/cnf
客户端:
spring:
application:
name: static-configer
profiles:
active: qas
# 配置中心存放配置文件格式:${application.name}-${profiles.active}.myl
# 例如static-configer-dev.yml、static-configer-qas.yml
# 通过上述两个配置去配置中心读取对应的配置文件
cloud:
config:
# uri 配置中心地址
uri: http://localhost:8000
fail-fast: true
服务端:
在src/main/resource/新增cnf文件夹,不推荐使用config作为名称,因为config会作为配置中心本身自己的配置文件夹
并在cnf中新增三个配置文件static-configer-dev.yml、static-configer-prd.yml、static-configer-qas.yml,分别填写不同配置
配置效果与写在本地一致
先启动服务端,后启动客户端
客户端日志打印了如下日志表示配置成功:
Fetching config from server at : http://localhost:8000
Located environment: name=static-configer, profiles=[qas], label=null, version=null, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='classpath:/config/static-configer-qas.yml'}]}
The following profiles are active: qas
无论配置文件是什么格式,yml或者properties
浏览器访问http://localhost:8000/static-configer-dev.yml( s p r i n g . a p p l i c a t i o n . n a m e − {spring.application.name}- spring.application.name−{profile})能输出yml格式的配置
spring:
cloud:
config:
override-none: true
访问 http://localhost:8000/static-configer-dev.properties 就能输出properties格式的配置
spring.cloud.config.override-none: true
或者:http://localhost:8000/static-configer/dev ( s p r i n g . a p p l i c a t i o n . n a m e / {spring.application.name}/ spring.application.name/{profile})
{"name":"static-configer","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/cnf/application.yml","source":{"spring.cloud.config.override-none":true}}]}
关键配置:
注意:这段配置是写在Config Server的配置文件中才会生效,Config Client则会报错
#默认情况下:
spring:
cloud:
config:
# 配置中心配置覆盖Java运行时参数。
# 值为true时:java -jar -Dserver.port=6666 myapp.jar 指定端口将被远程配置覆盖掉
# 外部属性覆盖系统属性,系统属性:JVM -Dparam
overrideSystemProperties: true
allowOverride: true
override-none: false
源码如下:
//org.springframework.cloud.bootstrap.config.PropertySourceBootstrapProperties.java
/**
* Flag to indicate that the external properties should override system properties.
* Default true.
*/
private boolean overrideSystemProperties = true;
/**
* Flag to indicate that {@link #isOverrideSystemProperties()
* systemPropertiesOverride} can be used. Set to false to prevent users from changing
* the default accidentally. Default true.
*/
private boolean allowOverride = true;
/**
* Flag to indicate that when {@link #setAllowOverride(boolean) allowOverride} is
* true, external properties should take lowest priority, and not override any
* existing property sources (including local config files). Default false.
*/
private boolean overrideNone = false;
优先级默认情况下:
配置中心>本地配置
overrideNone:
allowOverride:
overrideSystemProperties:
比如,serverA-dev.yml、serverB-dev.yml如果两个服务都需要使用同一个服务发现(Eureka)的配置,那就要在不同的配置文件中配置多遍,万一Eureka地址改了,这样的话,每个配置文件都要修改一遍,非常难以维护
与一般服务一样,指定的配置文件存放路径下(classpath:/cnf)application.yml(properties)将会被当做所有服务的公共配置
application-profile(dev\qas\prd).yml会被作为对应的环境的公共文件
例如存在application-dev.yml、application-qas.yml、application-prd.yml
编译profile=dev环境时application-dev.yml中的配置会被应用到所有服务。
服务端新增配置:
pom.xml:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
application.yml:
spring:
security:
user:
name: admin
password: admin123
客户端新增配置:
方式一:
spring:
clould:
config:
username: admin
password: admin123
方式二:
spring:
clould:
config:
uri: http://admin:admin123@localhost:8080/
如果方式一和方式二合并:
spring.cloud.config.password和spring.cloud.config.username的优先级更高,URL中包含的账号和密码将无效。
spring:
clould:
config:
uri: http://admin:admin123@localhost:8080/
username: admin2
password: admin456