- 项目地址完整例子传送门
- 项目中用到模块
- 模块介绍:
- eureka-server-peer-awareness: 提供注册中心的服务
- config:测试实例中需要用到的所有配置文件
- config-server: Spring Cloud统一配置管理的服务端
- config-client: Spring Cloud统一配置管理的客户端
- 官方文档
1.Spring Cloud Config介绍以及使用方式
2.spring-cloud/spring-cloud-config官方项目
1.Spring Cloud Config介绍
- Spring Cloud Config不是Netflix公司开源的项目,其实spring团队针对于微服务打造的一个全新的服务,目的是为了治理实际业务中服务过多导致的配置文件繁杂难以维护的问题。
- Spring Cloud Config可以对微服务配置进行统一的外部管理,并且默认采用Git来管理配置信息。将每个微服务的配置文件抽离出来,放置到统一的文件夹下管理或者针对于不同项目仓库,统一拉取管理。
- Spring Cloud Config包含了服务端Server和客户端Client。服务端用于从Git仓库中加载配置,并且缓存到本地,客户端用于从服务端获取配置信息。
2.创建Spring Cloud Config的服务端
- 在centralpark父项目下创建子项目config-server
- pom.xml文件中,引入依赖
spring-cloud-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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.friends</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.friends</groupId>
<artifactId>centralpark</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
- 启动类中
ConfigServerApplication.java
,添加注解@EnableConfigServer
,声明为配置服务类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 在
application.yml
中,写入配置中心的配置
server:
port: 8202
spring:
application:
name: ConfigServer
profiles:
active: configServer
---
spring:
cloud:
config:
server:
git:
uri: https://github.com/SpanishSoap/spring-cloud-example.git
search-paths: config
#username: xxx 若是私有仓库添加账号密码凭证
#password: xxx
profiles: configServer
spring.application.name:ConfigServer
服务名称
spring.profiles.active:configServer
代表需要激活的配置文件子部分,现在只有一个子配置
spring.cloud.config.server.git.uri: https://xx.git
git地址
spring.cloud.config.server.git.search-paths:config
代表搜索的子文件夹
spring.profiles:configServer
子配置名称
3.config-server中conf文件夹下配置文件
ConfigClient.yml
中
config: I’m ConfigClient
config-comm: I’m ConfigClientComm
ConfigClient-dev.yml
中
config: I’m ConfigClient-Dev
config-comm: I’m ConfigClientComm-Dev
ConfigClient-test.yml
中
config: I’m ConfigClient-Test
config-comm: I’m ConfigClientComm-Test
4.启动config-server服务
- 访问
http://localhost:8202/ConfigClient/dev
可见:
{
"name": "ConfigClient",
"profiles": [
"dev"
],
"label": null,
"version": "e2ac48f72403073325241f56a255ee062a106f68",
"state": null,
"propertySources": [
{
"name": "https://github.com/SpanishSoap/spring-cloud-example.git/config/ConfigClient-dev.yml",
"source": {
"config": "I'm ConfigClient-Dev",
"config-comm": "I'm ConfigClientComm-Dev"
}
},
{
"name": "https://github.com/SpanishSoap/spring-cloud-example.git/config/ConfigClient.yml",
"source": {
"config": "I'm ConfigClient",
"config-comm": "I'm ConfigClientComm"
}
}
]
}
-
若访问默认的ConfigClient.yml配置文件,可以使用:
http://localhost:8202/ConfigClient/default
-
启动服务后访问配置规则为:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
-
{application}
为spring.application.name
{profile}
为spring.profiles.active
{label}
为git分支,若不指定默认为master
5.搭建客户端config-client
- 有了config-server服务端,可以从git端拉取配置文件到本地缓存,现在创建config-client从config-server中拉取配置到自己的配置文件中
- pom.xml文件中
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.friends</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Cloud</description>
<parent>
<groupId>org.friends</groupId>
<artifactId>centralpark</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
- 启动类中,不用任何特殊配置
- 创建
ConfigClientApplicationController.java
读取配置文件,验证是否拉取配置成功
@RestController
public class ConfigClientApplicationController {
@Value("${config}")
private String configString;
@GetMapping("configString")
public String configString(){
return configString;
}
@Value("${config-comm}")
private String configCommString;
@GetMapping("configCommString")
public String configCommString(){
return configCommString;
}
}
- 项目主配置文件中
bootstrap.yml
,注意这里一定要使用bootstrap配置文件引导,不能使用application
server:
port: 8301
spring:
profiles:
active: profilesA
---
spring:
profiles: profilesA
application:
name: ConfigClient
cloud:
config:
uri: http://localhost:8201/
label: master
profile: dev
spring.cloud.config.uri
: config-server地址
spring.cloud.config.label
: 拉取git分支,对应{label}
spring.cloud.config.profile
: 拉取环境,对应{profile}
spring.profiles.name
:拉取主体,对应{application}
6.启动config-client服务
- 访问接口
http://localhost:8301/configString
返回,获取配置成功I’m ConfigClient-Dev
上一篇:Spring Cloud声明式服务调用,Feign(Finchley版本)
下一篇:Spring Cloud 分布式配置中心,配置文件子目录存储以及高可用 ,Spring Cloud Config(Finchley版本)