无法将 consul 键值属性读入 Spring Boot 应用程序

问题描述

我对领事很陌生。从 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 代理:

consul agent -server -bootstrap-expect=1 -data-dir=consul-data -ui -bind= -client 0.0.0.0

我在 consul UI 中将键值对配置为 config/keyvalue/greeting:

key-value config

当我运行 Spring Boot 应用程序时出现错误

引起:java.lang.IllegalArgumentException:无法解析值“${greeting}”中的占位符“greeting”

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)