从 Spring Cloud 配置服务器中读取配置属性
该指南阐述了你的Quarkus应用程序如何在运行时从 Spring Cloud Config Server 中读取配置属性。
先决条件
完成这个指南,你需要:
-
大概15分钟
-
编辑器
-
JDK 17+ installed with
JAVA_HOME
configured appropriately -
Apache Maven 3.9.9
-
如果你愿意的话,还可以选择使用Quarkus CLI
-
如果你想构建原生可执行程序,可以选择安装Mandrel或者GraalVM,并正确配置(或者使用Docker在容器中进行构建)
建立一个配置服务器
建立该指南所需的 配置服务器 (Config Server),请遵循 这里 的说明。该过程的最终目的是创建一个正在运行的且应用程序查询名为 a-bootiful-client
的服务器时,会为名为 message
的配置属性提供值为 Hello world
的配置服务器。
创建Maven项目
首先,我们需要一个新的项目。使用以下命令创建一个新项目:
For Windows users:
-
If using cmd, (don’t use backward slash
\
and put everything on the same line) -
If using Powershell, wrap
-D
parameters in double quotes e.g."-DprojectArtifactId=spring-cloud-config-quickstart"
该命令生成了一个已导入 spring-cloud-config-client
扩展的项目。
如果你已经配置了你的Quarkus项目,你可以通过在你的项目根目录下运行以下命令,将 spring-cloud-config-client
扩展添加到你的项目中:
quarkus extension add spring-cloud-config-client
./mvnw quarkus:add-extension -Dextensions='spring-cloud-config-client'
./gradlew addExtension --extensions='spring-cloud-config-client'
这会在你的构建文件中添加以下内容:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-spring-cloud-config-client</artifactId>
</dependency>
implementation("io.quarkus:quarkus-spring-cloud-config-client")
GreetingController
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)中获取到的配置属性,我们将更新 GreetingResource
以注入 message
属性。更新后的代码将如下所示:
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;
}
}
配置该应用程序
Quarkus在 quarkus.spring-cloud-config
根路径下提供了多种配置选项。就该指南而言,我们的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
If you are using Gradle, the Gradle setting |
打包并运行该应用程序
使用以下命令运行该应用程序:
quarkus dev
./mvnw quarkus:dev
./gradlew --console=plain quarkusDev
打开你的浏览器并访问 http://localhost:8080/greeting。
结果应该是: Hello world
,因为这是从Spring Cloud 配置服务器(Config Server)上获取的值。
以本地可执行文件的方式运行该应用程序
当然,你也可以使用 构建本地可执行文件指南 的说明来创建一个本地镜像。
Spring Cloud 配置客户端参考
Configuration property fixed at build time - All other configuration properties are overridable at runtime
Configuration property |
类型 |
默认 |
---|---|---|
If enabled, will try to read the configuration from a Spring Cloud Config Server Environment variable: Show more |
boolean |
|
If set to true, the application will not stand up if it cannot obtain configuration from the Config Server Environment variable: Show more |
boolean |
|
The Base URI where the Spring Cloud Config Server is available Environment variable: Show more |
string |
|
Name of the application on Spring Cloud Config server. Could be a list of names to load multiple files (value separated by a comma) Environment variable: Show more |
string |
|
The label to be used to pull remote configuration properties. The default is set on the Spring Cloud Config Server (generally "master" when the server uses a Git backend). Environment variable: Show more |
string |
|
The amount of time to wait when initially establishing a connection before giving up and timing out. Specify Environment variable: Show more |
|
|
The amount of time to wait for a read on a socket before an exception is thrown. Specify Environment variable: Show more |
|
|
The username to be used if the Config Server has BASIC Auth enabled Environment variable: Show more |
string |
|
The password to be used if the Config Server has BASIC Auth enabled Environment variable: Show more |
string |
|
TrustStore to be used containing the SSL certificate used by the Config server Can be either a classpath resource or a file system path Environment variable: Show more |
path |
|
Password of TrustStore to be used containing the SSL certificate used by the Config server Environment variable: Show more |
string |
|
KeyStore to be used containing the SSL certificate for authentication with the Config server Can be either a classpath resource or a file system path Environment variable: Show more |
path |
|
Password of KeyStore to be used containing the SSL certificate for authentication with the Config server Environment variable: Show more |
string |
|
Password to recover key from KeyStore for SSL client authentication with the Config server If no value is provided, the key-store-password will be used Environment variable: Show more |
string |
|
When using HTTPS and no keyStore has been specified, whether to trust all certificates Environment variable: Show more |
boolean |
|
Custom headers to pass the Spring Cloud Config Server when performing the HTTP request Environment variable: Show more |
Map<String,String> |
|
The profiles to use for lookup Environment variable: Show more |
list of string |
|
Microprofile Config ordinal. Environment variable: Show more |
int |
|
About the Duration format
To write duration values, use the standard You can also use a simplified format, starting with a number:
In other cases, the simplified format is translated to the
|