分布式配置中心(Config-Server)
前面博文中生产者provider工程中的配置文件全都是配置在本项目中,这样会带来什么问题?
微服务最大的一个特色是集群部署,试想一下如果某个服务部署了上百台集群节点,那么当我们需要修改某个配置属性的值我们就要每个节点都需要修改一次,这样的开销工作量是不可接受的。那么我们有什么办法解决上述问题呢?
Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。
在本文中,我们将学习如何构建一个基于Git存储的分布式配置中心,并对客户端进行改造,并让其能够从配置中心获取配置信息并绑定到代码中的整个过程。
准备配置仓库
-
准备一个git仓库,可以Github上创建都可以。比如本文准备的仓库示例:https://github.com/hua-qiqi/cloud-config/blob/master/config/
-
我们以之前讲述的服务提供者工程
feign-provider为例
,那么我们可以在git仓库中该项目的默认配置文件feign-provider.properties
:
info.profile=default
构建配置中心
通过Spring Cloud Config来构建一个分布式配置中心非常简单,只需要三步:
- 创建一个基础的Spring Boot工程,命名为:
config-server-git
,并在pom.xml
中引入下面的依赖(省略了parent和dependencyManagement部分):
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
创建Spring Boot的程序主类,并添加@EnableConfigServer
注解,开启Spring Cloud Config的服务端功能。
@EnableConfigServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
- 在
application.properties
中添加配置服务的基本信息以及Git仓库的相关信息,例如:spring.application.name=config-server server.port=1201 eureka.client.serviceUrl.defaultZone=http://localhost:1002/eureka/,http://localhost:1001/eureka/ spring.server.port=7010 # 配置git仓库地址 spring.cloud.config.server.git.uri=https://github.com/hua-qiqi/cloud-config # 配置仓库路径 spring.cloud.config.server.git.search-paths=config # 配置仓库的分支 spring.cloud.config.label=master # 访问git仓库的用户名 spring.cloud.config.server.git.username=**** # 访问git仓库的用户密码 如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写 spring.cloud.config.server.git.password=****
到这里,使用一个通过Spring Cloud Config实现,并使用Git管理配置内容的分布式配置中心就已经完成了。我们可以将该应用先启动起来,确保没有错误产生,然后再尝试下面的内容。
完成了这些准备工作之后,我们就可以通过浏览器、POSTMAN或CURL等工具直接来访问到我们的配置内容了。访问配置信息的URL与配置文件的映射关系如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties
对应的配置文件,其中{label}
对应Git上不同的分支,默认为master。我们可以尝试构造不同的url来访问不同的配置内容,比如,要访问master分支,config-client应用的dev环境,就可以访问这个url:http://localhost:1201/config-server/master
,并获得如下返回:
info.profile=default
说明配置中心搭建完成,接着我们需要生成者工程feign-provider从配置中心读取属性值。
微服务读取配置文件
在feign-provider工程的pom.xml
中引入下述依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 创建
bootstrap.yml
配置替代原来的applicition配置,因为按照springboot的加载顺序bootstrap配置最先被加载,也只有这样配置文件才能被正确加载
,来指定获取配置文件的config-server-git
位置,例如:
spring:
application:
name: feign-provider
cloud:
config:
uri: http://localhost:1201/
profile: default
label: master
server:
port: 1004
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1002/eureka/,http://localhost:1001/eureka/
- 修改conrtoller代码为:
package com.lyh.provider.controller;
import com.lyh.provider.bean.censusRegisterInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@Value("${info.profile}")
private String value;
@RequestMapping(method = RequestMethod.GET, value = "/queryNameById")
String queryNameById(@RequestParam(value = "id") Long id){
//模拟后端数据库查询操作
return id+value;
}
}
- 启动配置中心、生产者、消费者项目,访问链接http://localhost:1003/queryNameById?id=2
- 得到如下结果 default说明一个基本的配置中心服务搭建完成。
- 修改配置中心值为default1,再访问链接,得到的结果还是default,这是为什么呢?我们应该如何解决这个问题?