资料参考:《Spring Cloud 微服务实战》
目录
分布式配置中心
spring cloud config
是spring cloud
团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端和客户端两部分,其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息,加密/解密信息等访问接口,而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。spring cloud config
实现了对服务端和客户端中环境变量和属性设置的抽象映射,所以它除了适用于spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于spring cloud config
实现的配置中心默认采用git来存储配置信息,所以使用spring cloud config
构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过git
客户端工具来方便地管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如说svn仓库,本地化文件系统。
快速入门
构建一个基于git存储的分布式配置中心,并在客户端中演示如何通过配置指定微服务应用的所属配置中心,并让其能够从配置中心获取配置信息并绑定到代码的整个过程。
构建配置中心
- 创建一个名
为config-server-git
的springboot服务,第一步,加入依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 第二步,创建
springboot
程序主类,加上注解@EnableConfigServer
,开启spring cloud config
的服务端功能。
@SpringBootApplication
@EnableConfigServer
public class GitApplication {
public static void main(String[] args) {
SpringApplication.run(GitApplication.class, args);
}
}
- 第三步,在
application.yml
中添加配置服务的基本信息以及git仓库的相关信息
spring.application.name=config-server-git
server.port=7001
spring.cloud.config.server.git.uri=https://github.com/servef-toto/SpringCloud-Demo/
spring.cloud.config.server.git.search-paths=config-server-file/git-config
spring.cloud.config.server.git.username=xxxxxx
spring.cloud.config.server.git.password=xxxxx
其中git的配置分别表示如下内容
spring.cloud.config.server.git.uri
: 配置的git仓库位置spring.cloud.config.server.git.search-paths
: 配置仓库路径下的相对搜索位置,可以配置多个spring.cloud.config.server.git.username
: 访问git的用户名spring.cloud.config.server.git.password
: 访问git仓库的用户密码
到这里,一个基本得基于git得配置中心就好了。
配置规则详解
在git配置信息中指定的仓库位置,https://github.com/servef-toto/SpringCloud-Demo/config-server-file/git-config
目录下创建五个不同的配置文件:
zhihao.yml
zhihao-dev.yml
zhihao-test.yml
zhihao-pro.yml
application.yml
内容分别是:
zhihao.yml
from: git-default-1.0
spring:
datasource:
username: user_default
zhihao-dev.yml
from: git-dev-1.0
spring:
datasource:
username: user_dev
zhihao-test.yml
from: git-test-1.0
spring:
datasource:
username: user_test
zhihao-pro.yml
from: git-pro-1.0
spring:
datasource:
username: user_pro
application.yml
from: git-pro-1.0
spring:
datasource.
username:
zhihao.miao1
为了测试版本控制,在git
仓库的master
分支中,我们为from
属性加入1.0
的后缀,同时创建一个config-label-test
分支,并将各配置文件中的值用2.0
做为后缀.
完成上面的工作我们就可以通过url来访问这些配置内容了。
* /{application}/{profile}/[{label}]
* /{application}-{profile}.yml
* /{label}/{application}-{profile}.yml
* /{application}-{profile}.properties
* /{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.yml
对应的配置文件,其中{label}
对应git上不同的分支,默认是master
。我们可以尝试构造不同的url来访问不同的配置内容,比如,要访问config-label-test
分支,zhihao
应用的prod环境,就可以访问这个url:http://localhost:9090/zhihao/pro/config-label-test
同时,我们可以看到config-server-git
的控制台中还输出了下面的内容,配置服务器在从git中获取了配置信息后,会存储一份在config-server-git
的文件系统中,实质上config-server-git
是通过git clone
命令将配置内容复制了一份在本地存储,然后读取这些内容并返回给微服务应用进行加载。
2017-08-15 22:10:07.568 INFO 28701 --- [io-9090-exec-10] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ac2c286: startup date [Tue Aug 15 22:10:07 CST 2017]; root of context hierarchy
2017-08-15 22:10:07.579 INFO 28701 --- [io-9090-exec-10] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/config-repo/application-pro.yml
2017-08-15 22:10:07.579 INFO 28701 --- [io-9090-exec-10] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/config-repo/application.yml
2017-08-15 22:10:07.579 INFO 28701 --- [io-9090-exec-10] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5ac2c286: startup date [Tue Aug 15 22:10:07 CST 2017]; root of context hierarchy
config-server-git
通过从git在本地的仓库暂存,可以有效的防止当git仓库出现故障而引起无法加载配置信息的情况。我们可以通过断开网络(断开wifi),再次发起从http://localhost:9090/zhihaomiao/pro/config-label-test
请求,在控制台中可以输出如下内容,这些内容源于之前访问时存在于config-server-git服务本地文件系统中的配置信息。
2017-08-15 22:23:15.002 WARN 28701 --- [nio-9090-exec-5] .c.s.e.MultipleJGitEnvironmentRepository : Could not fetch remote for config-label-test remote: http://git.oschina.net/zhihaomiao/config-repo-demo
2017-08-15 22:23:15.074 INFO 28701 --- [nio-9090-exec-5] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7060493e: startup date [Tue Aug 15 22:23:15 CST 2017]; root of context hierarchy
2017-08-15 22:23:15.088 INFO 28701 --- [nio-9090-exec-5] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/config-repo/application-pro.yml
2017-08-15 22:23:15.089 INFO 28701 --- [nio-9090-exec-5] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/var/folders/p0/kw_s_8xj2gqc929nys7cj2yh0000gn/T/config-repo-2847833657021753497/config-repo/application.yml
2017-08-15 22:23:15.089 INFO 28701 --- [nio-9090-exec-5] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7060493e: startup date [Tue Aug 15 22:23:15 CST 2017]; root of context hierarchy
客户端配置映射
在完成上面得配置之后,确定配置中心已经启动。如何在微服务应用中获取上面的配置信息?
- 创建一个Springboot应用
config-client
,并在pom文件中引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 创建springboot应用主类
- 创建bootstrap.properties配置,来指定获取配置文件得配置中心得位置,和配置文件得信息。
#对应配置文件规则中得{application}部分
spring.application.name=zhihao
#对应配置文件规则中得{profile}部分
spring.cloud.config.profile=dev
#对应配置文件规则中得{label}部分
spring.cloud.config.label=master
#对应配置中心得地址
spring.cloud.config.uri=http://localhost:7001/
server.port=7002
上述配置参数与git中存储的配置文件中各个部分的对应关系如下:
spring.application.name
: 对应配置文件规则中的{application}
部分spring.cloud.config.profile
:对应配置文件规则中{profile}
部分spring.cloud.config.label
:对应配置文件规则中的{label}
部分spring.cloud.config.uri
:配置中心config-server
的地址。
这里需要格外注意,上面的属性必须配置在bootstrap.yml
中,这样config-server
中的配置信息才能被正确加载。springboot对配置文件的加载顺序,对于本应用jar包之外的配置文件加载会优于应用jar包内的配置内容,而通过bootstrap.yml
对config-server-git
的配置,使得该应用会从config-server-git
中获取一些外部配置信息,这些信息的优先级比本地的内容要高,从而实现了外部化配置。
- 创建一个接口来获取配置中心得from属性,通过@Value("${from}") 获取Environment对象,来获取属性得只
@RefreshScope
@RestController
public class TestController {
@Value("${from}")
private String from;
@Autowired
private Environment environment;
@RequestMapping("/from")
public String from() {
return this.from;
}
@RequestMapping("/envs")
public String envs() {
return this.environment.getProperty("from","undefined");
}
}
测试访问http://localhost:7002/from 和 http://localhost:7002/envs
git地址:https://github.com/servef-toto/SpringCloud-Demo/tree/master/config-server-file/git-config