我能够启动配置服务器,将浏览器指向 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
据我了解,上述客户端应用程序配置为使用配置服务器中的“示例”应用程序的“默认”配置文件配置。然而,这个应用程序失败了:
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>
最佳答案
面临类似的问题,您缺少依赖项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
关于spring-boot - Spring Boot 配置客户端未从配置服务器获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52409248/