写于:2018-12-18 08:52:37
一、Introduce
or
Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system. With the Config Server you have a central place to manage external properties for applications across all environments.
粗略翻译就一句话:Spring Cloud Config 就是为分布式环境准备的,用来将所有的配置集中进行管理。
二、Features
2.1、Server Features
HTTP, resource-based API for external configuration (name-value pairs, or equivalent YAML content)
(HTTP,为外部配置提供基于资源的API(键值对,或者等价的YAML内容)
Encrypt and decrypt property values (symmetric or asymmetric)
(属性值的加密和解密(对称加密和非对称加密))
Embeddable easily in a Spring Boot application using @EnableConfigServer
Spring boot 应用只需要 @EnableConfigServer 就能够实现功能)
2.2、Client Features
Config Client features (for Spring applications)(客户端特性)
Bind to the Config Server and initialize Spring
Environment
with remote property sources.绑定 Config Server,加载远程配置,进行 Spring Environment 的初始化
Encrypt and decrypt property values (symmetric or asymmetric).
属性值的加密和解密(对称加密和非对称加密)
@RefreshScope
for Spring@Beans
that want to be re-initialized when configuration changes.使用 @RefreshScope,在配置变更的时候,重新初始化 Bean
Use management endpoints: 【通过 actuator 提供的相关web访问端点】
/env
for updatingEnvironment
and rebinding@ConfigurationProperties
and log levels./refresh
for refreshing the@RefreshScope
beans./restart
for restarting the Spring context (disabled by default)./pause
and/resume
for calling theLifecycle
methods (stop()
andstart()
on theApplicationContext
).
- Bootstrap application context: a parent context for the main application that can be trained to do anything (by default, it binds to the Config Server and decrypts property values).
Bootstrap :spring Cloud 上下文,也是主程序的父上下文,所有关于 Config Server 的配置默认配置在其中
三、Getting Started
案例:构建 config server 和 config client ,模拟配置拉取。 友链: 《配置文件加载优先级》
3.1、项目结构如下
3.2、服务端 config-server
引入依赖
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- config server 服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Application.java
启动类追加注解
@EnableConfigServer
@SpringBootApplication
public class Application {
......
}
bootstrap.properties
配置如下
# applicationname
spring.application.name = config-server
# port
server.port = 28080
# git config
spring.cloud.config.server.git.uri = ${git仓库地址}
spring.cloud.config.server.git.search-paths = /case-1/**
spring.cloud.config.server.git.username = ${git仓库用户名}
spring.cloud.config.server.git.password = ${git仓库密码}
# 配置文件所在的分支
spring.cloud.config.label = master
准备配置文件,并发到git仓库中
启动服务,并测试
直接通过 url 访问:http://{ip}:{port}/{配置文件前缀}/{环境}
如: [http://127.0.0.1:28080/config-client/dev](http://127.0.0.1:28080/config-client/dev)
3.3、客户端 config-client
引入依赖
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
bootstrap.properties
配置如下
# 对应git 中配置文件的前缀
spring.cloud.config.name = config-client
# 配置文件环境
spring.cloud.config.profile = dev
# 配置服务中心
spring.cloud.config.uri = http://127.0.0.1:28080/
新增测试 API
/** 获取服务名称:spring.application.name **/
@RequestMapping("/get-name")
public String getApplicationName(){
return Optional.ofNullable(environment.getProperty("spring.application.name")).orElse("未知");
}
启动客户端
更改配置中的 spring.cloud.config.profile
,测试配置文件的变化。
[自行测试]