简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
Spring Cloud Config致力于为分布式系统中的外部化配置提供支持 ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
一个配置中心提供的核心功能
- 提供服务端和客户端支持
- 集中管理各环境的配置文件
- 配置文件修改之后,可以快速的生效
- 可以进行版本管理
- 支持大的并发查询
- 支持各种语言
Spring Cloud Config可以完美的支持以上所有的需求。
使用Config实现的配置中心-配置文件存储在git
-
创建配置中心的服务端
添加pom依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
主程序中添加注解@EnableConfigServer,启用Config Server作为配置中心
@SpringBootApplication @EnableDiscoveryClient @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
修改application.properties配置文件
spring.application.name=config-server server.port=8888 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka spring.cloud.config.server.git.uri=https://github.com/xiaoyuer2012/springcloud/ // 配置git仓库地址 spring.cloud.config.server.git.searchPaths=config-data // 配置查找配置的路径
测试
启动应用,访问http://localhost:8888/springcloud-test/dev,应能看到如下输出内容,说明服务启动成功。
{"name":"springcloud-test","profiles":["dev"],"label":null,"version":"d607a1236eb206221915cc3bdca470eef6f7b32d","state":null,"propertySources":[{"name":"https://github.com/xiaoyuer2012/springcloud//config-data/springcloud-test-dev.properties","source":{"hello":"hello,world!"}}]}
-
集成配置中心的客户端
添加pom依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
主应用程序不需要做特殊的修改
修改application.properties
server.port=8082 spring.application.name=msa-weather-city-service eureka.client.service-url.defaultZone = http://localhost:8761/eureka/ spring.cloud.config.profile = dev spring.cloud.config.uri=http://localhost:8888 spring.cloud.config.name=springcloud-test
测试代码
@RestController public class HelloController { @Value("${hello}") private String hello; // 获取配置文件的hello变量值 @RequestMapping("/hello") public String hello() { return hello; } }
启动客户端程序,访问 http://localhost:8082/hello,可看到如下截图,即为配置文件中配置的内容, config集成成功。
使用Config实现的配置中心-配置文件存储在本地
配置中心放置到本地与放置到服务器的搭建步骤是基本一致的,只是config-server配置中心服务端的配置有些区别,具体见下图。
spring.application.name=config-server server.port=8888 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka # 配置文件存储在git #spring.cloud.config.server.git.uri=https://github.com/xiaoyuer2012/springcloud/ #spring.cloud.config.server.git.searchPaths=config-data # 配置文件存储在本地 # 本地文件 spring.profiles.active=native #本地文件访问路径 spring.cloud.config.server.native.searchLocations=classpath:/config
将本地的配置放开,同时在相应的目录放置配置文件即可。修改完重启服务,访问http://localhost:8888/spring-test-dev.properties,可以看到下面的内容,即代表配置成功。
{"name":"springcloud-test","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/config/springcloud-test-dev.properties","source":{"hello":"hello,world!"}}]}
记得目录下面放置配置文件
思考
今天,我们把Spring Cloud的Config组件进行了集成,到目前为止,我们的程序已经集成了Eureka服务注册与发现,Zuul服务网关以及Config配置中心,我们已经基本实现了分布式系统的负载均衡和配置的统一管理。但是如下的问题还是需要我们思考一下:
如果某个服务因为逻辑原因或者异常原因,导致长时间无返回怎么办。大量的请求积压过来,会不会导致服务的崩溃?
配置中心的配置如果发生了变化,我们就需要重启服务吗?
这些问题Spring Cloud都提供了相应的机制去解决这些问题。我们将在后面进行详细的描述。