Spring 启动配置客户端未从配置服务器获取 属性
Spring Boot Config Client is not getting property from Config Server
我能够启动配置服务器,将我的浏览器指向 http://localhost:8888/example/default,得到:
{
"name": "example",
"profiles": [
"default"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/example.yaml",
"source": {
"example.mykey": "myvalue"
}
}
]
}
我假设以上应该足以看到配置服务器已启动并且 运行,它包含 "example.mykey" 属性,我打算在示例中使用它客户端应用程序。 (让我知道这里是否还需要配置服务器内部组件)。
客户端应用如下:
@SpringBootApplication
public class ExampleClientConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleClientConfigApplication.class, args);
}
@RestController
class ExampleController {
@Value("${example.mykey}")
private String value;
@RequestMapping
public String sayValue() {
return value;
}
}
}
客户端应用程序在其 src/main/resources 目录中也有一个 bootstrap.yaml:
spring:
application:
name: example
cloud:
config:
enabled: true
uri: http://localhost:8888
据我了解,上述客户端应用程序配置为使用来自配置服务器的 'example' 应用程序的 'default' 配置文件配置。然而,此应用程序失败并显示:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'example.mykey' in value "${example.mykey}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
为什么它不能从配置服务器获取这个 属性?我错过了什么?
=== 更新。根据评论中的要求添加客户端 pom.xml ===
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jnetx</groupId>
<artifactId>example-config-client</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<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>
</project>
在白费了一整天之后,我终于尝试了一些让它起作用的东西。服务器配置 uri 属于 spring.config.uri
而不是非工作 spring.cloud.config.uri
(在大多数示例中给出)。也许这在 spring boot 2.0.5 中已经改变,但是这个 yaml 有效:
spring:
application:
name: example
config:
enabled: true
uri: http://localhost:8888
遇到了类似的问题,您缺少依赖项 org.springframework.cloud:spring-cloud-starter-config
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
然后继续yml
spring:
application:
name: example
cloud:
config:
enabled: true
uri: http://localhost:8888
我能够启动配置服务器,将我的浏览器指向 http://localhost:8888/example/default,得到:
{
"name": "example",
"profiles": [
"default"
],
"label": null,
"version": null,
"state": null,
"propertySources": [
{
"name": "classpath:/example.yaml",
"source": {
"example.mykey": "myvalue"
}
}
]
}
我假设以上应该足以看到配置服务器已启动并且 运行,它包含 "example.mykey" 属性,我打算在示例中使用它客户端应用程序。 (让我知道这里是否还需要配置服务器内部组件)。
客户端应用如下:
@SpringBootApplication
public class ExampleClientConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleClientConfigApplication.class, args);
}
@RestController
class ExampleController {
@Value("${example.mykey}")
private String value;
@RequestMapping
public String sayValue() {
return value;
}
}
}
客户端应用程序在其 src/main/resources 目录中也有一个 bootstrap.yaml:
spring:
application:
name: example
cloud:
config:
enabled: true
uri: http://localhost:8888
据我了解,上述客户端应用程序配置为使用来自配置服务器的 'example' 应用程序的 'default' 配置文件配置。然而,此应用程序失败并显示:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'example.mykey' in value "${example.mykey}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
为什么它不能从配置服务器获取这个 属性?我错过了什么?
=== 更新。根据评论中的要求添加客户端 pom.xml ===
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jnetx</groupId>
<artifactId>example-config-client</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<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>
</project>
在白费了一整天之后,我终于尝试了一些让它起作用的东西。服务器配置 uri 属于 spring.config.uri
而不是非工作 spring.cloud.config.uri
(在大多数示例中给出)。也许这在 spring boot 2.0.5 中已经改变,但是这个 yaml 有效:
spring:
application:
name: example
config:
enabled: true
uri: http://localhost:8888
遇到了类似的问题,您缺少依赖项 org.springframework.cloud:spring-cloud-starter-config
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
然后继续yml
spring:
application:
name: example
cloud:
config:
enabled: true
uri: http://localhost:8888