Spring Cloud Config为服务消费者和服务提供者提供了外部化配置支持,方便了对不同的项目,不同的环境的配置提供了统一化管理。
- 创建cloud_config_server(配置中心)
1.1 创建spring boot工程,配置pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
其它通用配置请查看前言通用配置
1.2 创建springboot启动类,加入@EnableConfigServer注解
@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigServerApplication.class, args);
}
}
1.3配置application.properties
spring.application.name=config-server
server.port=10001
# config
spring.cloud.config.server.git.uri=http://git.oschina.net/xzwei/spring_cloud_config/
spring.cloud.config.server.git.searchPaths=local_config
spring.cloud.config.server.git.username=XXXXX@qq.com
spring.cloud.config.server.git.password=XXXXXX
配置说明:
spring.application.name 应用名称
server.port 对外暴露的端口
spring.cloud.config.server.git.uri 存放配置的git项目地址
spring.cloud.config.server.git.searchPaths 相对路径
spring.cloud.config.server.git.username 你的git账号
spring.cloud.config.server.git.password 对应的git密码
在git上local_config文件下创建四个文件:
local-test.properties 内容:from=git-test-1.0
local-dev.properties 内容:from=git-dev-1.0
local-prov.properties 内容:from=git-prov-1.0
local-default.properties 内容:from=git-default-1.0
其中,local是使用者的名称,test,dev,prov,default是使用者的环境,在使用者中将详细介绍。
1.4启动config_server
http://localhost:10001/local/test
将会看到 git-test-1.0
http://localhost:10001/local/dev
git-dev-1.0
http://localhost:10001/local/prov
git-prov-1.0
2. 创建cloud_config_client
2.1 创建spring boot工程,配置pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
其它通用配置请查看前言通用配置
2.2 创建bootstrap.properties
spring.application.name=local
spring.cloud.config.profile=test
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:10001/
server.port=10002
注意:这些配置必须要配置在bootstrap.properties中。
local是应用名称
profile是环境
label是git的分支名称,默认是master
uri是配置中心的地址
2.3 创建启动类
@SpringBootApplication
public class CloudConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigClientApplication.class, args);
}
}
2.4 创建rest接口
@RestController
public class IndexWeb {
@Value("${from}")
private String from;
@RequestMapping("/")
public String index() {
return from;
}
}
- 启动config_server ,config_client.
访问:http://localhost:10002/
本文介绍如何使用Spring Cloud Config搭建配置中心,包括创建配置服务器和服务客户端,并通过实例演示如何实现不同环境下的配置管理。

被折叠的 条评论
为什么被折叠?



