Reading properties from Spring Cloud Config Server

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

This guide explains how your Quarkus application can read configuration properties at runtime from the Spring Cloud Config Server.

Prerequisites

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

Solution

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

We recommend that you follow the instructions in the next sections and create the application step by step.

Stand up a Config Server

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

To stand up the Config Server required for this guide, please follow the instructions outlined here. The end result of that process is a running Config Server that will provide the Hello world value for a configuration property named message when the application querying the server is named a-bootiful-client.

Creating the Maven project

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

First, we need a new project. Create a new project with the following command:

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

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

This command generates a project which imports the spring-cloud-config-client extension.

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

If you already have your Quarkus project configured, you can add the spring-cloud-config-client extension to your project by running the following command in your project base directory:

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

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

This will add the following to your build file:

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 资源,如下所示:

First, create a simple GreetingResource Jakarta REST resource in the src/main/java/org/acme/spring/cloud/config/client/GreetingResource.java file that looks like:

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] 属性。更新后的代码看起来像这样:

As we want to use configuration properties obtained from the Config Server, we will update the GreetingResource to inject the message property. The updated code will look like this:

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 如下所示:

Quarkus provides various configuration knobs under the quarkus.spring-cloud-config root. For the purposes of this guide, our Quarkus application is going to be configured in application.properties as follows:

# 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 服务器看到的应用程序名称。

If you are using Gradle, the Gradle setting rootProject.name has precedence over quarkus.application.name so be sure to set the Gradle property to the application name you want the Spring Cloud Config server to see.

Package and run the application

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

Run the application with:

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

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

Open your browser to [role="bare"]http://localhost:8080/greeting.

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

The result should be: Hello world as it is the value obtained from the Spring Cloud Config server.

Run the application as a native executable

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

You can of course create a native image using the instructions of the Building a native executable guide.

More Spring guides

Spring Cloud Config Client Reference

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