Java Code Examples for org.apache.curator.x.discovery.ServiceProvider#start()
The following examples show how to use
org.apache.curator.x.discovery.ServiceProvider#start() .
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: ZookeeperFetcher.java From Ratel with Apache License 2.0 | 6 votes |
@SuppressWarnings("PMD.SignatureDeclareThrowsException") public ServiceProvider getProvider(String serviceName) throws Exception { if (StringUtils.isEmpty(serviceName)) { return null; } ServiceProvider provider = providers.get(serviceName); if (provider == null) { provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName) .providerStrategy(pickInstanceStrategy).build(); provider.start(); providers.put(serviceName, provider); } return provider; }
Example 2
Source File: ZooKeeperDiscovery.java From soabase with Apache License 2.0 | 6 votes |
@Override public ServiceProvider<Payload> load(String serviceName) throws Exception { InstanceFilter<Payload> filter = new InstanceFilter<Payload>() { @Override public boolean apply(ServiceInstance<Payload> instance) { Payload payload = instance.getPayload(); if ( payload.getForcedState() == ForcedState.CLEARED ) { return (payload.getHealthyState() == HealthyState.HEALTHY); } return (payload.getForcedState() == ForcedState.REGISTER); } }; ServiceProvider<Payload> provider = discovery .serviceProviderBuilder() .serviceName(serviceName) .additionalFilter(filter) .build(); provider.start(); return provider; }
Example 3
Source File: DiscoveryExample.java From ZKRecipesByExample with Apache License 2.0 | 6 votes |
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception { // this shows how to use a ServiceProvider // in a real application you'd create the ServiceProvider early for the // service(s) you're interested in if (args.length != 1) { System.err.println("syntax error (expected random <name>): " + command); return; } String serviceName = args[0]; ServiceProvider<InstanceDetails> provider = providers.get(serviceName); if (provider == null) { provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build(); providers.put(serviceName, provider); provider.start(); Thread.sleep(2500); // give the provider time to warm up - in a real // application you wouldn't need to do this } ServiceInstance<InstanceDetails> instance = provider.getInstance(); if (instance == null) { System.err.println("No instances named: " + serviceName); } else { outputInstance(instance); } }
Example 4
Source File: ZkApplicationDiscovery.java From xian with Apache License 2.0 | 5 votes |
@Override public ServiceProvider<NodeStatus> load(String key) throws Exception { ServiceProvider<NodeStatus> serviceProvider = serviceDiscovery.serviceProviderBuilder() // 如果我传入一个不存在的service名,会成功返回一个discovery对象,然后一直返回null的服务实例 .serviceName(key) .build(); serviceProvider.start(); serviceProvider.serviceCache().addListener(new ServiceCacheListener<NodeStatus>() { @Override public void cacheChanged() { } @Override public void cacheChanged(PathChildrenCacheEvent event, ServiceInstance<NodeStatus> instance) { ApplicationInstance applicationInstance = ZkServiceInstanceAdaptor.applicationInstance(instance); switch (event.getType()) { case CHILD_ADDED: EventPublisher.publish(new NodeOnlineEvent().setInstance(applicationInstance)); break; case CHILD_REMOVED: EventPublisher.publish(new NodeOfflineEvent().setInstance(applicationInstance)); break; case CHILD_UPDATED: EventPublisher.publish(new NodeUpdatedEvent().setInstance(applicationInstance)); break; default: LOG.debug("忽略其他事件:" + event.getType()); break; } } @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { } }); return serviceProvider; }
Example 5
Source File: ZkGroupDiscovery.java From xian with Apache License 2.0 | 5 votes |
@Override public ServiceProvider<GroupProxy> load(String groupName) throws Exception { ServiceProvider<GroupProxy> serviceProvider = serviceDiscovery.serviceProviderBuilder() .serviceName(groupName) .build(); serviceProvider.start(); return serviceProvider; }
Example 6
Source File: ZkUnitDiscovery.java From xian with Apache License 2.0 | 5 votes |
@Override public ServiceProvider<UnitProxy> load(String key) throws Exception { ServiceProvider<UnitProxy> serviceProvider = serviceDiscovery.serviceProviderBuilder() .serviceName(key) .build(); serviceProvider.start(); return serviceProvider; }
Example 7
Source File: DiscoveryExample.java From xian with Apache License 2.0 | 5 votes |
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception { // this shows how to use a ServiceProvider // in a real application you'd create the ServiceProvider early for the group(s) you're interested in if ( args.length != 1 ) { System.err.println("syntax error (expected random <name>): " + command); return; } String serviceName = args[0]; ServiceProvider<InstanceDetails> provider = providers.get(serviceName); if ( provider == null ) { provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build(); providers.put(serviceName, provider); provider.start(); Thread.sleep(2500); // give the provider time to warm up - in a real application you wouldn't need to do this } ServiceInstance<InstanceDetails> instance = provider.getInstance(); if ( instance == null ) { System.err.println("No instances named: " + serviceName); } else { outputInstance(instance); } }
Example 8
Source File: TestServiceProvider.java From xian 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 9
Source File: TestServiceProvider.java From xian 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(); 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(), null); Assert.assertTrue(provider.getAllInstances().isEmpty(), "Disabled instance still appears available via group provider"); } finally { Collections.reverse(closeables); for ( Closeable c : closeables ) { CloseableUtils.closeQuietly(c); } } }
Example 10
Source File: DiscoveryExample.java From curator with Apache License 2.0 | 5 votes |
private static void listRandomInstance(String[] args, ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, String command) throws Exception { // this shows how to use a ServiceProvider // in a real application you'd create the ServiceProvider early for the service(s) you're interested in if ( args.length != 1 ) { System.err.println("syntax error (expected random <name>): " + command); return; } String serviceName = args[0]; ServiceProvider<InstanceDetails> provider = providers.get(serviceName); if ( provider == null ) { provider = serviceDiscovery.serviceProviderBuilder().serviceName(serviceName).providerStrategy(new RandomStrategy<InstanceDetails>()).build(); providers.put(serviceName, provider); provider.start(); Thread.sleep(2500); // give the provider time to warm up - in a real application you wouldn't need to do this } ServiceInstance<InstanceDetails> instance = provider.getInstance(); if ( instance == null ) { System.err.println("No instances named: " + serviceName); } else { outputInstance(instance); } }
Example 11
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 12
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); } } }