org.apache.curator.x.discovery.ServiceDiscoveryBuilder Java Examples
The following examples show how to use
org.apache.curator.x.discovery.ServiceDiscoveryBuilder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestServiceDiscoveryBuilder.java From xian with Apache License 2.0 | 6 votes |
@Test public void testSetSerializer() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); ServiceDiscoveryBuilder<Object> builder = ServiceDiscoveryBuilder.builder(Object.class).client(client); builder.serializer(new InstanceSerializer<Object>() { @Override public byte[] serialize(ServiceInstance<Object> instance) { return null; } @Override public ServiceInstance<Object> deserialize(byte[] bytes) { return null; } }); ServiceDiscoveryImpl<?> discovery = (ServiceDiscoveryImpl<?>) builder.basePath("/path").build(); Assert.assertNotNull(discovery.getSerializer(), "default serializer not set"); Assert.assertFalse(discovery.getSerializer() instanceof JsonInstanceSerializer, "set serializer is JSON"); }
Example #2
Source File: ZookeeperRegistryBeanProvider.java From Ratel with Apache License 2.0 | 6 votes |
@SuppressWarnings("PMD.AvoidCatchingGenericException") private void configure() { System.setProperty("zookeeper.sasl.client", "false"); Environment env = beanFactory.getBean(Environment.class); String zkHostAddress = env.getProperty(RegistryBeanProviderFactory.SERVICE_DISCOVERY_ZK_HOST); this.curatorFramework = CuratorFrameworkFactory.newClient(zkHostAddress, new ExponentialBackoffRetry(1000, 3)); curatorFramework.start(); this.serviceDiscovery = ServiceDiscoveryBuilder.builder(Void.class).client(this.curatorFramework) .basePath("/services").build(); try { getServiceDiscovery().start(); } catch (Exception e) { throw new IllegalStateException("Service discovery start failed.", e); } this.zookeeperRegistry = new ZookeeperRegistry(getServiceDiscovery()); this.zookeeperFetcher = new ZookeeperFetcher(getServiceDiscovery()); this.zookeeperProxyGenerator = new ZookeeperProxyGenerator(beanFactory); }
Example #3
Source File: ZookeeperBridgeTest.java From vertx-service-discovery with Apache License 2.0 | 6 votes |
@Before public void startZookeeper() throws Exception { zkTestServer = new TestingServer(2181); cli = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(), new RetryOneTime(2000)); cli.start(); discovery = ServiceDiscoveryBuilder.builder(String.class) .client(cli) .basePath("/discovery") .watchInstances(true) .build(); discovery.start(); vertx = Vertx.vertx(); sd = io.vertx.servicediscovery.ServiceDiscovery.create(vertx); }
Example #4
Source File: ZookeeperServiceDiscovery.java From Microservices-Deployment-Cookbook with MIT License | 6 votes |
private static ServiceProvider<Object> getGeolocationServiceProvider() throws Exception { if(geolocationServiceProvider == null) { CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("192.168.99.100:2181", new RetryNTimes(5, 1000)); curatorFramework.start(); ServiceDiscovery<Object> serviceDiscovery = ServiceDiscoveryBuilder.builder(Object.class) .basePath("com.packt.microservices") .client(curatorFramework) .build(); serviceDiscovery.start(); geolocationServiceProvider = serviceDiscovery.serviceProviderBuilder() .serviceName("geolocation") .build(); geolocationServiceProvider.start(); } return geolocationServiceProvider; }
Example #5
Source File: DefaultBrokerRegistry.java From hermes with Apache License 2.0 | 6 votes |
@Override public void initialize() throws InitializationException { try { thisInstance = ServiceInstance.<Void> builder()// .name(m_config.getRegistryName(null))// .address(Networks.forIp().getLocalHostAddress())// .port(m_config.getListeningPort())// .id(m_config.getSessionId())// .build(); m_serviceDiscovery = ServiceDiscoveryBuilder.builder(Void.class)// .client(m_client.get())// .basePath(m_config.getRegistryBasePath())// .thisInstance(thisInstance)// .build(); } catch (Exception e) { throw new InitializationException("Failed to init broker registry.", e); } }
Example #6
Source File: MetaServerBaseTest.java From hermes with Apache License 2.0 | 6 votes |
private int brokerRegisterToZK() throws Exception { int port = brokerPort.getAndIncrement(); ServiceInstance<Void> thisInstance = ServiceInstance.<Void> builder()// .name("default")// .address(localhostIP)// .port(port)// .id(String.valueOf(port))// .build(); ServiceDiscovery<Void> m_serviceDiscovery = ServiceDiscoveryBuilder.builder(Void.class)// .client(m_client.get())// .basePath("brokers")// .thisInstance(thisInstance)// .build(); m_serviceDiscovery.start(); if (tempBrokers.containsKey(port)) { throw new RuntimeException("Already Existed in tempBrokers!!"); } else { tempBrokers.put(port, m_serviceDiscovery); return port; } }
Example #7
Source File: ExampleServer.java From curator with Apache License 2.0 | 6 votes |
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(); }
Example #8
Source File: TestServiceDiscoveryBuilder.java From curator with Apache License 2.0 | 6 votes |
@Test public void testSetSerializer() { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); ServiceDiscoveryBuilder<Object> builder = ServiceDiscoveryBuilder.builder(Object.class).client(client); builder.serializer(new InstanceSerializer<Object>() { @Override public byte[] serialize(ServiceInstance<Object> instance) { return null; } @Override public ServiceInstance<Object> deserialize(byte[] bytes) { return null; } }); ServiceDiscoveryImpl<?> discovery = (ServiceDiscoveryImpl<?>) builder.basePath("/path").build(); Assert.assertNotNull(discovery.getSerializer(), "default serializer not set"); Assert.assertFalse(discovery.getSerializer() instanceof JsonInstanceSerializer, "set serializer is JSON"); }
Example #9
Source File: TestServiceDiscovery.java From curator with Apache License 2.0 | 6 votes |
@Test public void testBasic() throws Exception { CuratorFramework client = null; ServiceDiscovery<String> discovery = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start(); ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build(); discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build(); discovery.start(); Assert.assertEquals(discovery.queryForNames(), Collections.singletonList("test")); List<ServiceInstance<String>> list = Lists.newArrayList(); list.add(instance); Assert.assertEquals(discovery.queryForInstances("test"), list); } finally { CloseableUtils.closeQuietly(discovery); CloseableUtils.closeQuietly(client); } }
Example #10
Source File: TestServiceDiscovery.java From curator with Apache License 2.0 | 6 votes |
@Test public void testCleaning() throws Exception { CuratorFramework client = null; ServiceDiscovery<String> discovery = null; try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start(); ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build(); discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build(); discovery.start(); discovery.unregisterService(instance); Assert.assertEquals(((ServiceDiscoveryImpl)discovery).debugServicesQty(), 0); } finally { CloseableUtils.closeQuietly(discovery); CloseableUtils.closeQuietly(client); } }
Example #11
Source File: ZkServiceRegistry.java From xian with Apache License 2.0 | 6 votes |
/** * 启动服务注册,只有启动了服务注册,才可以调用{@link #registerService(String, NodeStatus)} */ public static void start() { synchronized (lock) { if (registry != null) return; try { LOG.info("开始启动zk服务注册入口"); registry = new ZkServiceRegistry(); FastJsonServiceInstanceSerializer<NodeStatus> serializer = new FastJsonServiceInstanceSerializer<>(); registry.serviceDiscovery = ServiceDiscoveryBuilder.builder(NodeStatus.class) .client(ZkConnection.client) .basePath(ZkPathManager.getNodeBasePath()) .serializer(serializer) .build(); registry.serviceDiscovery.start(); LOG.info("zk服务注册入口启动完毕"); registerService(EnvUtil.getApplication(), LocalNodeManager.singleton.getFullStatus()); } catch (Exception e) { throw new RuntimeException("启动zkServiceRegistry失败", e); } } }
Example #12
Source File: ExampleServer.java From xian with Apache License 2.0 | 6 votes |
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(); }
Example #13
Source File: DiscoveryExample.java From ZKRecipesByExample with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { // This method is scaffolding to get the example up and running TestingServer server = new TestingServer(); CuratorFramework client = null; ServiceDiscovery<InstanceDetails> serviceDiscovery = null; Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap(); try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(PATH).serializer(serializer).build(); serviceDiscovery.start(); processCommands(serviceDiscovery, providers, client); } finally { for (ServiceProvider<InstanceDetails> cache : providers.values()) { CloseableUtils.closeQuietly(cache); } CloseableUtils.closeQuietly(serviceDiscovery); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
Example #14
Source File: CuratorDiscoveryIntegrationTest.java From genie with Apache License 2.0 | 6 votes |
@BeforeEach void setUp() throws Exception { this.zkServer = new TestingServer(); this.curator = CuratorFrameworkFactory.builder() .connectString(zkServer.getConnectString()) .retryPolicy(new ExponentialBackoffRetry(50, 4)) .build(); this.curator.start(); this.curator.blockUntilConnected(10, TimeUnit.SECONDS); this.serviceDiscovery = ServiceDiscoveryBuilder.builder(AgentRoutingServiceCuratorDiscoveryImpl.Agent.class) .basePath("/discovery") .client(curator) .build(); }
Example #15
Source File: ServiceRegister.java From incubator-sentry with Apache License 2.0 | 6 votes |
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(); }
Example #16
Source File: ServiceManager.java From incubator-sentry with Apache License 2.0 | 6 votes |
private void init() throws IOException { try { haContext.startCuratorFramework(); InstanceSerializer<Void> instanceSerializer = new FixedJsonInstanceSerializer<Void>(Void.class); serviceDiscovery = ServiceDiscoveryBuilder.<Void>builder(Void.class) .basePath(HAContext.SENTRY_SERVICE_REGISTER_NAMESPACE) .serializer(instanceSerializer) .client(haContext.getCuratorFramework()) .build(); serviceDiscovery.start(); serviceProvider = serviceDiscovery .serviceProviderBuilder() .serviceName(HAContext.SENTRY_SERVICE_REGISTER_NAMESPACE) .build(); serviceProvider.start(); } catch (Exception e) { throw new IOException(e); } }
Example #17
Source File: RemoteServiceLocatorConfig.java From springboot-plus with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Override public void addServiceURI(String path, String url) { try{ ServiceInstanceBuilder<Map> service = ServiceInstance.builder(); service.address(""); service.port(0); service.name(path); Map config = new HashMap(); config.put("url", url); service.payload(config); ServiceInstance<Map> instance = service.build(); ServiceDiscovery<Map> serviceDiscovery = ServiceDiscoveryBuilder.builder(Map.class).client(client) .serializer(new JsonInstanceSerializer<Map>(Map.class)).basePath("/service").build(); // 服务注册 serviceDiscovery.registerService(instance); serviceDiscovery.start(); }catch(Exception ex){ log.warn(ex.getMessage(), ex); throw new PlatformException("服务注册中心 出错",ex); } }
Example #18
Source File: ZookeeperAutoConfiguration.java From genie with Apache License 2.0 | 5 votes |
/** * The Curator-based Service Discovery bean. * * @param client The curator framework client to use * @param zookeeperProperties The Zookeeper properties to use * @return {@link ServiceDiscovery} bean for instances of type {@link AgentRoutingServiceCuratorDiscoveryImpl.Agent} */ @Bean @ConditionalOnMissingBean(ServiceDiscovery.class) ServiceDiscovery<AgentRoutingServiceCuratorDiscoveryImpl.Agent> serviceDiscovery( final CuratorFramework client, final ZookeeperProperties zookeeperProperties ) { return ServiceDiscoveryBuilder.builder(AgentRoutingServiceCuratorDiscoveryImpl.Agent.class) .basePath(zookeeperProperties.getDiscoveryPath()) .client(client) .build(); }
Example #19
Source File: ZKAssistedDiscovery.java From attic-apex-malhar with Apache License 2.0 | 5 votes |
private ServiceDiscovery<byte[]> getDiscovery(CuratorFramework curatorFramework) { return ServiceDiscoveryBuilder.builder(byte[].class) .basePath(basePath) .client(curatorFramework) .serializer(instanceSerializerFactory.getInstanceSerializer( new TypeReference<ServiceInstance<byte[]>>() {})).build(); }
Example #20
Source File: TcpDiscoveryZookeeperIpFinder.java From ignite with Apache License 2.0 | 5 votes |
/** Initializes this IP Finder by creating the appropriate Curator objects. */ private void init() { if (!initGuard.compareAndSet(false, true)) return; String sysPropZkConnString = System.getProperty(PROP_ZK_CONNECTION_STRING); if (sysPropZkConnString != null && !sysPropZkConnString.trim().isEmpty()) zkConnectionString = sysPropZkConnString; if (log.isInfoEnabled()) log.info("Initializing ZooKeeper IP Finder."); if (curator == null) { A.notNullOrEmpty(zkConnectionString, String.format("ZooKeeper URL (or system property %s) cannot be null " + "or empty if a CuratorFramework object is not provided explicitly", PROP_ZK_CONNECTION_STRING)); curator = CuratorFrameworkFactory.newClient(zkConnectionString, retryPolicy); } if (curator.getState() == CuratorFrameworkState.LATENT) curator.start(); A.ensure(curator.getState() == CuratorFrameworkState.STARTED, "CuratorFramework can't be started."); discovery = ServiceDiscoveryBuilder.builder(IgniteInstanceDetails.class) .client(curator) .basePath(basePath) .serializer(new JsonInstanceSerializer<>(IgniteInstanceDetails.class)) .build(); }
Example #21
Source File: CuratorServiceDiscoveryAutoConfiguration.java From spring-cloud-zookeeper with Apache License 2.0 | 5 votes |
@Bean @ConditionalOnMissingBean public ServiceDiscovery<ZookeeperInstance> curatorServiceDiscovery( ServiceDiscoveryCustomizer customizer) { return customizer .customize(ServiceDiscoveryBuilder.builder(ZookeeperInstance.class)); }
Example #22
Source File: DefaultServiceDiscoveryCustomizer.java From spring-cloud-zookeeper with Apache License 2.0 | 5 votes |
@Override public ServiceDiscovery<ZookeeperInstance> customize( ServiceDiscoveryBuilder<ZookeeperInstance> builder) { // @formatter:off return builder .client(this.curator) .basePath(this.properties.getRoot()) .serializer(this.instanceSerializer) .build(); // @formatter:on }
Example #23
Source File: ZookeeperServiceDiscoveryOps.java From syncope with Apache License 2.0 | 5 votes |
@Override public void afterPropertiesSet() throws Exception { discovery = ServiceDiscoveryBuilder.builder(Void.class). client(client). basePath(SERVICE_PATH). build(); discovery.start(); }
Example #24
Source File: DiscoveryBundle.java From snowizard-discovery with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Override public void run(final T configuration, final Environment environment) throws Exception { final DiscoveryFactory discoveryConfig = getDiscoveryFactory(configuration); final CuratorFactory factory = new CuratorFactory(environment); final CuratorFramework framework = factory.build(discoveryConfig); final JacksonInstanceSerializer<InstanceMetadata> serializer = new JacksonInstanceSerializer<InstanceMetadata>( mapper, new TypeReference<ServiceInstance<InstanceMetadata>>() { }); discovery = ServiceDiscoveryBuilder.builder(InstanceMetadata.class) .basePath(discoveryConfig.getBasePath()).client(framework) .serializer(serializer).build(); final CuratorAdvertiser advertiser = new CuratorAdvertiser( discoveryConfig, discovery); // this listener is used to get the actual HTTP port this server is // listening on and uses that to register the service with ZK. environment.lifecycle().addServerLifecycleListener( new CuratorAdvertisementListener(advertiser)); // this managed service is used to register the shutdown handler to // de-advertise the service from ZK on shutdown. environment.lifecycle() .manage(new CuratorAdvertiserManager(advertiser)); // this managed service is used to start and stop the service discovery environment.lifecycle().manage( new ServiceDiscoveryManager<InstanceMetadata>(discovery)); }
Example #25
Source File: DiscoveryExample.java From curator with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { // This method is scaffolding to get the example up and running TestingServer server = new TestingServer(); CuratorFramework client = null; ServiceDiscovery<InstanceDetails> serviceDiscovery = null; Map<String, ServiceProvider<InstanceDetails>> providers = Maps.newHashMap(); try { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client.start(); JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class); serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class).client(client).basePath(PATH).serializer(serializer).build(); serviceDiscovery.start(); processCommands(serviceDiscovery, providers, client); } finally { for ( ServiceProvider<InstanceDetails> cache : providers.values() ) { CloseableUtils.closeQuietly(cache); } CloseableUtils.closeQuietly(serviceDiscovery); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
Example #26
Source File: TestServiceProvider.java From curator with Apache License 2.0 | 5 votes |
@Test public void testBasic() throws Exception { List<Closeable> closeables = Lists.newArrayList(); try { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); closeables.add(client); client.start(); ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build(); ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build(); closeables.add(discovery); discovery.start(); ServiceProvider<String> provider = discovery.serviceProviderBuilder().serviceName("test").build(); closeables.add(provider); provider.start(); Assert.assertEquals(provider.getInstance(), instance); List<ServiceInstance<String>> list = Lists.newArrayList(); list.add(instance); Assert.assertEquals(provider.getAllInstances(), list); } finally { Collections.reverse(closeables); for ( Closeable c : closeables ) { CloseableUtils.closeQuietly(c); } } }
Example #27
Source File: TestServiceProvider.java From curator with Apache License 2.0 | 5 votes |
@Test public void testDisabledInstance() throws Exception { List<Closeable> closeables = Lists.newArrayList(); try { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); closeables.add(client); client.start(); ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).enabled(false).build(); InstanceSerializer<String> serializer = new JsonInstanceSerializer<>(String.class, false); ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).serializer(serializer).basePath("/test").client(client).thisInstance(instance).build(); closeables.add(discovery); discovery.start(); ServiceProvider<String> provider = discovery.serviceProviderBuilder().serviceName("test").build(); closeables.add(provider); provider.start(); Assert.assertEquals(provider.getInstance(), null); Assert.assertTrue(provider.getAllInstances().isEmpty(), "Disabled instance still appears available via service provider"); } finally { Collections.reverse(closeables); for ( Closeable c : closeables ) { CloseableUtils.closeQuietly(c); } } }
Example #28
Source File: TestServiceDiscoveryBuilder.java From curator with Apache License 2.0 | 5 votes |
@Test public void testDefaultSerializer() { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); ServiceDiscoveryBuilder<Object> builder = ServiceDiscoveryBuilder.builder(Object.class).client(client); ServiceDiscoveryImpl<?> discovery = (ServiceDiscoveryImpl<?>) builder.basePath("/path").build(); Assert.assertNotNull(discovery.getSerializer(), "default serializer not set"); Assert.assertTrue(discovery.getSerializer() instanceof JsonInstanceSerializer, "default serializer not JSON"); }
Example #29
Source File: ZooKeeperServiceRegistryTest.java From camel-spring-boot with Apache License 2.0 | 5 votes |
public ZooKeeperTestClient(String nodes) { curator = CuratorFrameworkFactory.builder() .connectString(nodes) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); discovery = ServiceDiscoveryBuilder.builder(ZooKeeperServiceRegistry.MetaData.class) .client(curator) .basePath(SERVICE_PATH) .serializer(new JsonInstanceSerializer<>(ZooKeeperServiceRegistry.MetaData.class)) .build(); }
Example #30
Source File: ExampleServer.java From ZKRecipesByExample with Apache License 2.0 | 5 votes |
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(); }