Reading properties from Spring Cloud Config Server

本指南解释了你的 Quarkus 应用程序如何从 @ [14] 中在运行时读取配置属性。

Prerequisites

Unresolved directive in spring-cloud-config-client.adoc - include::{includes}/prerequisites.adoc[]

Solution

我们建议你按照下一节中的说明逐步创建应用程序。

Stand up a Config Server

要建立本指南所需的 Config Server,请遵循 @ [18] 中概述的说明。该过程的最终结果是一个正在运行的 Config Server,它将在应用程序查询服务器被命名为 @ [17] 时为名为 @ [16] 的配置属性提供 @ [15] 值。

Creating the Maven project

首先,我们需要一个新项目。使用以下命令创建一个新项目:

Unresolved directive in spring-cloud-config-client.adoc - include::{includes}/devtools/create-app.adoc[]

此命令生成一个导入 @ [19] 扩展名的项目。

如果你已配置了 Quarkus 项目,则可以通过在项目基础目录中运行以下命令向项目添加 @ [20] 扩展名:

Unresolved directive in spring-cloud-config-client.adoc - include::{includes}/devtools/extension-add.adoc[]

这会将以下内容添加到构建文件中:

pom.xml
<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-spring-cloud-config-client</artifactId>
</dependency>
build.gradle
implementation("io.quarkus:quarkus-spring-cloud-config-client")

GreetingController

首先,在 @ [22] 文件中创建一个简单的 @ [21] Jakarta REST 资源,如下所示:

package org.acme.spring.spring.cloud.config.client;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "hello";
    }
}

由于我们想要使用从 Config Server 获取的配置属性,因此我们将更新 @ [23] 以注入 @ [24] 属性。更新后的代码看起来像这样:

package org.acme.spring.spring.cloud.config.client;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.config.inject.ConfigProperty;

@Path("/hello")
public class GreetingResource {

    @ConfigProperty(name = "message", defaultValue="hello default")
    String message;

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return message;
    }
}

Configuring the application

quarkus.spring-cloud-config 根下,Quarkus 提供了各种配置旋钮。为了本指南的目的,我们的 Quarkus 应用程序将配置在 application.properties 如下所示:

# use the same name as the application name that was configured when standing up the Config Server
quarkus.application.name=a-bootiful-client
# enable retrieval of configuration from the Config Server - this is off by default
quarkus.spring-cloud-config.enabled=true
# configure the URL where the Config Server listens to HTTP requests - this could have been left out since http://localhost:8888 is the default
quarkus.spring-cloud-config.url=http://localhost:8888

如果您使用的是 Gradle,则 Gradle 设置 rootProject.name 优先于 quarkus.application.name,因此请务必将 Gradle 属性设置为希望 Spring Cloud Config 服务器看到的应用程序名称。

Package and run the application

使用以下内容运行应用程序:

Unresolved directive in spring-cloud-config-client.adoc - include::{includes}/devtools/dev.adoc[]

用浏览器打开 [role="bare"][role="bare"]http://localhost:8080/greeting.

结果应为 Hello world,因为它是从 Spring Cloud Config 服务器获取的值。

Run the application as a native executable

您当然可以使用 Building a native executable guide 的说明创建一个本机映像。

Spring Cloud Config Client Reference

Unresolved directive in spring-cloud-config-client.adoc - include::{generated-dir}/config/quarkus-spring-cloud-config-client.adoc[]