当前位置:   article > 正文

Spring Cloud Config 统一管理微服务配置(Config Server、Config Client搭建、Config Server Git仓库配置详解)_微服务configservice 部署

微服务configservice 部署

一、为什么要统一管理微服务配置

二、Spring Cloud Config介绍

三、编写Config Server

四、Config Server的端点

五、编写Config Client

六、Config Server的Git仓库配置详解

1、占位符支持

2、模式匹配

3、搜索目录

4、启动时加载配置文件


一、为什么要统一管理微服务配置

对于传统的单体应用而言,常使用配置文件来管理所有配置,比如SpringBoot的application.yml文件,但是在微服务架构中全部手动修改的话很麻烦而且不易维护。
微服务的配置管理一般有以下需求:

  • 集中配置管理,一个微服务架构中可能有成百上千个微服务,所以集中配置管理是很重要的。
  • 不同环境不同配置,比如数据源配置在不同环境(开发,生产,测试)中是不同的。
  • 运行期间可动态调整。
  • 配置修改后可自动更新。

二、Spring Cloud Config介绍

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,启动的时候会请求它获取所需要的配置属性,然后缓存这些属性以提高性能。

三、编写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如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.springclouddemo</groupId>
  12. <artifactId>config-server</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>config-server</name>
  15. <description>Config Server</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-config-server</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>${spring-cloud.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

3、在启动类添加@EnableConfigServer注解

  1. package com.springclouddemo.configserver;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. /**
  6. * @author 何昌杰
  7. */
  8. @EnableConfigServer
  9. @SpringBootApplication
  10. public class ConfigServerApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(ConfigServerApplication.class, args);
  13. }
  14. }

4、编写application配置文件

  1. server.port=7400
  2. spring.application.name=config-server
  3. spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
  4. #spring.cloud.config.server.git.username=
  5. #spring.cloud.config.server.git.password=

这样,一个Config Server就完成了。

 

测试:

  1. 运行项目config-server
  2. 访问http://localhost:7400/application-dev.yml,得到如下响应:
  3. 访问http://localhost:7400/application/dev,得到如下响应:

四、Config Server的端点

我们可以使用Config Server的端点获取配置内容。端点与配置文件的映射规则如下:

  1. /{application}/{profile}[/{label}]
  2. /{application}-{profile}.yml
  3. /{label}/{application}-{profile}.yml
  4. /{application}-{profile}.properties
  5. /{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

http://localhost:7400/application-dev.properties

http://localhost:7400/application-dev.yml

五、编写Config Client

我们已经知道如何使用Config Server端点获取配置内容,这一节我们讨论微服务如何获取配置信息。

1、创建项目config-client

pom.xml如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.springclouddemo</groupId>
  12. <artifactId>config-client</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>config-client</name>
  15. <description>Config Client</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.cloud</groupId>
  23. <artifactId>spring-cloud-starter-config</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>${spring-cloud.version}</version>
  37. <type>pom</type>
  38. <scope>import</scope>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. <build>
  43. <plugins>
  44. <plugin>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-maven-plugin</artifactId>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

2、编写application配置文件

server.port=7401

3、编写bootstrap.properties配置文件

  1. spring.application.name=application
  2. spring.cloud.config.uri=http://localhost:7400/
  3. spring.cloud.config.profile=dev
  4. spring.cloud.config.label=master
  • spring.application.name:对应Config Server所获取的配置文件中的{application}
  • spring.cloud.config.uri:指定Config Server的地址,默认http://localhost:8888
  • spring.cloud.config.profile:对应Config Server所获取的配置文件的{profile}
  • Spring.cloud.config.label:对应Git仓库的分支,对应Config Server所获取配置文件的{label}
  • 特别注意这里是bootstrap配置文件

第三步属性配置应在bootstrap上,而不是application中。如果在applicaton中该部分配置将不能正常工作。

Spring Cloud有一个“引导上下文”的概念,这里是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载的application.*中的属性不同,引导上下文加载bootstrap.*中的属性。配置在bootstrap.*中的属性有更高的优先级,因此默认情况下他们不能被本地覆盖。

4、编写controller

编写controller/DemoController.java

  1. package com.springclouddemo.configclient.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * @author 何昌杰
  7. */
  8. @RestController
  9. public class DemoController {
  10. @Value("${profile}")
  11. private String profile;
  12. @GetMapping("/demo")
  13. public String demo(){
  14. return this.profile;
  15. }
  16. }
  • @Value注解绑定Git仓库配置文件中的profile属性

项目结构:

 

测试:

  1. 启动项目config-server、config-client
  2. 访问http://localhost:7401/demo,得到响应如下:

六、Config Server的Git仓库配置详解

前文中,使用spring.cloud.config.server.git.uri指定一个Git仓库,事实上,该属性非常灵活

1、占位符支持

Config Server的占位符支持{application}、{profile}、{label}

例如:

  1. server.port=7400
  2. spring.application.name=config-server
  3. spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/{application}

使用这种方式,即可轻松支持一个应用对应一个Git仓库。同理,也可支持一个profile对应一个Git仓库

2、模式匹配

模式匹配指的是带有通配符的{application}/{profile}名称的列表。如果{application}/{profile}不匹配任何模式,将使用这个配置项spring.cloud.config.server.git.url定义的URL。

  1. spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
  2. spring.cloud.config.server.git.repos.simple=https://gitee.com/simpler/config-server
  3. spring.cloud.config.server.git.repos.special.pattern=special*/dev,*special*/dev*
  4. spring.cloud.config.server.git.repos.special.uri=https://gitee.com/simpler/config-server

对simple仓库,它匹配所以配置文件。对special仓库,它只匹配所以配置文件中名为special的配置文件。

3、搜索目录

很多场景下,可能吧配置文件放在Git仓库的子目录下,此时可以使用search-path指导,search-path同样支持占位符

  1. spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
  2. spring.cloud.config.server.git.search-paths=app,bar*

这样Config Server就会在Git仓库跟目录,app目录,以及所有bar开头的子目录查找配置文件

4、启动时加载配置文件

默认情况下,在配置被首次请求时,Config Server才会clone Git仓库,也可以让Config Server在启动时就clone Git仓库。

  1. spring.cloud.config.server.git.uri=https://gitee.com/jie_harris/config-server
  2. spring.cloud.config.server.git.repos.simple.uri=https://gitee.com/simpler/config-server
  3. spring.cloud.config.server.git.repos.simple.pattern=application-*
  4. 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进行全局配置。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/84101
推荐阅读
相关标签
  

闽ICP备14008679号