原文: https://howtodoinjava.com/spring-cloud/spring-cloud-config-server-git/

现在,微服务方法已成为任何新 API 开发的行业标准,并且几乎所有组织都在推广这种方法。 Spring Cloud 提供了出色的工具,可以在 Spring 启动框架之上构建这些微服务。

在本 Spring Cloud 配置教程中,我们将讨论称为配置服务器的特定微服务功能。 配置服务器是存储和维护所有微服务的所有可配置参数的位置。

这更像是将属性/资源文件从项目代码库外部完全外部化到外部服务,以便对任何给定属性的任何更改都不需要重新部署使用该属性的服务。 所有此类属性更改都将反映出来,而无需重新部署微服务。

1. 为什么要使用 Spring Cloud 配置服务器

配置服务器的思想来自 12 因子应用程序宣言,该宣言与开发现代云原生应用程序的最佳实践指南有关。 它建议将属性或资源文件从服务器外部化,在服务器中,这些资源或资源的值在运行时会发生变化 – 通常情况下,不同的配置在每种环境中都会有所不同。

例如,假设一个服务依赖于另一服务(针对特定业务场景调用),并且该依赖服务的 URL 是否更改为其他内容。 然后,通常我们需要使用更新的 URL 构建和部署我们的服务。 现在,如果我们采用 12 要素应用程序方法,并且从外部服务读取了这些配置属性,那么我们只需要更新配置服务器中的 URL 并刷新该客户端服务配置即可使用更新的 URL。

因此,该想法是显而易见且有效的。 现在让我们来看如何创建 Spring Cloud 配置服务器。

2. 技术栈

我们将使用基于 spring boot 的 spring cloud API,该 API 随时可用且非常受欢迎。 在 Spring 框架命名中,它称为配置服务器。 另外,我们将使用 git 配置来托管属性文件。

因此,最后,此演示的技术栈为:

  1. Java 1.8
  2. Eclipse IDE
  3. Spring Cloud
  4. Spring Boot
  5. Spring Rest
  6. GitHub 作为资源库
  7. Maven
  8. REST 客户端

首先,我们将使用 spring boot 开发两个微服务。

  1. 一个是配置服务器服务,在运行时提供配置
  2. 一种是配置客户端服务,使用作为配置服务器公开的配置。

3. 服务器配置

首先,按照给定的步骤构建配置服务器部分:

  1. 生成项目结构


spring boot 初始化器页面开始,这是创建任何基于 spring boot 的应用程序的一个很好的起点。 在这里,我们将仅选择配置服务器启动器 pom。 屏幕截图是这样的。 使用此配置,一旦生成项目,便会下载一个 zip 文件,解压缩后将其导入 eclipse 中。
集成 Git 的 Spring Cloud 配置服务器 - 图1
使用配置服务器启动器 POM 生成服务器项目

  1. 在 Eclipse 中导入项目


从 spring 初始化器页面获得 zip 文件后,我们需要将其解压缩到我们选择的目录中,然后将其导入作为 Eclipse 作为 Maven 项目。
pom.xml

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  4. <java.version>1.8</java.version>
  5. <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
  6. </properties>
  7. <dependencyManagement>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.springframework.cloud</groupId>
  11. <artifactId>spring-cloud-dependencies</artifactId>
  12. <version>${spring-cloud.version}</version>
  13. <type>pom</type>
  14. <scope>import</scope>
  15. </dependency>
  16. </dependencies>
  17. </dependencyManagement>
  18. <dependencies>
  19. <dependency>
  20. <groupId>org.springframework.cloud</groupId>
  21. <artifactId>spring-cloud-config-server</artifactId>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-actuator</artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-test</artifactId>
  30. <scope>test</scope>
  31. </dependency>
  32. </dependencies>
  1. 启动 Eclipse


