Java, Java Microservices, Micronaut, Microservices

Micronaut Consul Service Discovery Example In Java

Previously, we had an application that reads a property from a distributed configuration. This post shows how to use Micronaut with the Consul’s Service Discovery. We will use two applications where one of them accesses the other’s URI. Also, we will use similar requisites and run everything in a local environment.

Consul Service Registration And Discovery

Service Discovery means that a service is discoverable. In our case, it is discoverable in Consul. Therefore, an application has to register itself in Consul, so others become aware of it. The registration typically happens during application startup. Note that an instance of Consul needs to be running before we proceed. For simplicity, our sample applications perform service registration with Consul at startup.

Create Two Micronaut Applications For Consul Service Discovery

We will play around with two Micronaut applications. The first one, app1, runs on port 8081, registers itself in Consul, and returns some application information. The other application, app2, runs on port 8080 and accesses the latter’s URI from the controller class. Therefore, app2 access a URI in app1 to retrieve a JSON string.

Create these applications with the Consul Discovery feature, as shown below.


The First Micronaut Application

The first Micronaut has the following controller class. It exposes a URI ( /appinfo) that returns JSON string data. We won’t access this URI directly on the browser.

Its application.yml has the following codes.

Finally, its pom.xml has the following content.

Note the service name for registration with Consul. When we start up this application, it will register itself as servicediscovery-app1.

The Second Micronaut Application That Access URL in servicediscovery-app1

The second Micronaut application will access the URL in servicediscovery-app1 from the following class without directly relying on its IP address and port number.

Its application.yml has the following lines.

The only information this application need is the other application’s registered name in Consul. In this case, it’s servicediscoveryApp1. Consider the following codes.

We have an interface that is annotated with @Client  with id referring to servicediscoveryApp1. Then, our codes inject an instance of that interface in our controller class.


Micronaut Applications And Consul Service Discovery In Action

Finally, let’s try out the Consul Service Discovery using our Micronaut applications. Start the applications up and access the Consul UI Dashboard.

Micronaut Consul Service Discovery

Then, access the servicediscoveryApp2 URL.

Micronaut Consul Service Discovery

As we can see, servicediscoveryApp2 displayed a page with information that came from servicediscovery1. For more information, check out the Consul Service Discovery documentation.


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