使用Spring Cloud Consul无法读取远程属性

时间:2016-11-11 15:44:18

标签: spring spring-cloud consul

我们计划将众多.properties文件的内容移至Consul 我已经实现了一个带有以下定义的演示

@EnableAutoConfiguration
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableConfigurationProperties
public class Application {

    @Autowired
    private Environment env;

    @RequestMapping("/")
    public String home() {
        return env.getProperty("dc1/kv/property1/");
    } 
}

consul服务器以默认值

运行

当前env.getProperty("dc1/kv/property1/"); 不返回任何内容

我的pom看起来像这样

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<build>
    <plugins>
        <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>Brixton.SR6</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-consul-dependencies</artifactId>
            <version>1.0.2.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<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-consul-all</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

请有人点灯吗

1 个答案:

答案 0 :(得分:1)

您没有引用像consul这样的属性,您可以像弹簧属性一样引用它们。默认情况下,spring-cloud-consul在/config上下文下查找。您是否需要将property1放入/config/property1并通过env.getProperty("property1")引用它。对于/config/foo/property,您可以在春天的环境中引用foo.property