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.
packageexamples;importio.smallrye.stork.Stork;importio.smallrye.stork.api.ServiceDefinition;importio.smallrye.stork.loadbalancer.random.RandomConfiguration;importio.smallrye.stork.servicediscovery.consul.ConsulRegistrarConfiguration;importio.smallrye.stork.servicediscovery.staticlist.StaticConfiguration;importio.smallrye.stork.servicediscovery.staticlist.StaticRegistrarConfiguration;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()));// Another service using the random selection strategy, instead of round-robin// and a static service registrarstork.defineIfAbsent("my-second-service",ServiceDefinition.of(newStaticConfiguration().withAddressList(example),newRandomConfiguration(),newStaticRegistrarConfiguration()));}}
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.
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.
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;importio.smallrye.stork.servicediscovery.staticlist.StaticRegistrarConfiguration;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());// Another service using the random selection strategy, instead of round-robin// and a static service registrarstork.defineIfAbsent("my-third-service",ServiceDefinition.of(newStaticConfiguration().withAddressList(example),newRandomConfiguration(),newStaticRegistrarConfiguration()));}}