How to Register Spring Cloud with Consul Service Discovery


One of the main component of micro service architecture is service discovery (SD). So far in this blog I mentioned two tools for service discovery, which are Eureka and Consul. But if you may notice, whenever I used spring-cloud framework, I always choose Eureka. On the other hand, I pick consul when I talked about Golang.

Sharing is caring!

Overview

So far, we use eureka as our service discovery. It is a good service discovery, and it suffices our needs. Until we want to leverage service discovery, for instance monitoring services, with other non java tools, such as Prometheus. I found this is going to be a problem because ideally, by using service discovery, you do not have to memorize which IP addresses and ports, which your services registered into. Unfortunately, Prometheus and Eureka do not have the mechanism to acknowledge the services. As a result, Prometheus cannot export service metrics. Therefore consul will suit nicely to support this feature out of the box.

The scope of this article only registering services into consul, and how to discover them.

Step-by-step guide

Before the first step, let us assume the consul already up and running at http://192.168.99.100:8500

Register your service into consul

  1. Go to http://start.spring.io/, select dependencies Consul Discovery
  2. Open your project, add this lines in application.properties (or yml) ACL token is generated via consul cli, to give access for registering services. These are the rules:
    See https://www.consul.io/docs/guides/acl.html for more info.
  3. Because consul needs health check, include actuator dependencies into maven configuration. Then for the sake of example, disable the security. (I DO NOT RECOMMEND TO DISABLE THIS. USE SPRING-SECURITY INSTEAD).
  4. Create controller, let say HelloController
  5. Run the service, then check to your consul ui (http://localhost:8500/ui)

Access Service through Feign

Please add feign dependency to your project.

  1. Enable discovery client and feign client in your services.
  2. Configure your application.properties (oryml) just like previous one.
  3. Create Feign Interface.
  4. Create controller.
  5. Start your spring boot application, then test the endpoint via curl.

Advance Topic

Distinguish Your Service by Tags

One of the interesting feature in consul is tags. This feature is similar with Eureka instance metadata. With this feature, we can separate our services based on specific criteria. For instance by version. By filtering our service by version, we can leverage the functionality for blue-green or canary deployment technique.

Tag Your Service
  1. Add tags to your service. This can be done via application.properties file.
  2. Deploy the service.
Access Service by Service Name and Tags

To make easy access, I created a configuration bean, to filter the desired service by name and tags. For example, the target service name is spring-consul-hello. I will make a feature toggle as well, whenever you want to disable the tags filter.

  1. Create a configuration bean.
  2. Configure in application.properties.
  3. Create a custom ribbon configuration.
  4. Deploy the service.

Accessing Key Value Consul Data Store

One of the most interesting feature in consul is their embedded key value data store. You can access or store the value by typing this:

Note: you can get the url, port and token by accessing the properties. This is hard coded just for clarity.

Conclusion

Consul as a service discovery is a great option, beside eureka. What I really like about consul is many supporting tools, like prometheus, provide a feature for accessing the registered services. Moreover, many consul client from other programming language, provide libraries to register and retrieve the services. So we can have many options to deal with, when we want to build a new micro service.

That’s all from me right now. Have a good day.

Author: ru rocker

I am a professional software developer with more than 10 years experiences. I am a certified Java Developer (SCJP and SCWCD). However, In the recent months, I have more interest in DevOps and start to become a polyglot developer. Python and Go-lang become my favorite programming languages besides Java.

Leave a Reply

Your email address will not be published. Required fields are marked *