3

我正在尝试使用 spring-cloud-consul 从 Consul KV 商店获取价值

密钥存在: curl http://localhost:8500/v1/kv/config/HelloWorldClient/testKey [{"LockIndex":0,"Key":"config/HelloWorldClient/testKey","Flags":0,"Value" :"Y29uc3VsVGVzdEtleQ==","CreateIndex":2748497,"ModifyIndex":2748497}]

这是我的 bootstrap.yml

spring:
  application:
    name: HelloWorldClient
  cloud:
    consul:
      config:
        enabled: true
        format: YAML
        prefix: config
        default-context: apps
        fail-fast: true
        data-key: data
      host: localhost
      port: 8500
      enabled: true

样品控制器:

@EnableDiscoveryClient
@Controller
@EnableConfigurationProperties(PropertySourceBootstrapProperties.class)
public class ConsulValuesGetter {

    @Value("${testKey}")
    String testKeyValue;

    @Autowired
    private Environment env;

    @GetMapping("/getKey")
    public String helloClient() {

        System.out.println("Test KEY= " + null + "ENV: " +  env.toString());
        return env.toString();
    }
}

我收到错误:创建名为“consulValuesGetter”的 bean 时出错:注入自动装配的依赖项失败;嵌套异常是 java.lang.IllegalArgumentException:无法解析值“${testKey}”中的占位符“testKey”

我看到这个条目:位于的属性源:CompositePropertySource [name='consul', propertySources=[ConsulPropertySource [name='config/HelloWorldClient/'], ConsulPropertySource [name='config/application/']]]

马文有:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-all</artifactId>
     </dependency>

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-consul-dependencies</artifactId>
                <version>1.2.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
</dependencyManagement>

请帮助解决这个问题。谢谢你。

4

0 回答 0