浅析 Spring Cloud Config 分布式配置中心

    下午抽空看了下Spring Cloud Config搭建配置中心,过程中有点坑,不过总算过来了,和大家分享一下。

    了解Spring Boot+Spring Cloud的微服务框架的都应该知道,它的出现不是为了单个的服务,而是为了更多的服务而产生的,也正是这种需求,在开发中,服务配置的管理尤为重要,毕竟这是可以不断累加的,越来越多的服务配置需要管理,一着不慎,就可能出现很多错误,而且也不方便,这也是我们所不想看到的。在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件:Spring Cloud Config。

    Spring Cloud Config组件支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client,下面我来说下配置在远程Git仓库的实现。

    开发环境:Windows

    开发工具:Eclipse_oxygen(不必统一,IDEA更好),Maven

    1、构建Config Server

    创建Maven项目,名为EurekaConfigServer,几个配置文件如下:

    pom.xml配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>EurekaConfigServer</groupId>
	<artifactId>EurekaConfigServer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>EurekaConfigServer</name>
	<description>Demo For EurekaConfigServer</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--config server -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- spring boot test -->
		<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>Dalston.SR4</version><!-- 对所依赖jar包进行版本管理的管理器 -->
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

    这里要注意几点,不然后面运行会有问题。首先添加必要的依赖项:

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

    然后注意dependencyManagement中version的版本和parent中version版本,如果是其他的版本,在运行程序时可能会报错,当然这不是唯一的,就像我之前parent的版本是1.4.7就出错了,改成1.5.4就好了,这是在运行http://localhost:8888/config-client/dev时报错的,错误如下:org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfiguration,后来查看了对应的jar包才发现原来的版本里没有这个类,所以换成有这个类的版本就好了(祝脱坑)。

    application.properties配置:

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/forezp/SpringcloudConfig/
#配置仓库路径
spring.cloud.config.server.git.searchPaths=respo
#配置仓库的分支
spring.cloud.config.label=master
#访问git仓库的用户名(公共仓库可不填)
#spring.cloud.config.server.git.username=
#访问git仓库的用户密码(公共仓库可不填)
#spring.cloud.config.server.git.password=

    这里的公共仓库里已新建了一个配置文件:config-client-dev.properties,主要是为了读取里面的属性。

http请求地址和资源文件映射如下:

/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties

    Application.java代码:

@SpringBootApplication
@EnableConfigServer   //开启配置服务器功能 
public class ConfigServerApplication{

   public static void main(String[] args){
     SpringApplication.run(ConfigServerApplication.class,args);
   }
}

     运行Application,访问http://localhost:8888/config-client-dev.properties,可得到如下配置:

democonfigclient.message: hello spring io
foo: foo version 2

    这是git仓库中新建的配置文件里的属性,当然也可以这样访问http://localhost:8888/config-client/dev,结果如下:

{"name":"config-client","profiles":["dev"],"label":null,"version":"a68876a6211369bae723348d5f8c3defe4a55e04",
"state":null,"propertySources":[{"name":"https://github.com/forezp/SpringcloudConfig/respo/config-client-dev.properties",
"source":{"democonfigclient.message":"hello spring io","foo":"foo version 2"}}]}

    2、构建config client

    同理,创建Maven项目,名为EurekaConfigClient,几个配置文件如下:

    pom.xml配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>EurekaConfigClient</groupId>
  <artifactId>EurekaConfigClient</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>EurekaConfigClient</name>
  <description>Demo For EurekaConfigClient</description>
  
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--config -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<!-- spring boot test -->
		<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>

	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR4</version><!-- 对所依赖jar包进行版本管理的管理器 -->
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

    这里注意一下和config server配置里的不同。

    application.properties配置:

spring.application.name=config-client
server.port=8889

#配置服务中心网址
spring.cloud.config.uri=http://localhost:8888/
#dev开发环境配置文件,test测试环境,pro正式环境
spring.cloud.config.profile=dev
#远程仓库的分支
spring.cloud.config.label=master

    application.java代码:

@SpringBootApplication
@RestController
public class ConfigClientApplication{

   public static void main(String[] args){
     SpringApplication.run(ConfigClientApplication.class,args);
   }
   
   @Value("${foo}")
   String foo;
   @RequestMapping(value="/hi")
   public String hi() {
      return foo;
   }
}

    这里写一个程序的入口类,API接口“/hi”,返回从配置中心读取的foo变量的值,运行程序访问http://localhost:8889/hi,结果如下:

foo version 2

    可以看到,可以成功读取了远程Git仓库中的配置信息了。

文章来源:

Author:海岸线的曙光
link:https://my.oschina.net/u/3747963/blog/1594729