Spring Cloud Config为分布式系统中的外部化配置提供服务器端和客户端支持。使用Config Server,您可以在中心位置管理所有环境中应用程序的外部属性。客户端和服务器上的概念与Spring Environment和PropertySource抽象,因此它们非常适合Spring应用程序,但可以与以任何语言运行的任何应用程序一起使用。在应用程序从开发人员到测试人员再到生产人员的整个部署过程中,您可以管理这些环境之间的配置,并确保应用程序具有它们迁移时所需的一切。服务器存储后端的默认实现使用git,因此它轻松支持带标签的配置环境版本,并且可通过各种工具来访问以管理内容。添加替代实现并将其插入Spring配置很容易。
config server
- 父工程下创建一个项目
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springcloud-demo</artifactId>
<groupId>demo.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<name>config-server</name>
<dependencies>
<!-- config server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
- 修改application.yml文件
server:
port: 8888
spring:
application:
name: config-server
cloud:
config:
server:
git:
#git仓库地址
uri: https://github.com/dean4lee/springcloud-demo
#仓库地址下的路径,多个用,隔开
search-paths: conf
#用户名
username:
#密码
password:
git仓库地址有3个文件
- hello-dev.yml
- hello-pro.yml
- hello-test.yml
- 开启配置服务器
@SpringBootApplication
//开启配置服务器
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 测试
浏览器访问http://localhost:8888/hello/dev
{
"name": "hello",
"profiles": [
"dev"
],
"label": null,
"version": "5ffedb8bb6174039200a961d311fe2a6976d367f",
"state": null,
"propertySources": [
{
"name": "https://github.com/dean4lee/springcloud-demo/conf/hello-dev.yml",
"source": {
"user.username": "admin",
"user.password": 12345
}
}
]
}
直接访问文件数据http://localhost:8888/hello-dev.yml
user:
password: 12345
username: admin
仓库中的配置文件会被映射成web接口:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
hello-dev.yml中application为hello,profile为dev
config client
- 在父工程下创建一个项目
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springcloud-demo</artifactId>
<groupId>demo.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client</artifactId>
<name>config-client</name>
<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>
</dependencies>
</project>
- 创建一个bootstrap.yml
spring:
cloud:
config:
#配置服务器地址
uri: http://localhost:8888
#对应{application}
name: hello
#对应{profile}
profile: dev
#git分支
label:
#如果有密码验证
username:
password:
这些参数必须设置在bootstrap.yml中才能正确加载,由于config配置优先于application.yml文件加载
- application.yml
server:
port: 8880
spring:
application:
name: config-client
- 创建测试类
@RestController
public class TestCtrl {
@Value("${user.username}")
private String username;
@GetMapping("getUsername")
public String getUsername(){
return username;
}
}
- 测试
浏览器访问http://localhost:8880/getUsername
admin
作者公众号