Spring Cloud 与 Git 集成的Config Server

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(768)   2023-03-28 11:29:14

演示项目源码下载:(访问密码:9987)
spring-cloud-config-server-git.zip

微服务方法现在已经成为任何新 API 开发的行业标准,几乎所有组织都在推广它。Spring cloud 提供了优秀的工具来在 Spring boot 框架之上构建这些微服务。

在这个Spring Cloud 配置教程中,我们将讨论一个名为Config Server的特定微服务功能。配置服务器是存储和维护所有微服务的所有可配置参数的地方。

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

1. 为什么要使用 Spring Cloud Config Server

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

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

所以,这个想法是显而易见的和有效的。现在让我们看看如何创建 spring cloud config server
 

2. 技术栈

我们将使用现成且非常流行的基于 spring-boot 的 spring-cloud API。它在 spring 框架命名法中称为配置服务器。此外,我们将使用 git 配置来托管属性文件。
所以最后,我们这个演示的技术栈将是:
  1. Java 1.8
  2. Eclipse IDE
  3. Spring cloud
  4. Spring boot
  5. Spring Rest
  6. GitHub as resource repository
  7. Maven
  8. REST client
首先,我们将使用 spring boot 开发两个微服务。
  1. 一个是配置服务器服务,在运行时提供配置
  2. 一种是配置客户端服务,使用作为配置服务器公开的配置。
 

3.配置服务器配置

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

1.生成项目结构

从spring boot 初始化器门户开始,这是创建任何基于 spring boot 的应用程序的一个很好的起点。这里我们将只选择Config server starter pom。屏幕截图是这样的。有了这个配置,一旦我们生成了项目,就会下载一个zip文件,解压后我们在eclipse中导入。创建项目

2.在 Eclipse 中导入项目

从 spring 初始值设定项门户获得 zip 文件后,我们需要将其解压缩到我们选择的目录中,并将其作为 maven 项目导入到 eclipse 中。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
 
<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>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3.在eclipse中构建

下一步将是mvn clean install从命令提示符或 eclipse 运行任何你喜欢的东西。在这一步中,所有必需的依赖项都将从 maven repo 下载。确保您是从没有下载限制的任何网络尝试它。在此步骤中成功构建非常需要继续下一步。
 

4.添加@EnableConfigServer 注解

现在打开spring已经提供的Spring Application类,@EnableConfigServer在类前添加注解,再次构建项目。有了这个注解,这个工件就会像一个 spring 配置服务器。

添加此注释后,类将如下所示 - 类名可能会有所不同,具体取决于您在生成时提供的项目名称。您也可以手动将类名更改为您喜欢的名称。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
 
@EnableConfigServer
@SpringBootApplication
public class SpringConfigServerApplication 
{
    public static void main(String[] args) 
    {
        SpringApplication.run(SpringConfigServerApplication.class, args);
    }
}

5.git 存储库中的客户端属性

下一个重要步骤是创建本地 git 存储库。稍后可以通过在属性文件中配置其 URL 轻松将其转换为远程存储库。我们将放置外部属性文件[configuration],Config 服务器微服务将使用该文件来提供属性的外部配置。我们需要按照以下步骤创建本地 git 存储库并签入示例属性文件。
  1. 确保您的机器上安装了 git shell,并且您可以从命令提示符运行 git bash。要验证它,请打开命令提示符并键入git,如果它识别该命令,那么您可能已经安装了 git 提示符,如果没有,请按照 git 网站,按照说明下载并安装。
  2. 现在在您的桌面中创建一个目录config-server-repo
  3. 然后config-server-client.properties在 config-server-repo 目录中创建一个文件file 并在那里添加消息msg = Hello world - this is from config server
  4. 然后config-server-client-development.properties在 config-server-repo 目录中创建另一个文件文件并在那里添加消息msg = Hello world - this is from config server – Development environment.
  5. 然后config-server-client-production.properties在 config-server-repo 目录中创建另一个文件文件并在那里添加消息msg = Hello world - this is from config server – Production environment.
  6. 这里我们为不同的环境维护相同的属性名称,因为我们通常为不同的环境维护属性,如 url、凭据、数据库详细信息等。 这里最重要的一点是我们需要在每个属性中附加连字符 (-) 和环境名称以便配置服务器理解它。此外,我们需要使用我们将在此之后创建的配置客户端服务名称命名属性文件。
  7. 现在从config-server-repo目录打开命令提示符并运行命令git init将该目录作为 git 存储库。
  8. 现在运行git add .以将所有内容添加到此 repo。
  9. 最后,我们需要通过运行命令来提交属性文件git commit –m "initial checkin"。这应该检查 git 存储库中的所有文件。这是相同的命令提示符屏幕截图。cmd
