我对领事很陌生。从 POC 开始,能够使用 consul 建立两个服务之间的通信,但无法将 consul 中的键/值对读取到 spring boot 应用程序中。
浏览了互联网上所有可用的解决方案以了解我做错了什么,但没有任何帮助。
请看一下下面的代码,如果我做错了,请向我提出建议
主类:
@SpringBootApplication
@EnableAutoConfiguration
@RestController
@EnableConfigurationProperties(value = {PropertySourceBootstrapProperties.class})
@EnableDiscoveryClient
public class KeyvalueApplication {
@Value("${greeting}")
private String greeting;
@GetMapping("/keys")
public String getName() {
return "Config data "+ greeting;
}
public static void main(String[] args) {
SpringApplication.run(KeyvalueApplication.class, args);
}
}
应用程序.yml
server:
port: 8676
spring:
application:
name: keyvalue
cloud:
consul:
enabled: true
host: localhost
port: 8500
config:
fail-fast: true
enabled: true
prefix: config
default-context: keyvalue
profile-separator: '::'
watch:
enabled: true
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.keys</groupId>
<artifactId>keyvalue</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>keyvalue</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
我正在使用 consul 1.9.2 版我使用以下命令启动 consul 代理:
领事代理 -server -bootstrap-expect=1 -data-dir=consul-data -ui -bind= -client 0.0.0.0
我在 consul UI 中将键值对配置为 config/keyvalue/greeting:
当我运行我的 Spring Boot 应用程序时,出现错误:
原因:java.lang.IllegalArgumentException:无法解析值“${greeting}”中的占位符“greeting”