下一步将是从命令提示符处或从 eclipse 运行mvn clean install,无论您喜欢什么。 在这一步中,所有必需的依赖项将从 Maven 存储库中下载。 确保从没有下载限制的任何网络上尝试该操作。 非常需要成功完成此步骤才能继续进行下一步。

  1. 添加@EnableConfigServer注解


现在,打开 spring 已经提供的SpringApplication类,并在类之前添加@EnableConfigServer注解,然后再次构建项目。 有了此注解,此工件将充当 spring 配置服务器。
添加此注解后,类将如下所示 – 根据您在生成时提供的项目名称,类名称可以不同。 您也可以手动将类名称更改为您喜欢的名称。
SpringConfigServerApplication.java

  1. package com.howtodoinjava.example.springconfigserver;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.config.server.EnableConfigServer;
  5. @EnableConfigServer
  6. @SpringBootApplication
  7. public class SpringConfigServerApplication
  8. {
  9. public static void main(String[] args)
  10. {
  11. SpringApplication.run(SpringConfigServerApplication.class, args);
  12. }
  13. }
  1. git 存储库中的客户端属性


下一步是创建本地 git 存储库。 通过稍后在属性文件中配置其 URL,可以轻松地将其转换为远程存储库。 我们将放置外部属性文件configuration,配置服务器微服务将使用该文件来提供属性的外部配置。 我们需要按照以下步骤创建本地 git 存储库并签入示例属性文件。

  1. 确保您的计算机中安装了 git shell,并且可以从命令提示符运行 git bash。 要验证它,请打开命令提示符并键入git,如果它可以识别该命令,则可能安装了 git 提示符;如果没有,请访问 git 网站,按照说明下载并安装。

  2. 现在,在桌面中创建目录config-server-repo

  3. 然后在config-server-repo目录中创建文件config-server-client.properties文件,并在其中添加消息msg = Hello world - this is from config server

  4. 然后在config-server-repo目录中创建另一个文件config-server-client-development.properties文件,并在其中添加消息msg = Hello world - this is from config server – Development environment.

  5. 然后在config-server-repo目录中创建另一个文件config-server-client-production.properties文件,并在其中添加消息msg = Hello world - this is from config server – Production environment.

  6. 在这里,我们为不同的环境维护相同的属性名称,因为我们通常为不同的环境(例如 url,凭据,数据库详细信息等)维护属性。这里最重要的一点是,我们需要在每个属性中在环境名称后附加连字符(-) 以便配置服务器可以理解。 另外,我们需要使用将在此之后创建的配置客户端服务名称来命名属性文件。

  7. 现在从config-server-repo目录中打开命令提示符,然后运行命令git init以将该目录作为 git 存储库。

  8. 现在运行git add .将所有内容添加到此仓库中。

  9. 最后,我们需要通过运行命令git commit –m "initial checkin"来提交属性文件。 这应该检入 git 存储库中的所有文件。 这是相同的命令提示符屏幕截图。
    集成 Git 的 Spring Cloud 配置服务器 - 图2
    Git 中的属性检入
    client-config.properties

    1. msg = Hello world - this is from config server - default profile


client-config-development.properties

  1. msg = Hello world - this is from config server - Development Environment


client-config-production.properties

  1. msg = Hello world - this is from config server - Prodcution Environment


properties文件夹中执行的 Git 命令

  1. $ git init
  2. $ git add .
  3. $ git commit -m "initial commit"
  1. 从配置服务器指向 Git 存储库


spring-config-sever项目的src\main\resources目录中创建一个名为bootstrap.properties的文件,然后添加以下行。
bootstrap.properties

  1. #Server port
  2. server.port = 8888
  3. #Git repo location
  4. spring.cloud.config.server.git.uri=E:\\devsetup\\gitworkspace\\spring-cloud\\config-git-repo
  5. #Verify any repository issue in service startup
  6. spring.cloud.config.server.git.cloneOnStart=true
  7. #Disable security of the Management endpoint
  8. management.security.enabled=false