$title(client-config.properties)
msg = Hello world - this is from config server - default profile
 
$title(client-config-development.properties)
msg = Hello world - this is from config server - Development Environment
 
$title(client-config-production.properties)
msg = Hello world - this is from config server - Prodcution Environment
 
$ git init
 
$ git add .
 
$ git commit -m "initial commit"

6.从配置服务器指向 Git 存储库

创建一个bootstrap.properties在项目src\main\resources目录中调用的文件spring-config-sever并添加以下行。
$title(bootstrap.properties)
#Server port
server.port = 8888
 
#Git repo location
spring.cloud.config.server.git.uri=E:\\devsetup\\gitworkspace\\spring-cloud\\config-git-repo
 
#Verify any repository issue in service startup
spring.cloud.config.server.git.cloneOnStart=true
 
#Disable security of the Management endpoint
management.security.enabled=false
现在让我们了解这些属性。所以,这一步将指向一个 git 位置和服务器端口。
以上所有步骤都是我们需要在配置服务器端做的,现在mvn clean install在这个项目上做一个最终的命令,这样一切都被正确编译并打包到目标文件夹以及本地 maven 存储库中。一旦我们准备好客户端部分,我们将启动配置服务器服务,我们将最终测试该功能。
  • server.port 定义嵌入式服务器将启动的端口。
  • spring.cloud.config.server.git.uri将绑定 git 位置以查找配置。这里我们使用本地 git repo,但只需更改此位置即可切换到远程获取位置。
  • management.security.enabled=false 将在 /env、/refresh 等管理节点上禁用 spring 安全性。这是用于开发设置,在生产中应该启用安全性。

7.验证配置

java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar但是,我们将在测试部分重新讨论以嵌入模式运行服务的命令。

要检查配置服务器是否可以识别属性,请首先使用项目代码库位置的命令提示符中的给定命令从命令提示符运行配置服务器微服务。
java -jar target\spring-config-server-0.0.1-SNAPSHOT.jar
现在打开浏览器并检查下面的 URL,它将返回JSON输出,在propertySources部分我们可以看到我们在属性中添加的所有属性。这确保了 config-server 成功运行,它已经识别了 git 位置,并且它正在为不同的环境提供配置服务。还要检查属性文件中的任何更改是否在不重新启动的情况下由服务器反映 - 更改任何环境的属性和签入属性文件的值。然后运行该特定环境的端点,并验证是否应立即反映更改的属性值,而无需重新启动服务器。
要进行 git check in,在进行更改并通过任何文本编辑器保存文件后,运行命令git add .git commit -m "test"
  • http://localhost:8888/client-config/development
  • http://localhost:8888/client-config/production
 

4. Config 客户端配置

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

创建 Maven 项目

转到https://start.spring.io/门户网站并使用以下选定的工件生成客户端项目:
  1. Actuator
  2. Config Client
  3. Web
  4. Rest Repositories
生成前屏幕将如下所示;一旦我们点击生成,我们将获得.zip文件下载选项。和 Spring-Config-Server 一样,将文件解压到某个目录下,然后导入到 eclipse 中。
$title(pom.xml)
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>
 
<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>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
项目构建
 

创建 REST 资源

添加一个RestController以查看响应中的服务器端属性值。为此,请打开@SpringBootApplication已生成的类文件,并在该文件的末尾添加以下小类。这是非常简单和直接的,我们只是在/messageURL 处暴露一个方法,我们将在其中返回msg配置服务器微服务提供的属性值,该微服务被配置到本地 git 存储库(它将被迁移到一个生产中的远程 git 存储库!)。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@SpringBootApplication
public class SpringConfigClientApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(SpringConfigClientApplication.class, args);
    }
     
    @Autowired
    public void setEnv(Environment e)
    {
        System.out.println(e.getProperty("msg"));
    }
}
 
@RefreshScope
@RestController
class MessageRestController {
 
    @Value("${msg:Config Server is not working. Please check...}")
    private String msg;
 
    @GetMapping("/msg")
    public String getMsg() {
        return this.msg;
    }
}

与配置服务器绑定

