Service Discovery
In a Microservice architecture services need to discover each other in a decoupled manner that is independent of the service discovery mechanism.
Micronaut features a Service Discovery abstraction that allows service discovery to be backed onto different implementations including Kubernetes, HashiCorp Consul, Eureka and more.
The original MuShop application featured extensive handcrafted service discovery code in both the Spring application:
- OrdersConfigurationProperties & RestProxyTemplate - hand crafted Spring service discovery configuration handling
- OrdersConfigurationProperties & RestProxyTemplate - hand crafted Spring service discovery configuration handling
And Node/Express JavaScript API:
- common/index.js - Manual service discovery implementation
- endpoints.js - Manual service discovery configuration
- helpers/index.js - More manual service discovery routines
By migrating the code to Micronaut all this code could be deleted and instead encapsulated by the defining of a simple service ID and declarative client interfaces:
@Client(id = "mushop-catalogue", path = "/catalogue")
public interface CatalogueClient {
@Get("/{id}")
Maybe<Product> getItem(String id);
}
Using the service ID Micronaut provides client side lookup and load balancing and the target service to invoke is abstracted such that it can be found via explicit declaration in configuration (for example when working locally):
micronaut:
http:
services:
mushop-catalogue:
url: http://localhost:8082
Or automatically via Micronaut’s integration with Kubernetes when deployed to a cluster.