现在让我们了解这些属性。

  • server.port定义嵌入式服务器将在其上启动的端口。
  • spring.cloud.config.server.git.uri将绑定 git 位置以查找配置。 在这里,我们使用本地 git 仓库,但只需更改此位置即可将其切换到远程位置。
  • management.security.enabled=false将在/env/refresh等管理点上禁用 Spring Security。这是用于开发设置的,应启用生产安全性。

mvn clean install

  1. 验证配置


在嵌入式模式下运行服务的命令是java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar,但是我们将在测试部分中重新访问它。
要检查配置服务器是否可以识别属性,请首先使用项目代码库位置的命令提示符下的给定命令,从命令提示符下运行配置服务器微服务。

  1. java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar


现在打开浏览器并检查下面的 URL,它将返回 JSON 输出,在propertySources部分中,我们可以看到我们在属性中添加的所有属性。 这可以确保config-server成功运行,它已经识别了 git 位置,并且正在为不同环境提供配置。

  • http://localhost:8888/client-config/development
  • http://localhost:8888/client-config/production

git add .``git commit -m "test"

4. 客户端配置

现在,我们将进行客户端实现,在此我们将使用来自单独的微服务的那些属性,这是我们的最终目标 – 将配置外部化为不同的服务。

  1. 创建 Maven 项目


转到 https://start.spring.io/ Web 页面,并使用以下选定的工件生成客户端项目:

  1. 执行器
  2. 配置客户端
  3. 网页
  4. 其余存储库

.zip``Spring-Config-Server `pom.xml````java

UTF-8 UTF-8 1.8 Hoxton.RELEASE org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import

org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-data-rest org.springframework.boot spring-boot-starter-web

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-test</artifactId>
  4. <scope>test</scope>
  5. </dependency>

  1. ![](https://cdn.nlark.com/yuque/0/2020/jpg/348784/1599984157129-40baebb6-9e4a-4c35-9d63-80ab75b4e9fa.jpg#alt=Generate%20Client%20Project%20with%20Listed%20Dependencies)
  2. 2.
  3. <a name="e01dde7b"></a>
  4. #### 创建 REST 资源
  5. <br />添加一个[`RestController`](https://howtodoinjava.com/spring/spring-boot/spring-boot-tutorial-with-hello-world-example/)以查看响应中的服务器端属性值。 为此,请打开已生成的[`@SpringBootApplication`](https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-using-springbootapplication-annotation.html)类文件,并将以下小类添加到该文件的末尾。 这非常简单明了,我们只是在`/message` URL 处公开一种方法,我们将只返回由配置服务器微服务提供的`msg`的属性值,该属性值已配置到本地 git 存储库(将会迁移到生产环境中的远程 git 存储库!)。
  6. <br />`SpringConfigClientApplication.java`
  7. ```java
  8. package com.howtodoinjava.example.springconfigclient;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.boot.SpringApplication;
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;
  13. import org.springframework.cloud.context.config.annotation.RefreshScope;
  14. import org.springframework.core.env.Environment;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.RestController;
  17. @SpringBootApplication
  18. public class SpringConfigClientApplication {
  19. public static void main(String[] args) {
  20. SpringApplication.run(SpringConfigClientApplication.class, args);
  21. }
  22. @Autowired
  23. public void setEnv(Environment e)
  24. {
  25. System.out.println(e.getProperty("msg"));
  26. }
  27. }
  28. @RefreshScope
  29. @RestController
  30. class MessageRestController {
  31. @Value("${msg:Config Server is not working. Please check...}")
  32. private String msg;
  33. @GetMapping("/msg")
  34. public String getMsg() {
  35. return this.msg;
  36. }
  37. }
  1. 与配置服务器绑定


