Junchen Liu
Junchen Liu

Reputation: 5624

Fail to read remote property using Spring Cloud Consul

We are planing to move the content of our numerous .properties files into Consul I have implemented a demo with the following definition

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

    @Autowired
    private Environment env;

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

with the consul server running at the default

at moment env.getProperty("dc1/kv/property1/"); returns nothing

my pom looks like this

<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>

can anyone shed some lights please

Upvotes: 1

Views: 963

Answers (1)

spencergibb
spencergibb

Reputation: 25157

You don't reference properties like consul, you reference them like spring properties. By default, spring-cloud-consul looks under a /config context. Do you would need to put property1 in /config/property1 and reference it via env.getProperty("property1"). For /config/foo/property you would reference foo.property in spring's environment.

Upvotes: 2

Related Questions