Class ServiceDiscoveryImpl<T>
java.lang.Object
org.apache.curator.x.discovery.details.ServiceDiscoveryImpl<T>
- All Implemented Interfaces:
Closeable
,AutoCloseable
,ServiceDiscovery<T>
A mechanism to register and query service instances using ZooKeeper
-
Constructor Summary
ConstructorDescriptionServiceDiscoveryImpl
(CuratorFramework client, String basePath, InstanceSerializer<T> serializer, ServiceInstance<T> thisInstance, boolean watchInstances) ServiceDiscoveryImpl
(CuratorFramework client, DiscoveryPathConstructor pathConstructor, InstanceSerializer<T> serializer, ServiceInstance<T> thisInstance, boolean watchInstances) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
protected void
internalRegisterService
(ServiceInstance<T> service) queryForInstance
(String name, String id) Return a service instance POJOqueryForInstances
(String name) Return all known instances for the given serviceReturn the names of all known servicesvoid
registerService
(ServiceInstance<T> service) Register/re-register/update a service instanceAllocate a new service cache builder.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 Details
-
ServiceDiscoveryImpl
public ServiceDiscoveryImpl(CuratorFramework client, 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
-
ServiceDiscoveryImpl
public ServiceDiscoveryImpl(CuratorFramework client, DiscoveryPathConstructor pathConstructor, InstanceSerializer<T> serializer, ServiceInstance<T> thisInstance, boolean watchInstances) - Parameters:
client
- the clientpathConstructor
- constructor for instance pathsserializer
- 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 Details
-
start
The discovery must be started before use- Specified by:
start
in interfaceServiceDiscovery<T>
- Throws:
Exception
- errors
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
registerService
Register/re-register/update a service instance- Specified by:
registerService
in interfaceServiceDiscovery<T>
- Parameters:
service
- service to add- Throws:
Exception
- errors
-
updateService
Description copied from interface:ServiceDiscovery
Update a service- Specified by:
updateService
in interfaceServiceDiscovery<T>
- Parameters:
service
- service to update- Throws:
Exception
- errors
-
internalRegisterService
- Throws:
Exception
-
unregisterService
Unregister/remove a service instance- Specified by:
unregisterService
in interfaceServiceDiscovery<T>
- Parameters:
service
- the service- Throws:
Exception
- errors
-
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
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
Return the names of all known services- Specified by:
queryForNames
in interfaceServiceDiscovery<T>
- Returns:
- list of service names
- Throws:
Exception
- errors
-
queryForInstances
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:
Exception
- errors
-
queryForInstance
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:
Exception
- errors
-