src\main\resources目录中创建一个名为bootstrap.properties的文件,并添加以下属性以与配置服务器以及一些必需的配置连接。
bootstrap.properties

  1. spring.application.name=client-config
  2. #Active Profile - will relate to development properties file in the server.
  3. #If this property is absent then,default profile will be activated which is
  4. #the property file without any environment name at the end.
  5. spring.profiles.active=development
  6. # N.B. this is the default:
  7. spring.cloud.config.uri=http://localhost:8888
  8. management.security.enabled=false


现在让我们了解属性。

  • spring.application.name只是将要部署的微服务的应用程序名称。
  • spring.cloud.config.uri是提及配置服务器 URL 的属性。 请注意,我们的配置服务器在端口8888上运行; 通过打开 spring 配置服务器代码库的application.properties文件进行验证,然后检查server.port=8888
  • management.security.enabled=false将在诸如/env/refresh等管理端点上禁用 Spring Security。这是用于开发设置的,应启用生产安全性。
    1. 验证客户端配置


这是我们需要在配置客户端执行的,而不是在此项目上执行最终的mvn clean install命令,以便正确编译所有内容并将其打包在目标文件夹以及本地 Maven 存储库中。 我们将与服务器端一起启动配置客户端服务,最后我们将测试该功能。

5. 演示

让我们测试配置服务器应用程序。

  • 生成并运行配置服务器项目
    spring-config-server文件夹中打开命令提示符,然后运行mvn clean install命令。 一旦构建完成,就可以通过java -jar命令(例如java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar)从该命令提示符本身运行应用程序。
    这将在本地主机的 8888 端口中启动配置服务器服务。

  • 生成并运行配置客户端项目
    同样,从spring-config-client文件夹中打开命令提示符,然后运行mvn clean install命令。 一旦构建完成,就可以通过java -jar命令(例如java -jar target\spring-config-client-0.0.1-SNAPSHOT.jar)从该命令提示符本身运行应用程序。
    这将在localhost的 8080 端口中启动配置客户端服务。

  • 测试 REST 端点
    现在,在浏览器中,通过浏览 URL http://localhost:8080/msg打开/msg REST 端点。 它应该返回config-server-client-development.properties文件中提到的Hello world - this is from config server
    集成 Git 的 Spring Cloud 配置服务器 - 图3
    REST 端点

  • 测试属性更改
    现在,我们将进行属性更改,并测试该更改是否可以反映在配置客户端服务中,而无需重新启动任何微服务。
    进行一些更改,在config-server-client-development.properties 中的msg属性的值中,并在本地 git 中签入,然后在浏览器中再次单击http://localhost:8080/msg,您将只使用旧值。
    为了反映新值,我们需要通过从任何 REST 客户端使用POST方法命中http://localhost:8080/refresh端点来刷新配置
    成功刷新配置客户端服务后,新值应反映在服务响应中。 这是因为@RefreshScope注解了我们已经公开的 Rest 控制器。

6. 检查是否遇到任何错误的东西

  • 属性文件名称和客户端模块服务名称spring.application.name = config-server-client应该完全相同,否则,将不会检测到属性。 实际上,配置服务器在属性文件名的端点公开属性,如果浏览URLhttp://localhost:8888/config-server-client/development,它将返回所有 Dev 环境值。
    集成 Git 的 Spring Cloud 配置服务器 - 图4
    所有 Dev 属性视图

  • 确保如上所述使用git init/add/commit命令在 git 仓库中签入属性文件。

  • 通过任何 REST 客户端调用http://localhost:8080/refreshPOST方法,确保已刷新客户端服务环境。 否则,更改后的值将不会反映在客户端服务中。

  • 确保在启动配置客户端服务时,配置服务器服务已在运行。 否则,注册可能会花费一些时间,这可能会在测试时造成混乱。

这就是为微服务创建配置服务器。 如果您在配置本文中提到的所有要点时遇到任何困难,请添加评论,我们很乐意调查问题。

配置服务器源码

配置客户端源码

下载 Git 仓库

学习愉快!