创建一个bootstrap.propertiessrc\main\resources目录中调用的文件并添加以下属性以连接配置服务器以及一些必需的配置。
$title(bootstrap.properties)
spring.application.name=client-config
 
#Active Profile - will relate to development properties file in the server. 
#If this property is absent then,default profile will be activated which is 
#the property file without any environment name at the end. 
spring.profiles.active=development
 
# N.B. this is the default:
spring.cloud.config.uri=http://localhost:8888
 
management.security.enabled=false
现在让我们了解这些属性。
  • spring.application.name 只是将要部署的微服务的应用程序名称。
  • spring.cloud.config.uri是提及配置服务器 url 的属性。请注意,我们的配置服务器正在端口上运行8888;通过打开application.propertiesspring 配置服务器代码库的文件来验证它并检查server.port=8888.
  • management.security.enabled=false将禁用管理端点上的 spring 安全性,例如/env/refresh等等。这是用于开发设置,在生产中应该启用安全性。

验证客户端配置

这是我们需要在 config 客户端做的很多事情,而不是mvn clean install在这个项目上做最后的命令,这样一切都被正确编译并打包在目标文件夹以及本地 maven 存储库中。我们将与服务器端一起启动配置客户端服务,我们将最终测试该功能。
 

5. 演示

让我们测试配置服务器应用程序。
  • 构建和运行配置服务器项目

    spring-config-server文件夹打开命令提示符并运行mvn clean install命令。构建完成后,通过java -jarjava -jar target\spring-config-server-0.0.1-SNAPSHOT.jar.
    这将在本地主机的 8888 端口中启动配置服务器服务。
  • 构建和运行配置客户端项目

    同样,从spring-config-client文件夹打开命令提示符并运行mvn clean install命令。构建完成后,通过java -jarjava -jar target\spring-config-client-0.0.1-SNAPSHOT.jar.
    这将在 localhost 的 8080 端口中启动 Config Client 服务。
  • 测试 REST 端点

    现在在浏览器中/msg通过浏览 url打开其余端点http://localhost:8080/msg。它应该返回文件中Hello world - this is from config server提到的config-server-client-development.properties内容。
    演示结果
  • 测试属性变化

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

6. 遇到错误时需要检查的事项

  • 属性文件名和客户端模块服务名spring.application.name=config-server-client要完全一致,否则检测不到属性。实际上,Config Server 在属性文件名的端点中公开属性,如果您浏览 URL http://localhost:8888/config-server-client/development,它将返回所有开发环境值。属性图
  • 确保您已使用git init/add/commit上述命令检入 git 存储库中的属性文件。
  • 确保您已通过调用任何 REST 客户端的POST方法刷新了客户端服务环境http://localhost:8080/refresh。否则更改的值将不会反映在客户端服务中。
  • 确保在启动配置客户端服务时,配置服务器服务已经在运行。否则,注册可能需要一些时间,这可能会在测试时造成混乱。


 

演示项目源码下载:(访问密码:9987)
spring-cloud-config-server-git.zip

地址:https://www.leftso.com/article/862.html

相关阅读

演示项目源码下载:(访问密码:9987)spring-cloud-config-server-git.zip微服务方法现在已经成为任何新 API 开发的行业标准,几乎所有组织都在推广它
在spring cloud项目中配置配置服务的地址spring.cloud.config.uri不生效的解决办法,spring cloud
随着Spring Cloud 的越来越流行,国内很多公司也在开始使用该框架了
在这个 Spring cloud 教程中,学习在 spring boot/cloud 项目中使用 Netflix Ribbon 使用客户端负载平衡
演示项目源码下载:(访问密码:9987)Spring-Cloud-discovery-server.zip 了解如何创建微服务的基础上,Spring Cloud,对Netflix的Eureka注...
错误信息 spring cloud 启动报错 No spring.config.import property has been defined 环境说明 spring ...
我们知道spring boot可以通过文件名来区分配置,如下:application.ymlapplication-dev.yml #开发环境application-test.yml #测试环境...
演示项目源码下载:(访问密码:9987)spring-cloud-zipkin.zipZipkin是非常有效的工具分布追踪在微服务生态系统
项目源码下载:(访问密码:9987)Spring-Cloud-Circuit-Breaker.zip学习在调用底层微服务的同时利用调用的Spring Cloud Netflix堆栈组件之一Hys...
演示项目源码下载:(访问密码:9987)Spring-Cloud-Consoul.zip了解如何创建微服务的基础上Spring cloud,对登记HashiCorp Consul注册服务器,以及...