一、简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
二、构建Config Server
创建一个spring-boot项目,取名为config-server,其pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>configserver</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>${spring-cloud.version}</version>
<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>
在程序的入口Application类加上 @EnableConfigServer注解 开启配置服务器的功能,代码如下:
@SpringBootApplication
@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.service-url.defaultZone=http://localhost:8761/eureka/
#服务的git仓库地址,末尾加或不加".git"都可
spring.cloud.config.server.git.uri=https://gitee.com/xiaoliu66007/springcloud /*替换成自己的git地址*/
#配置文件所在的目录
spring.cloud.config.server.git.searchPaths=/**/config /*替换成自己的git地址下的文件夹*/
#配置文件所在的分支
spring.cloud.config.label=master
spring.cloud.config.server.git.username=******* /*替换成git账号*/
spring.cloud.config.server.git.password=******* /*替换成git密码*/
注意事项:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
为了方便测试,我是在码云上自己上传了 config-server-dev.properties文件到config文件夹下,如图:
----------------------------------------------------------------------------注意事项完毕------------------------------------------------------------------------------------------------
config-server-dev.properties内容如下
foo = foo version 3
启动程序:访问http://localhost:8888/config-server/dev,其中的config-server和dev 参照下面的“http请求地址和资源文件映射:”
效果如下:
证明配置服务中心可以从远程程序获取配置信息。
接上面的注意事项,http请求地址和资源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
三、构建一个config client
重新创建一个springboot项目,取名为config-client,其pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties> <dependencies>
<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>${spring-cloud.version}</version>
<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>
其配置文件bootstrap.properties:
server.port=8881
spring.application.name=config-server
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
- spring.cloud.config.label 指明远程仓库的分支
-
spring.cloud.config.profile
- dev开发环境配置文件
- test测试环境
- pro正式环境
spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。
程序的入口类,如下:
/**
http://localhost:8881/hi可以访问到配置中心config-server,通过配置中心访问到远程git地址下的"config-server-dev.properties"(详见bootstrap.properties配置)
bootstrap.properties文件指定读取的文件为:
“【spring.application.name】- 【spring.cloud.config.profile】.properties”
即config-server-dev.properties
*/
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @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;
}
}
访问 http://localhost:8881/hi结果如下
foo version 3
这就说明,config-client从config-server获取了name和age的属性,而config-server是从git仓库读取的,如图
本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter6