查看原文
其他

如何使用 Spring Config Server

Darren Luo SpringForAll社区 2020-10-17

详细了解如何在你的应用程序中创建远程配置。

这次,我们将使用 Spring Cloud Config 包为我们的应用程序进行远程配置。

这个想法主要是让我们的程序可以将配置移动到外部位置,这样我们的应用程序可以轻松配置甚至修改其设置。

这被广泛用于微服务中。同一的服务或应用程序可以在不同的容器中多次启动,有趣的是,他们可以读取一个中心位置配置这些服务。

为此,我们将创建一个配置服务器和一个读取该服务器配置的客户端。配置服务器使用 GitHub 上的一个我们存储了配置文件的 GIT 仓库。

应用程序数据如下:

配置服务器

  • 项目:config-server

  • 端口:8888

  • Spring 名称:config-server

  • GIT服务器:https://github.com/chuchip/servercloudconfig.git

配置客户端

  • 项目:config-client

  • 端口:8080

  • Spring 名称:config-client

可以在这里找到程序源码。

配置服务器

你只需要在你的 Maven 项目中引入该依赖。

  1. <dependency>

  2.     <groupId>org.springframework.cloud</groupId>

  3.     <artifactId>spring-cloud-config-server</artifactId>

  4. </dependency>

Spring Starter 的名字是 Config Server

服务器配置由单个类组成,详细信息如下:

  1. package com.profesorp.configserver;


  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 ConfigServerApplication {


  8.    public static void main(String[] args) {

  9.        SpringApplication.run(ConfigServerApplication.class, args);

  10.    }

  11. }

如例子所示,这里唯一要强调的就是 @EnableConfigServer 标签。

在 application.properties 文件里,需要配置参数:spring.cloud.config.server.git.uri:

  1. spring.application.name=config-server

  2. server.port=8888


  3. spring.cloud.config.server.git.uri=https://github.com/chuchip/servercloudconfig.git

在本例中,我们使用 GitHub 上托管的服务器 Git。我们也可以指定使用本地的 GIT 仓库,如下所示:

  1. spring.cloud.config.server.git.uri=file://eclipe/spring-config/

Spring Cloud 配置服务器支持以下后端:

  • GIT

  • Vault

  • JDBC

这些后端甚至可以混合使用,这取决于选择的配置文件,他们将使用其中一个。但这超出了本文的范围。

在本里中使用的 GIT 服务器,重要的是有一个使用客户端名的文件,其中包含请求的数据。这个文件将使用后缀 .properties

因此,如果我们想要为一个名为 config-client 的客户端应用程序进行配置,也就是说变量 spring.application.name 等于 config-client,我们必须由一个名为 config-client.properties 的文件。该文件的内容将是:

  1. datosservidor.minimum=11

  2. datosservidor.maximum=20

  3. limites.minimum=-1

  4. limites.maximum=2

  5. valores.valor_fijo: VALORFIJO

  6. valores.valor_funcion: "VALORDEFUNCION";

注意,可以使用 := 指定属性值。

不要使用引号来分隔文字,除非你想要我们的文字(字符串)也包含这些引号。

要查看我们的客户端值,我们将发出一个 GET 请求指定客户端名称和配置文件。

在这种情况下,如果你没有指定任何配置文件,我们会访问客户端配置 config-clientdefault 配置文件。

要查看 production 配置文件的设置,你应该调用 URL: http://localhost:8888/config-client/production,它显示以下输出:

这展示了 config-client-production.properties 的文件内容,然后是 config-client.properties 的文件内容。

因此,如果变量在所需的配置文件中存在,则返回该值。否则,它查看默认配置文件,如果有的话则返回指定的值。

客户端设置

一旦我们的配置服务器开始工作,我们将创建客户端。

添加此依赖到我们的 Maven 文件:

  1. <dependency>

  2.   <groupId>org.springframework.cloud</groupId>

  3.   <artifactId>spring-cloud-starter-config</artifactId>

  4. </dependency>

使用 Spring Initializr 会添加 Config Client 依赖。此外,要热更新配置,需要添加 starter Actuator。

现在,我们为应用程序配置指定监听配置服务器的位置。首先,我们将通过 bootstrap.properties 更改 config.properties 文件的名称。因此,Spring Boot 知道寻找服务器配置。

在此文件中,我们添加 spring.cloud.uri 属性,指定我们服务器配的 URL。

  1. spring.application.name=config-client

  2. spring.cloud.config.uri=http://localhost:8888

  3. #spring.profiles.active=production

  4. management.endpoints.web.exposure.include=refresh

我们还设定 management.endpoints.web.exposure.include 属性为 refresh 来配置包执行程序,以便你可以访问 URL: http://localhost:8080/actuator/refresh 来强制我们刷新配置。

请记住, spring.application.name 属性设置了应用程序名称,并指示在 GIT 仓库中寻找设置的文件名称。

使用 spring.profiles.active 变量,我们将指出使用哪个配置文件。如果我们不放任何内容(注释行也一样),使用的配置文件将是默认配置文件。

在本文里中,我使用了几种方法来读取配置。

1. 创建包含 @ConfigurationProperties 标签的组件

用这个最简单的方法,我们指定要读取的属性的根,然后定义需要 Spring 填充的变量。

configuration.java 类中,我们指定我们要配置的以 limites 开头的变量。

  1. import org.springframework.boot.context.properties.ConfigurationProperties;

  2. import org.springframework.stereotype.Component;

  3. import lombok.Data;


  4. @Data

  5. @Component

  6. @ConfigurationProperties("limites")

  7. public class Configuration {

  8.    private int minimum;

  9.    private int maximum;    

  10. }

因此, dato1 变量将具有 limites.dato1 指定的值。

如果你的 limites.dato1 的值不能转成整数,我们的应用程序会出错;但是,如果你没有找到该值,只是不会填充它而不会给出任何错误。

组件将通过 @Autowired 标签注入。

  1. @Autowired

  2. private Configuration configuration;

2. 使用 @Value 注解创建变量

这样,也可以读取服务器配置的值。最大的区别是该值将被固定,因为它将被分配运行该应用程序,永不刷新。

  1. @Value("${valores.valor_fijo}")

  2. String valorFijo;

valorFijo 变量将分配 valores.valor_fijo 行的值。

3. 在函数的参数中使用 @Value 条目

同样,从服务器配置中读取该值,优点是可以刷新该值。

  1. @GetMapping("/refrescado")

  2. public BeanConfiguration getConfiguracionRefrescada(@Value("${valores.valor_funcion}") String valorFuncion)

  3.    { .... }

运行

在我们的示例中,公开了 URL /limitedrefrescadodatos

调用 limites 返回此输出:

如果我们修改 config-client.properties 文件并在我们的 GIT 仓库服务器中 pushcommit 这样的值,对此 URL 进行连续调用都将显示旧数据,因为客户端只在开始时读取配置,除非你强制他刷新数据。

想象一下,我们更改了 ‘config-client.properties’ 文件,我们现在有这些值:

  1. datosservidor.minimum=10

  2. datosservidor.maximum=20

  3. limites.minimum=-101

  4. limites.maximum=201

  5. valores.valor_fijo: OTROVALORFIJO

  6. valores.valor_funcion: "OTROVALORDEFUNCION"

作相应的 commitpush

  1. > git commit -a -m "cambiada configuracion";git push

当你使用 POST 类型请求调用 URL http://localhost: 8080/actuator/refresh,Spring 强制他们调用服务器配置并刷新值。

如图所示,该请求的输出返回刷新的变量。

现在,如果我们调用 http://localhost: 8080/limits,我们可以看到 minResultadosmaxResultados 已经改变了。然而, valorFijovalorFuncion 都没有改变。

如果我们调用 URL http://localhost: 8080/refrescado,会看到变量 valorfuncion 已经更新,调用放置了 @Value 标签,此时变量被读取。但是,变量 valorFijo 不会改变在程序开始时确定的值,它的值保持不变。

一个重要的概念是,如果我们在我们的配置文件中移除了变量,变量的值不会为 null。相反,它保留了先前确定的值。无论是用 @Value 读取变量,还是使用 @ConfigurationPropertiesBean,都是如此。


原文链接:https://dzone.com/articles/using-spring-config-server

作者:Jesus J. Puente

译者:Darren Luo


推荐: SpringBoot WebFlux 入门案例

上一篇:使用Kubernetes和Spring&nbsp;Boot进行自我修复的应用程序

关注公众号

点击原文阅读更多



    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存