SpringCloud配置中心搭建与使用(本地存储配置)


转自:“https://msd.misuland.com/pd/3107373619924174238

 

本文涉及要点:

  • SpringCloud Config Center作为配置中心
  • 本地存储配置文件(还有svn,git方式等)
  • 配置中心config的优先级问题
  • 基于HTTP Basic的用户认证

SpringBoot版本:2.0.4.RELEASE

SpringCloud版本:Finchley.SR1

application.name:

服务端:config-center

客户端:static-configer

依赖

服务端:

  • pom.xml配置:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 

客户端:

  • pom.xml配置:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> 

配置

服务端:

  • SpringBoot启动类添加@EnableConfigServer注解
  • bootstrap.yml配置:
spring: application: name: config-center profiles: #native表示本地方式 active: native cloud: config: server: native: # 配置文件存放路径 search-locations: classpath:/cnf 

客户端:

  • bootstrap.yml配置:
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( {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 ( {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:

  • 默认false
  • allowOverride=true时才生效
  • true:ConfigCenter最低优先级,不覆盖任何现有属性源(包括本地配置文件)
  • 也就是说设置为true,Client Server之外的任何配置优先级都大于对应的Client Server config

allowOverride:

  • 默认:true
  • 用来控制systemPropertiesOverride是否生效
  • 设置为false以防止用户意外更改默认值(默认值:配置中心配置的值)
  • 设置为false,overrideSystemProperties的值为默认值true,修改无效

overrideSystemProperties:

  • 默认:true
  • 用以控制ConfigCenter是否覆盖【命令行指定】的属性(“-D”指定、“–”前缀指定)

配置中心公共配置

比如,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中的配置会被应用到所有服务。

基于HTTP Basic的用户认证

服务端新增配置:

pom.xml:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </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 

 


 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM