protected ServiceDiscovery serviceDiscovery(ServiceInstance serviceInstance) { return ServiceDiscoveryBuilder.builder(Void.class) .basePath(this.zookeeperDiscoveryProperties.getRoot()) .client(this.curatorFramework).thisInstance(serviceInstance).build(); }
@Override public void start() { startCuratorClient(); group = getOrDefault(ZookeeperDiscoveryProperties.GROUP, DEFAULT_GROUP); try { String path = getOrDefault(ZookeeperDiscoveryProperties.ZOOKEEPER_PATH, DEFAULT_PATH); ServiceDiscoveryBuilder<Void> discoveryBuilder = ServiceDiscoveryBuilder.builder(Void.class) .basePath(path) .client(client); if (isMember()) { //register members only into zookeeper //there no need to register clients prepareServiceInstance(); discoveryBuilder.thisInstance(serviceInstance); } serviceDiscovery = discoveryBuilder.build(); serviceDiscovery.start(); } catch (Exception e) { throw new IllegalStateException("Error while talking to ZooKeeper. ", e); } }
public ExampleServer(CuratorFramework client, String path, String serviceName, String description) throws Exception { // in a real application, you'd have a convention of some kind for the // URI layout UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); thisInstance = ServiceInstance.<InstanceDetails> builder().name(serviceName).payload(new InstanceDetails(description)) .port((int) (65535 * Math.random())) // in a real application, // you'd use a common // port .uriSpec(uriSpec).build(); // if you mark your payload class with @JsonRootName the provided // JsonInstanceSerializer will work JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(path).serializer(serializer) .thisInstance(thisInstance).build(); }
public ExampleServer(CuratorFramework client, String path, String serviceName, String description) throws Exception { // in a real application, you'd have a convention of some kind for the URI layout UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}"); thisInstance = ServiceInstance.<InstanceDetails>builder() .name(serviceName) .payload(new InstanceDetails(description)) .port((int)(65535 * Math.random())) // in a real application, you'd use a common port .uriSpec(uriSpec) .build(); // if you mark your payload class with @JsonRootName the provided JsonInstanceSerializer will work JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class) .client(client) .basePath(path) .serializer(serializer) .thisInstance(thisInstance) .build(); }
public void regService(String host, int port) throws Exception { haContext.startCuratorFramework(); ServiceInstance<Void> serviceInstance = ServiceInstance.<Void>builder() .address(host) .port(port) .name(HAContext.SENTRY_SERVICE_REGISTER_NAMESPACE) .build(); InstanceSerializer<Void> instanceSerializer = new FixedJsonInstanceSerializer<Void>(Void.class); ServiceDiscoveryBuilder.builder(Void.class) .basePath(HAContext.SENTRY_SERVICE_REGISTER_NAMESPACE) .client(haContext.getCuratorFramework()) .serializer(instanceSerializer) .thisInstance(serviceInstance) .build() .start(); }