Reputation: 8585
I wonder how to share some properties in HashiCorp Consul using Spring Boot, I read there's the dependency 'spring-cloud-consul-config' but I could not find how to load a folder with all properties files from there.
Using Spring Cloud Config Server this is possible, for example:
spring:
profiles:
active: native
cloud:
config:
server:
native:
searchLocations: file:C:/springbootapp/properties/
But, how to do it in Spring Cloud Config Consul?
Upvotes: 4
Views: 5973
Reputation: 2191
Assuming that you have consul running on a standard port there is not much configuration needed using spring boot. The whole code is pasted below (no other configuration files).
The tricky part for me was to figure out how key/values should be stored in consul in order to be visible by spring boot app. There is some information in documentation but it is a little bit misleading I think.
To answer your question I put the values under key "config/bootstrap/key1" inside consul to make the following example work.
Here is a sample code that worked for me:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<version>1.0.0.M5</version>
</dependency>
Application.java
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class Application {
@Autowired
private Environment env;
@RequestMapping("/")
public String home() {
return env.getProperty("key1");
}
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
Upvotes: 2