Spring Cloud(九)Config配置中心
1 概述
1.1 产生背景
分布式系统中,会将服务拆分成一个个独立的服务,这些服务都要通过配置文件配置相应信息才能运行,随着系统内微服务数量的增多,配置文件也会不断的增多,大量的配置文件的管理成为一个繁琐的问题。因此一套集中式的、动态的配置管理设施是必不可少的。
1.2 Spring Cloud Config配置中心
- SpringCloud Config是一个提供外部集中式配置管理的组件,配置服务器为各种不同的微服务提供了一个中心化的外部配置中心。
SpringCloud Config分为服务端和客户端两部分:
- 服务端:分布式配置中心,是一个独立的微服务,用来连接并为客户端提供配置、加密/解密信息等访问入口。
- 客户端:通过指定的配置中心获取配置资源,推荐用git来存储配置文件。
1.3 解决的问题
- 集中管理配置文件。
- 不同的环境对应不同的配置,动态化的更新配置。
- 不需要在每个服务部署的机器上编写配置文件,让服务中心统一为服务拉取配置文件。
- 将配置信息以REST接口形式暴露。
2 案例
2.1 服务端
- 新建github远程仓库,如:spring-cloud-config。
将新建的远程仓库克隆到本地,新建date-dev.yml配置文件,将该文件上传至远程仓库。
date:
description: today is 2018-10-31保存为UTF-8格式
新建config server服务模块,加入依赖。
org.springframework.cloud
spring-cloud-config-server
appliacation.yml配置文件。
server:
port: 2233
spring:
application:name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/chenyulin19930815/spring-cloud-config.git #GitHub上面的git仓库名字
username: xxx #git仓库用户名
password: xxxx #github仓库密码
编写主启动类,加入@EnableConfigServer注解。
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServerApplication2233 {public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication2233.class, args);
}
}
启动服务,访问配置文件。
有以下访问规则:
- {application}:配置文件的文件名
- {profile}:读取的环境
- {lable}:分支
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
本示例有以下访问方式。
- http://localhost:2233/date-dev.yml
- http://localhost:2233/master/date-dev.yml
- http://localhost:2233/date-dev.yml/master
- http://localhost:2233/date-dev.yml
2.2 客户端
在本地仓库新建spring-cloud-config-client.yml上传至远程仓库。
spring:
profiles:active: dev
server:
port: 2244
spring:
profiles: dev
application:name: spring-cloud-config-client
server:
port: 2245
spring:
profiles: test
application:name: spring-cloud-config-client
新建config client服务模块,导入依赖。
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
新建bootstrap.yml1配置文件。
spring:
cloud:config:
name: spring-cloud-config-client #从git仓库读取的配置文件资源名称,没有后缀
profile: dev
label: master
uri: http://localhost:2233 #本微服务启动后先去找配置中心服务端,以获取GitHub的服务地址
新建application.yml
spring:
application:name: spring-cloud-config-client
启动类。
@SpringBootApplication
public class SpringCloudConfigClientApplication2244 {public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigClientApplication2244.class, args);
}
}
web测试controller。
@RestController
public class ConfigController {@Value("${spring.application.name}")
private String applicationName;
@Value("${spring.profiles}")
private String springProfiles;
@Value("${server.port}")
private String port;
@RequestMapping("/config")
public String getConfig() {
return "applicationName: " + applicationName +"<br/>springProfiles:"+springProfiles+"<br/>port: " + port;
}
}
测试:依次启动config server,config client服务。
首先,dev启动的端口为2244,访问:http://localhost:2244/config
然后,更改bootstrap.yml中profile: dev为profile: test,端口切换为了2245,访问:http://localhost:2245/config
遗留问题
将spring-cloud-config-client.yml文件中test的spring.application.name=spring-cloud-config-client改为spring.application.name=spring-cloud-config-client-test,再次访问发现名字没有更换,说明修改github配置信息后,系统不会主动去获取。
看源码点这里
- 一,bootstrap.yml比application.yml具有更高的优先级。
二,bootstrap.yml是系统级的资源配置项,application.yml是用户级的资源配置项。
三,SpringCloud会创建”BootStrap Context”作为”ApplicationContext”的父上下文。初始化的时候BootStrap Context负责从外部源加载配置属性并解析。这两个上下文共享一个”Environment”,BootStrap 具有更高优先级,他们不会被本地配置覆盖。 ↩︎
还没有评论,来说两句吧...