赞
踩
对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护。
微服务的配置管理一般有以下需求:
Spring Cloud Config主要是为了分布式系统的外部配置提供了服务器端和客户端的支持,只要体现为Config Server和Config Client两部分。由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此Spring Cloud Config很适合Spring应用程序。
Config Server是一个可横向扩展的,集中式的配置服务器,它用于集中管理应用程序各个环境下配置,默认使用Git存储配置内容。
Config Client是一个Config Server的客户端,用于操作存储在Config Server上的配置属性,所有微服务都指向Config Server,启动的时候会请求它获取所需要的配置属性,然后缓存这些属性以提高性能。
1、在远程Git仓库中新建一个配置文件
application-dev.properties
application.properties
application-dev.properties内容如下:
profile=dev-1.0
application.properties内容如下:
profile=default-1.0
2、创建项目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 http://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.1.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.springclouddemo</groupId>
- <artifactId>config-server</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>config-server</name>
- <description>Config Server</description>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
-
- <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>${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>

3、在启动类添加@EnableConfigServer注解
- package com.springclouddemo.configserver;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.config.server.EnableConfigServer;
-
- /**
- * @author 何昌杰
- */
- @EnableConfigServer
- @SpringBootApplication
- public class ConfigServerApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(ConfigServerApplication.class, args);
- }
-
- }

4、编写application配置文件
- server.port=7400
- spring.application.name=config-server
- spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
- #spring.cloud.config.server.git.username=
- #spring.cloud.config.server.git.password=
这样,一个Config Server就完成了。
测试:
我们可以使用Config Server的端点获取配置内容。端点与配置文件的映射规则如下:
- /{application}/{profile}[/{label}]
-
- /{application}-{profile}.yml
-
- /{label}/{application}-{profile}.yml
-
- /{application}-{profile}.properties
-
- /{label}/{application}-{profile}.properties
application: 表示微服务的虚拟主机名,即配置的spring.application.name
profile: 表示当前的环境,dev,test or production?
label: 表示git仓库分支,master or relase or others repository name?默认是master
如果没有找到,就会找默认的
例如:
http://localhost:7400/application/dev
我们已经知道如何使用Config Server端点获取配置内容,这一节我们讨论微服务如何获取配置信息。
1、创建项目config-client
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 http://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.1.6.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.springclouddemo</groupId>
- <artifactId>config-client</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>config-client</name>
- <description>Config Client</description>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
-
- <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>${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>

2、编写application配置文件
server.port=7401
3、编写bootstrap.properties配置文件
- spring.application.name=application
- spring.cloud.config.uri=http://localhost:7400/
- spring.cloud.config.profile=dev
- spring.cloud.config.label=master
第三步属性配置应在bootstrap上,而不是application中。如果在applicaton中该部分配置将不能正常工作。
Spring Cloud有一个“引导上下文”的概念,这里是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载的application.*中的属性不同,引导上下文加载bootstrap.*中的属性。配置在bootstrap.*中的属性有更高的优先级,因此默认情况下他们不能被本地覆盖。
4、编写controller
编写controller/DemoController.java
- package com.springclouddemo.configclient.controller;
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author 何昌杰
- */
- @RestController
- public class DemoController {
- @Value("${profile}")
- private String profile;
-
- @GetMapping("/demo")
- public String demo(){
- return this.profile;
- }
- }

项目结构:
测试:
前文中,使用spring.cloud.config.server.git.uri指定一个Git仓库,事实上,该属性非常灵活
Config Server的占位符支持{application}、{profile}、{label}
例如:
- server.port=7400
- spring.application.name=config-server
- spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/{application}
使用这种方式,即可轻松支持一个应用对应一个Git仓库。同理,也可支持一个profile对应一个Git仓库
模式匹配指的是带有通配符的{application}/{profile}名称的列表。如果{application}/{profile}不匹配任何模式,将使用这个配置项spring.cloud.config.server.git.url定义的URL。
- spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
- spring.cloud.config.server.git.repos.simple=https://gitee.com/simpler/config-server
- spring.cloud.config.server.git.repos.special.pattern=special*/dev,*special*/dev*
- spring.cloud.config.server.git.repos.special.uri=https://gitee.com/simpler/config-server
对simple仓库,它匹配所以配置文件。对special仓库,它只匹配所以配置文件中名为special的配置文件。
很多场景下,可能吧配置文件放在Git仓库的子目录下,此时可以使用search-path指导,search-path同样支持占位符
- spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
- spring.cloud.config.server.git.search-paths=app,bar*
这样Config Server就会在Git仓库跟目录,app目录,以及所有bar开头的子目录查找配置文件
默认情况下,在配置被首次请求时,Config Server才会clone Git仓库,也可以让Config Server在启动时就clone Git仓库。
- spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
- spring.cloud.config.server.git.repos.simple.uri=https://gitee.com/simpler/config-server
- spring.cloud.config.server.git.repos.simple.pattern=application-*
- spring.cloud.config.server.git.repos.simple.clone-on-start=true
这样就可以在Config Server启动时clone指定Git仓库了,当然也可以使用spring.cloud.config.server.git.repos.clone-on-start=true进行全局配置。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。