Not able to read consul key value properties into spring boot application

1.9k views Asked by At

I am very new to the consul. Started with POC, able to establish communication between two services using the consul, but unable to read key/value pairs from consul into spring boot application.

Have gone through all the solutions available on the internet to understand what I am doing wrong, but nothing helped me.

Please take a look at the code below and suggest to me if I am doing any mistakes

Main class:

@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);
    }
}

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

I am using consul version 1.9.2 I used the below command to start the consul agent:

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

I configured key-value pair in consul UI as config/keyvalue/greeting:

key-value config

When I run my spring boot application I am getting the error:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'greeting' in value "${greeting}"

0

There are 0 answers