Spring Boot with Consul

Author: Shazin Sadakath

This is the part 2 of Spring Boot with Consul series I am writing and if you missed part 1 I highly recommend you follow that one here. In this session I am planning to explain how to do the following;

Ok lets get started. 

Step 1 - Moving Core configurations to bootstrap.yml

This is a file provided by Spring Boot which loads before application.yml and thus enables to have any load time critical configurations. So we move our Consul related configurations to this file.

    name: web
      host: localhost
      port: 8500
          enabled: true

The above file now specifies the configurations spring.application.name, spring.cloud.consul.host, spring.cloud.consul.port, etc. 

Step 2 - Adding Custom configurations to application.yml

Now our application Specific configurations will go into this file

    conf: 10

Step 3 - Creating Key/Values in Consul 

Inorder to maintain configurations in a centralized manner, we can create Key/Value in Consul as following;

Goto Key/Value table in Consul 

By default the root of all configurations will be "config" so we should create a Key/Value by the name config/consul_service_name/any/property.

Thus in our case config/web/my/property/conf. There are more easier and automated ways to do this which will be have look at later. For the time being please create manually which will give a good learning and feel of Consul.

Click save.

Step 4 - Using the property and Running the Spring Boot "web" service

You can use the property using the @Value annotation as below;

public class GreetingController {

    private String conf;

    public String greet() {
        return "Hello , "+conf;


And take special note of the @RefreshScope annotation. This is provided by Spring Cloud to refresh any bean in case it's configurations changed. spring.cloud.consul.config.watch.enabled property which is set in bootstrap.yml starts a Consul Config watcher which refreshes any bean annotation with this annotation as soon as it is changed.

Now you can run the Spring Boot service and when you hit http://localhost:port you should get the following result.

This is because the default value in application.yml for property my.property.conf is set to 10 by default. 

Step 5 - Dynamically change properties and see them reflected in Spring Boot without restart.

Now we can change the value for my.property.conf in Consul to 200 and save.

As soon as we do that, the log Refresh keys changed: [my.property.conf] is made in Spring Boot "web" service.

Now when you hit the app endpoint again you see the configuration is dynamically changed to 200.

That's all for this tutorial. The source code is available at https://github.com/shazin/spring-boot-consul. Let's learn more in coming weeks. Register with us for more informative posts like this one.


Tags: SpringBoot Consul Microservice ServiceDiscovery ServiceRegistry
Views: 2509
Register for more exciting articles


Please login or register to post a comment.

There are currently no comments.