Spring boot Get properties from consul server
Asked Answered
P

1

5

I have a spring boot application and I would like to get properties that I have on a consul agent.

@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages={"com.commons"})
public class MainAppProxy   implements CommandLineRunner {      
    @Value("${proxy.endpoint}")
    private String endpointAddress;

My application.properties is under src/main/resources

spring.application.name=SOAPProxy
spring.cloud.consul.host=http://10.0.1.241
spring.cloud.consul.port=8500
spring.cloud.config.discovery.enabled=false

in pom.xml I have the following config (short version)

            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Camden.SR5</version>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>

The properties are stored on consul in this format: Business/SOAPProxy/proxy.endpoint

When the application boots, it seems that it finds consul but it cannot get the values as it could before trying consul @Value("${proxy.endpoint}") How can I get the properties on consul?

Phosphide answered 2/3, 2017 at 16:44 Comment(1)
do you need to get configuration from consul?Avowed
A
8

You can use three way to load configuration from consul

  1. key/value
  2. yaml
  3. file

I used in yaml to load configuration.

This is my bootstrap.yml file (you can use .property file also)

spring:
  application:
    name: SOAPProxy

---

spring:
  profiles: default
  cloud:
    consul:
      config:
        data-key: data
        prefix: config
        format: yaml
      host: localhost
      port: 8500
      discovery:
        prefer-ip-address: true  

my boot app Annotate like below

@EnableDiscoveryClient
@EnableAutoConfiguration
@SpringBootApplication
public class SpringBootConsulApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootConsulApplication.class, args);
    }
}

maven dependancy add like this

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

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

this is configuration of consul agent key/value

enter image description here

now in startup all configuration load to the application

Avowed answered 9/3, 2017 at 5:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.