Java, Java Microservices, Spring Boot

Spring Boot Consul Service Discovery And Client Example

This post shows how to use Spring Boot Service Discovery with Consul. We’ll have two Spring Boot applications. One application registers itself with Consul. It then becomes discoverable to another application. Meanwhile, the other application does not register itself with Consul. Instead, it only contacts Consul to look for a service by name. Then, Consul returns the correct URL the application can use. Clear enough? Let’s go!

Requisites – Things We Used

For some, the requirements are already apparent. However, listing out the requisites is excellent and helpful for newbies.

  1. IntelliJ IDEA
  2. Spring Boot 2.4.5
  3. JDK 14
  4. Consul
  5. Docker for Windows
  6. Windows 10

Start Consul Docker Container Up

Next, we start up a Consul Docker container in our local development machine using the following command.

Create First Spring Boot Service Application For Consul

We can either use Spring Initialzr or IntelliJ IDE to create the first Spring Boot service application for Consult. Then, we add only two dependencies – Spring Web and Consul Discovery. First, we have our pom.xml file.

Second, we create an application.yml file as follows. For this service, we have to make it register itself at startup. Also, we provide a particular URL for Consult to perform a health check on the application.

Note the instance-id and serviceName properties – they are prefixed with letters. See comments on the file. When the values are invalid, the application fails to start.

Next, we have the Java source code files. First, we have the main class.

Then, we have the health check controller for Consul.

Lastly, we have our API controller.

Before moving on to the second Spring Boot application, we can start this application up. Then, we can check on Consul to see our registered service application.

Create Second Spring Boot Client Application That Uses Service Discovery

The second Spring Boot application uses the same set of dependencies similar to the first application. However, we don’t allow it to register itself with Consul at startup. After creating the project, create or update the application.yml as follows.

This Spring Boot application will only discover the other application in Consul via Service Discovery. Then, it uses the correct URL to use the service application. Next, we create the Java source files. The main class has the following content.

Note that we are using the @EnableDiscoveryClient to reach out to Consul to discover the service application. Next, we have a REST controller to test.

When we started this second Spring Boot application, we can test it using the HTTP://localhost:8081/get URL to retrieve information from the first Spring Boot application registered in Consul.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like