一、 Spring cloud config 介绍
Spring cloud config是一个分布式的配置中心,用来给服务端和客户端实现外部的配置支持。可以充当为一个配置服务器来使用,为各应用提供一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource的抽象映射,所以它适用于Spring构建的应用程序,也可以在任何其他语言构建的应用程序中使用。
配置服务器默认采用git来存储配置信息,可以通过git来对配置进行版本的管理。
二、构建一个Spring cloud config应用
第一种方式:
将config-server注册为一个服务,然后其他服务直接连接该config-server。步骤如下:
1. 添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2. 在config-server启动类上添加一个@EnableConfigServer注解,用来开启Spring cloud config 。
package com.itmuch.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3. 在配置文件.properties中,添加如下配置:
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://github.com/zzb15997937197/spring-cloud-config.git/
username: git用户名
password: git 密码
url可以去掉.git以后的内容,只需要加一个仓库名就行:
https://github.com/zzb15997937197/spring-cloud-config
4. 在访问配置前,需要知道url与配置文件的映射关系如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml /{label}
/{application}-{profile}.yml
/{application}-{profile}.properties /{label}
/{application}-{profile}.properties
5. 在git中新建不同环境的配置文件
application.properties
application-dev.properties
application-test.properties
application-prod.properties
然后在每个环境的配置文件中,新增一个from属性:
from=git-default-1.0
from=git-dev-1.0
from=git-test-1.0
from=git-prod-1.0
验证一: 使用浏览器直接访问git配置
方式一: config-servier启动成功后可以直接访问地址: http://localhost:8080/application-dev.properties, application-uat.properties为git中存储的配置文件名称。
返回结果: profile: dev ,表示拿到了在git仓库中的 application-dev.properties文件里的配置:
方式二: 使用/{application}/{profile}来访问,查看结果,比如我访问git上配置为application-uat.properties的配置文件,只需要
http://localhost:8080/application/uat
返回结果如下:
解析一下:
{
"name": "application",
"profiles": ["uat"],
"label": "master",
"version": "d3f880cfcf2fbe2e5ae0beabcce17d8ebc34b4cc",
"state": null,
"propertySources": [{
"name": "https://github.com/zzb15997937197/spring-cloud-config.git/application-uat.properties",
"source": {
"profile": "abc"
}
}]
}
返回的json里面包含了,应用名,环境名,版本号,数据源等信息。
若需要禁用引导过程,那么设置 spring.cloud.bootstrap.enabled=false
验证二: 使用config-client拿到config-server在git中存储的配置
1. 启动一个config-client客户端,新增一个Bootstrap.yml文件,在bootstrap.yml中添加如下配置:
添加依赖:
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
通过profile来指定配置文件,bootstrap.yml文件中配置属性如下:
spring:
cloud:
config:
uri: http://localhost:8080
profile: dev
label: master # 当configserver的后端存储是Git时,默认就是master
application:
name: config-client
需要注意的是: 以上的属性配置需要放到bootstrap.yml文件中,不能放到application.yml中。如果是配置到application.yml文件中,spring cloud config client的会连接到spring.cloud.config.uri的默认值http://localhost:8888,而并非配置是http://localhost:8080 地址。
因为Spring Cloud有一个"引导上下文" 的概念,这是主应用程序的父上下文。引导上下文负责 从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.*中的属性不同,引导上下文加载bootstrap.*中的属性,该配置属性的优先级高于application.*,因此默认情况下,不会被本地配置所覆盖。
2.然后写一个测试的controller:
package com.itmuch.cloud;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String getProfile() {
return this.profile;
}
}
访问: localhost:8081/profile,结果如下:
最后将git中指定profile为dev的文件中的配置属性profile取出来了。
三、在config-server启动时加载配置文件
spring cloud config会在应用获取到首次请求时,将git上的配置克隆到本地,然后再响应给请求。这样会导致请求效率很慢的问题,spring cloud config同时也提供了一个配置属性,用于在服务启动的时候将git的配置克隆到本地,然后请求过来的时候会快速响应出去, 在bootstrap.yml文件中添加配置:
spring:
cloud:
config:
server:
git:
clone-on-start: true
添加这个配置后,再次用客户端请求config-server获取的速度会明显增加,只不过服务在启动时的时间会加长点。可以设置日志的级别为DEBUG,来看项目启动时的信息:
logging:
level:
org:
springframework:
cloud: DEBUG
boot: DEBUG
信息:
可以从控制台上发现,Spring Cloud Config已经将git的配置克隆到本地一个文件中:
2020-06-14 14:59:34.635 INFO 22840 --- [nio-8080-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/C:/Users/ADMINI~1/AppData/Local/Temp/config-repo-2472557199743150060/application-dev.properties
访问的时候的日志:
2020-06-14 20:48:35.272 DEBUG 55968 --- [nio-8080-exec-1] s.b.e.YamlPropertySourceLoader$Processor : Unmatched document: {server={port=8080}, spring={cloud={config={server={git={uri=https://github.com/zzb15997937197/spring-cloud-config, username=***, password=***., clone-on-start=true, search-paths=[foo, bar]}}}}}, logging={level={org.springframework.cloud=DEBUG, org.springframework.boot=DEBUG}}}