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 void
close()
protected void
internalRegisterService(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 servicesvoid
registerService(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.void
start()
The discovery must be started before usevoid
unregisterService(ServiceInstance<T> service)
Unregister/remove a service instancevoid
updateService(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.Exception
The discovery must be started before use- Specified by:
start
in interfaceServiceDiscovery<T>
- Throws:
java.lang.Exception
- errors
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in 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:
registerService
in 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:ServiceDiscovery
Update a service- Specified by:
updateService
in 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:
unregisterService
in 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:
serviceProviderBuilder
in 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:
serviceCacheBuilder
in interfaceServiceDiscovery<T>
- Returns:
- new cache builder
-
queryForNames
public java.util.Collection<java.lang.String> queryForNames() throws java.lang.Exception
Return the names of all known services- Specified by:
queryForNames
in 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:
queryForInstances
in 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:
queryForInstance
in interfaceServiceDiscovery<T>
- Parameters:
name
- name of the serviceid
- ID of the instance- Returns:
- the instance or
null
if not found - Throws:
java.lang.Exception
- errors
-
-