主要配置方式
4. 混合模式
详细说明:
1.引入配置
parent引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
模块项目引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2. git 模式
spring:
application:
name: configserver
profiles:
active: git #设置使用本地配置(默认是git,可以设置:subversion(SVN),native(本地))
cloud:
config:
server:
#如下是GIT配置
git:
uri: https://github.com/zuowj/learning-demos # 配置git仓库的地址(最后不需要带/,否则会出现:No custom http config found for URL: XXX)
search-paths: config # git仓库地址下的相对搜索地址(可用使用通配符),可以配置多个,用,分割。可以{application}实现按应用查配置
username: # git仓库的账号(公开仓库无需账号信息)
password: # git仓库的密码(公开仓库无需账号信息)
default-label: master #git默认分支
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
test:
pattern:
- '*/development'
- '*/staging'
uri: https://github.com/development/config-repo
通过使用spring.cloud.config.server.git.refreshRate
,可以控制配置服务器从Git后端获取更新配置数据的频率。此属性的值以秒为单位指定。默认值为0,这意味着每次请求配置服务器时,配置服务器都会从Git repo
获取更新后的配置。
2. svn方式
<!--spring cloud配置中心依赖[默认GIT]-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--使用SVN作为配置中心依赖-->
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
</dependency>
spring:
application:
name: configserver
profiles:
active: subversion #设置使用本地配置(默认是git,可以设置:subversion(SVN),native(本地))
cloud:
config:
server:
#如下是SVN配置
svn:
uri: http://svnhost:port/svn/app-config #SVN仓库地址
username: svnuser #SVN账号(如果没有权限可为空)
password: svnpassword #SVN密码(如果没有权限可为空)
default-label: trunk #默认SVN分支
3. 本地模式
spring:
application:
name: configserver
profiles:
active: native #设置使用本地配置(默认是git,可以设置:subversion(SVN),native(本地))
cloud:
config:
server:
#如下是本地文件配置
native:
search-locations: classpath:/configs #配置文件存放的目录
4. 混合模式
spring:
profiles:
active: composite #启用的配置文件
cloud:
config:
enabled: true
watch:
enabled: false
profile: ${spring.custom.default-profile}
name: ${spring.custom.default-name}
label: ${spring.custom.default-label}
failFast: false
# 本地集成服务端config配置
server: #ConfigServerProperties
bootstrap: true
prefix: /config
default-label: ${spring.custom.default-label}
default-application-name: ${spring.custom.default-name}
default-profile: ${spring.custom.default-profile}
strip-document-from-yaml: true #in "native" form
accept-empty: true #HTTP 404 needs to be sent
overrides: # 属性覆盖
foo: bar
health:
enabled: false
composite: # 复合数据源配置
-
type: native # 本地数据源配置
order: 1
add-label-locations: true
fail-on-error: false #how to handle exceptions during decryption
search-locations:
- classpath:/config/${spring.custom.default-env}
-
type: git # git数据源配置
order: 2
uri: http://gitlab.**.biz/**/config-server.git
search-paths: poly/${spring.custom.default-env}
skip-ssl-validation: true
force-pull: true
clone-on-start: true
delete-untracked-branches: true
basedir: /var/tt/config-repo #远程文件本地保存位置
timeout: 5 #单位秒
username: user
password: secret
proxy:
https: #http或https
host: my-proxy.host.io
port: '3128'
username: myproxyusername
password: myproxypassword
non-proxy-hosts: example.com
5. 高可用配置
spring cloud 实现服务端的高可用非常简单,主要有以下两种方式。
- 传统模式:不需要为这些服务端做额外的配置,只需要遵守一个配置规则,将所有的
config server
都指向同一个Git
仓库,这样所有的配置内容就通过统一的共享文件系统来维护。而客户端在指定Config Server
位置时,只需要配置Config Server
上层的负载均衡设备地址即可。 - 服务模式:我们仍可以将
Config Server
作为一个普通的微服务应用,纳入Eureka
的服务治理体系中。这样微服务应用就可以通过配置中心的服务名来获取配置信息。不过这部分实现需要客户端配合
6. 注意:
参数:
- spring.application.name:对应文件规则的
应用名
- spring.cloud.config.profile:对应
环境名
- spring.cloud.config.label:对应
分支名
- spring.cloud.config.uri:对应Config Server开放的地址
- 如果要用到动态刷新,SpringBoot 1.5版本以上需要使用management.security.enabled=false
参考资料:
https://www.cnblogs.com/zuowj/p/10432445.html#s-2
https://www.cnblogs.com/hellxz/p/9306507.html