Stork proposes a programmatic API that lets you register new service Definitions and do manual lookup and selection.
When using the programmatic API of Stork, you can:
Retrieve the singleton Stork instance. This instance is configured with the set of Services it manages.
Register new service definition.
Retrieve the Service you want to use. Each Service is associated with a name.
Retrieve the ServiceInstance, which will provide the metadata to access the actual instance.
Initializing Stork
If your framework does not already provide a configured Stork instance, you need to do:
To register a new ServiceDefinition, use the defineIfAbsent method:
1 2 3 4 5 6 7 8 91011121314151617181920212223
packageexamples;importio.smallrye.stork.Stork;importio.smallrye.stork.api.ServiceDefinition;importio.smallrye.stork.loadbalancer.random.RandomConfiguration;importio.smallrye.stork.servicediscovery.staticlist.StaticConfiguration;publicclassDefinitionExample{publicstaticvoidexample(Storkstork){Stringexample="localhost:8080, localhost:8081";// A service using a static list of locations as discovery// As not set, it defaults to round-robin to select the instance.stork.defineIfAbsent("my-service",ServiceDefinition.of(newStaticConfiguration().withAddressList(example)));// Another service using the random selection strategy, instead of round-robinstork.defineIfAbsent("my-second-service",ServiceDefinition.of(newStaticConfiguration().withAddressList(example),newRandomConfiguration()));}}
The ServiceDefinition instances can be created from:
A service discovery configuration - these classes are provided by the service discovery implementations,
An optional load balancer configuration - these classes are provided by the load balancer implementations
Attributes from the service discovery and load balancer can be configured from the Configuration classes.
Looking for service instances
To list the service instances for a given service, or to select an instance according to the load balancer strategy, use the following code:
packageexamples;importio.smallrye.mutiny.Uni;importio.smallrye.stork.Stork;importio.smallrye.stork.api.Service;importio.smallrye.stork.api.ServiceDefinition;importio.smallrye.stork.api.ServiceInstance;importio.smallrye.stork.loadbalancer.random.RandomConfiguration;importio.smallrye.stork.servicediscovery.staticlist.StaticConfiguration;importjava.util.List;importjava.util.Map;publicclassLookupExample{publicstaticvoidexample(Storkstork){Serviceservice=stork.getService("my-service");// Gets all the available instances:Uni<List<ServiceInstance>>instances=service.getInstances();// Select one instance using the load balancing strategyUni<ServiceInstance>instance=service.selectInstance();// Gets all the managed services:Map<String,Service>services=stork.getServices();}}
The lookup and selection methods are returning Uni as these processes are asynchronous.
All in one example
The following snippet provides an all in one example of the Stork programmatic API:
packageexamples;importio.smallrye.stork.Stork;importio.smallrye.stork.api.ServiceDefinition;importio.smallrye.stork.api.ServiceInstance;importio.smallrye.stork.loadbalancer.random.RandomConfiguration;importio.smallrye.stork.servicediscovery.staticlist.StaticConfiguration;importjava.time.Duration;publicclassStorkApiExample{publicstaticvoidmain(String[]args){Stork.initialize();Storkstork=Stork.getInstance();Stringexample="localhost:8080, localhost:8082";// A service using a static list of locations as discovery// As not set, it defaults to round-robin to select the instance.stork.defineIfAbsent("my-service",ServiceDefinition.of(newStaticConfiguration().withAddressList(example)));// Another service using the random selection strategy, instead of round-robinstork.defineIfAbsent("my-second-service",ServiceDefinition.of(newStaticConfiguration().withAddressList(example),newRandomConfiguration()));ServiceInstanceinstance=stork.getService("my-second-service").selectInstance().await().atMost(Duration.ofSeconds(1));System.out.println(instance.getHost()+":"+instance.getPort());}}