public class ZKDiscoveryService extends Object implements DiscoveryService, DiscoveryServiceClient, AutoCloseable
DiscoveryService
and DiscoveryServiceClient
.
Discoverable services are registered within Zookeeper under the namespace 'discoverable' by default.
If you would like to change the namespace under which the services are registered then you can pass
in the namespace during construction of ZKDiscoveryService
.
Following is a simple example of how ZKDiscoveryService
can be used for registering services
and also for discovering the registered services.
DiscoveryService service = new ZKDiscoveryService(zkClient); service.register(new Discoverable() { @Override public String getName() { return 'service-name'; } @Override public InetSocketAddress getSocketAddress() { return new InetSocketAddress(hostname, port); } }); ... ... ServiceDiscovered services = service.discovery("service-name"); ...
Constructor and Description |
---|
ZKDiscoveryService(ZKClient zkClient)
Constructs ZKDiscoveryService using the provided zookeeper client for storing service registry.
|
ZKDiscoveryService(ZKClient zkClient,
String namespace)
Constructs ZKDiscoveryService using the provided zookeeper client for storing service registry under namespace.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
ServiceDiscovered |
discover(String service)
Discover service with the given name.
|
Cancellable |
register(Discoverable discoverable)
Registers a
Discoverable in zookeeper. |
public ZKDiscoveryService(ZKClient zkClient)
zkClient
- The ZKClient
for interacting with zookeeper.public ZKDiscoveryService(ZKClient zkClient, String namespace)
zkClient
- of zookeeper quorumnamespace
- under which the service registered would be stored in zookeeper.
If namespace is null
, no namespace will be used.public Cancellable register(Discoverable discoverable)
Discoverable
in zookeeper.
Registering a Discoverable
will create a node <base>/<service-name>
in zookeeper as a ephemeral node. If the node already exists (timeout associated with emphemeral node creation),
then a runtime exception is thrown to make sure that a service with an intent to register is not started without
registering.
When a runtime exception is thrown, expectation is that the process being started will fail and would be started
again by the monitoring service.
register
in interface DiscoveryService
discoverable
- Information of the service provider that could be discovered.Cancellable
public ServiceDiscovered discover(String service)
DiscoveryServiceClient
discover
in interface DiscoveryServiceClient
service
- Name of the serviceServiceDiscovered
object representing the result.public void close()
close
in interface AutoCloseable
Copyright © 2013-2016 The Apache Software Foundation. All rights reserved.