Service discovery using SmallRye Stork with Quarkus

European handicapped girl in vr glasses on sofa at home. Healing technology, robotic limb.
Table of contents
Reading Time: 2 minutes

In this era of microservice based architecture where multiple services are interacting with each other to carry out a transaction, service discovery becomes an integral part as it provides a mechanism to register and find the services. However, it leads to another issue which is “How to select the right service instance in case we have multiple instances of a service running?” .

SmallRye Stork provides both the features:

  • Service discovery: Lets’ you locate the service instance
  • Service selection: Lets’ you select the right instance when there are more than one instance

What is Stork?

SmallRye Stork is a service discovery and client-side load-balancing framework. It comes pre-configured with integrations for Kubernetes, Eureka, and Hashicorp Consul as well as a few load-balancing methods, including round-robin. But the most noteworthy feature of Stork is its extensibility. You can create your own service selection strategy or plug in your own service discovery mechanism.

Using Stork with Quarkus

Since Quarkus 2.5.0.Final, the discovery and selection of the services can be handled via Stork with Quarkus gRPC and the Quarkus Reactive REST Client. Let’s see how we can use service discovery using Stork with Quarkus.

The Project

Create a Quarkus project with the REST Client Reactive extension using code.quarkus.io. The corresponding Quarkus guide describes the extension in more detail.

Dependencies

Storks supports multiple types of service discovery client like Consul and Eureka, along with custom implementation. Based on the choice of implementation appropriate dependencies need to be added. Below we are adding the consul dependencies for service discovery with round robin load-balancer.

    <dependency>
        <groupId>io.smallrye.stork</groupId>
        <artifactId>smallrye-stork-service-discovery-consul</artifactId>
        <version>SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>io.smallrye.stork</groupId>
        <artifactId>smallrye-stork-load-balancer-round-robin</artifactId>
        <version>SNAPSHOT</version>
    </dependency>

The Client

In order to utilise the REST client to communicate with a remote endpoint, we need to create an interface that specifies the communication’s route. The baseUrl (or baseUri) of the client must point to the remote endpoint’s address.

Set the host-name of the URI to the name of the Stork service and the scheme of the URI to stork to use Stork to locate the actual address.

Application configuration

If your Consul instance is running on localhost on port 8500, service discovery configuration should look as follows:

stork.product-service.service-discovery=consul
stork.product-service.service-discovery.consul-host=localhost
stork.product-service.service-discovery.consul-port=8500
stork.product-service.load-balancer=round-robin

Conclusion

SmallRye Stork and its connection with Quarkus are introduced in this post. Stork offers a method for finding and choosing services. It is easy to use and adaptable.