Class ServiceDiscoveryImpl<T>
- java.lang.Object
-
- org.apache.curator.x.discovery.details.ServiceDiscoveryImpl<T>
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,ServiceDiscovery<T>
public class ServiceDiscoveryImpl<T> extends java.lang.Object implements ServiceDiscovery<T>
A mechanism to register and query service instances using ZooKeeper
-
-
Constructor Summary
Constructors Constructor Description ServiceDiscoveryImpl(org.apache.curator.framework.CuratorFramework client, java.lang.String basePath, InstanceSerializer<T> serializer, ServiceInstance<T> thisInstance, boolean watchInstances)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()protected voidinternalRegisterService(ServiceInstance<T> service)ServiceInstance<T>queryForInstance(java.lang.String name, java.lang.String id)Return a service instance POJOjava.util.Collection<ServiceInstance<T>>queryForInstances(java.lang.String name)Return all known instances for the given servicejava.util.Collection<java.lang.String>queryForNames()Return the names of all known servicesvoidregisterService(ServiceInstance<T> service)Register/re-register/update a service instanceServiceCacheBuilder<T>serviceCacheBuilder()Allocate a new service cache builder.ServiceProviderBuilder<T>serviceProviderBuilder()Allocate a new builder.voidstart()The discovery must be started before usevoidunregisterService(ServiceInstance<T> service)Unregister/remove a service instancevoidupdateService(ServiceInstance<T> service)Update a service
-
-
-
Constructor Detail
-
ServiceDiscoveryImpl
public ServiceDiscoveryImpl(org.apache.curator.framework.CuratorFramework client, java.lang.String basePath, InstanceSerializer<T> serializer, ServiceInstance<T> thisInstance, boolean watchInstances)- Parameters:
client- the clientbasePath- base path to store dataserializer- serializer for instances (e.g.JsonInstanceSerializer)thisInstance- instance that represents the service that is running. The instance will get auto-registeredwatchInstances- if true, watches for changes to locally registered instances
-
-
Method Detail
-
start
public void start() throws java.lang.ExceptionThe discovery must be started before use- Specified by:
startin interfaceServiceDiscovery<T>- Throws:
java.lang.Exception- errors
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
registerService
public void registerService(ServiceInstance<T> service) throws java.lang.Exception
Register/re-register/update a service instance- Specified by:
registerServicein interfaceServiceDiscovery<T>- Parameters:
service- service to add- Throws:
java.lang.Exception- errors
-
updateService
public void updateService(ServiceInstance<T> service) throws java.lang.Exception
Description copied from interface:ServiceDiscoveryUpdate a service- Specified by:
updateServicein interfaceServiceDiscovery<T>- Parameters:
service- service to update- Throws:
java.lang.Exception- errors
-
internalRegisterService
protected void internalRegisterService(ServiceInstance<T> service) throws java.lang.Exception
- Throws:
java.lang.Exception
-
unregisterService
public void unregisterService(ServiceInstance<T> service) throws java.lang.Exception
Unregister/remove a service instance- Specified by:
unregisterServicein interfaceServiceDiscovery<T>- Parameters:
service- the service- Throws:
java.lang.Exception- errors
-
serviceProviderBuilder
public ServiceProviderBuilder<T> serviceProviderBuilder()
Allocate a new builder.ServiceProviderBuilder.providerStrategy(org.apache.curator.x.discovery.ProviderStrategy<T>)is set toRoundRobinStrategy- Specified by:
serviceProviderBuilderin interfaceServiceDiscovery<T>- Returns:
- the builder
-
serviceCacheBuilder
public ServiceCacheBuilder<T> serviceCacheBuilder()
Allocate a new service cache builder. The refresh padding is defaulted to 1 second.- Specified by:
serviceCacheBuilderin interfaceServiceDiscovery<T>- Returns:
- new cache builder
-
queryForNames
public java.util.Collection<java.lang.String> queryForNames() throws java.lang.ExceptionReturn the names of all known services- Specified by:
queryForNamesin interfaceServiceDiscovery<T>- Returns:
- list of service names
- Throws:
java.lang.Exception- errors
-
queryForInstances
public java.util.Collection<ServiceInstance<T>> queryForInstances(java.lang.String name) throws java.lang.Exception
Return all known instances for the given service- Specified by:
queryForInstancesin interfaceServiceDiscovery<T>- Parameters:
name- name of the service- Returns:
- list of instances (or an empty list)
- Throws:
java.lang.Exception- errors
-
queryForInstance
public ServiceInstance<T> queryForInstance(java.lang.String name, java.lang.String id) throws java.lang.Exception
Return a service instance POJO- Specified by:
queryForInstancein interfaceServiceDiscovery<T>- Parameters:
name- name of the serviceid- ID of the instance- Returns:
- the instance or
nullif not found - Throws:
java.lang.Exception- errors
-
-