天天看点

Spring Cloud微服务架构:分布式配置中心

Spring Cloud Config是Spring Cloud团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密/解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于Spring构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于Spring Cloud Config实现的配置中心默认采用Git来存储配置信息,所以使用Spring Cloud Config构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过Git客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN仓库、本地化文件系统。

在本文中,我们将学习如何构建一个基于Git存储的分布式配置中心,并对客户端进行改造,并让其能够从配置中心获取配置信息并绑定到代码中的整个过程。

准备配置仓库

  • 准备一个git仓库,可以在码云或Github上创建都可以。比如本文准备的仓库示例:http://git.oschina.net/didispace/config-repo-demo
  • 假设我们读取配置中心的应用名为

    config-client

    ,那么我们可以在git仓库中该项目的默认配置文件

    config-client.yml

info:
  profile: default
           
  • 为了演示加载不同环境的配置,我们可以在git仓库中再创建一个针对dev环境的配置文件

    config-client-dev.yml

    : 
info:
  profile: dev
           

构建配置中心

通过Spring Cloud Config来构建一个分布式配置中心非常简单,只需要三步:

  • 创建一个基础的Spring Boot工程,命名为:

    config-server-git

    ,并在

    pom.xml

    中引入下面的依赖(省略了parent和dependencyManagement部分):
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.3.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>

</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
           
  • 创建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=7001

# git管理配置
spring.cloud.config.server.git.uri=http://git.oschina.net/didispace/SpringBoot-Learning/
spring.cloud.config.server.git.searchPaths=Chapter9-1-4/config-repo
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password
           
  • spring.cloud.config.server.git.uri:配置git仓库位置
  • spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个
  • spring.cloud.config.server.git.username:访问git仓库的用户名
  • spring.cloud.config.server.git.password:访问git仓库的用户密码

到这里,使用一个通过Spring Cloud Config实现,并使用git管理内容的配置中心已经完成了,启动该应用,成功后开始下面的内容。

Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性

spring.profiles.active=native

,Config Server会默认从应用的

src/main/resource

目录下检索配置文件。也可以通过

spring.cloud.config.server.native.searchLocations=file:F:/properties/

属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

服务端验证

为了验证上面完成的配置服务器,在http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/ 下创建了一个config-repo目录作为配置仓库,并根据不同环境新建了下面四个配置文件:

  • didispace.properties
  • didispace-dev.properties
  • didispace-test.properties
  • didispace-prod.properties

其中设置了一个from属性,为每个配置文件分别设置了不同的值,如:

  • from=git-default-1.0
  • from=git-dev-1.0
  • from=git-test-1.0
  • from=git-prod-1.0

为了测试版本控制,在master中,我们都加入1.0的后缀,同时创建一个config-label-test分支,并将各配置文件中的值用2.0作为后缀。

完成了这些准备工作之后,我们就可以通过浏览器或POSTMAN等工具直接来访问到我们的配置内容了。

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来访问不同的配置内容,比如:要访问config-label-test分支,didispace应用的prod环境,可以通过这个url:http://localhost:7001/didispace/prod/config-label-test

{
  "name": "didispace",
  "profiles": [
    "prod"
  ],
  "label": "config-label-test",
  "version": "19de8a25575a7054a34230f74a22aa7f5575a9d1",
  "propertySources": [
    {
      "name": "http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace-prod.properties",
      "source": {
        "from": "git-prod-2.0"
      }
    },
    {
      "name": "http://git.oschina.net/didispace/SpringBoot-Learning/Chapter9-1-4/config-repo/didispace.properties",
      "source": {
        "from": "git-default-2.0"
      }
    }
  ]
}
           

微服务端映射配置

在完成并验证了配置服务中心之后,下面看看我们如何在微服务应用中获取配置信息。

  • 创建一个Spring Boot应用,在pom.xml中引入spring-cloud-starter-config依赖,完整依赖关系如下:
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.3.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

	<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>

</dependencies>

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Brixton.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
           
  • 创建最基本的Spring Boot启动主类 
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}

}
           
  • 创建

    bootstrap.properties

    配置,来指定config server,例如: 
spring.application.name=didispace
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/

server.port=7002
           
  • spring.application.name:对应前配置文件中的{application}部分
  • spring.cloud.config.profile:对应前配置文件中的{profile}部分
  • spring.cloud.config.label:对应前配置文件的git分支
  • spring.cloud.config.uri:配置中心的地址

这里需要格外注意:上面这些属性必须配置在

bootstrap.properties

中,config部分内容才能被正确加载。因为config的相关配置会先于

application.properties

,而

bootstrap.properties

的加载也是先于

application.properties

  • 创建一个Rest Api来返回配置中心的from属性,具体如下:
@RefreshScope
@RestController
class TestController {

    @Value("${from}")
    private String from;

    @RequestMapping("/from")
    public String from() {

        return this.from;
    }

}
           

通过

@Value("${from}")

绑定配置服务中配置的from属性。

启动该应用,并访问:http://localhost:7002/from ,我们就可以根据配置内容输出对应环境的from内容了。

继续阅读