org.apache.curator.x.discovery.ServiceProvider Java Examples
The following examples show how to use
org.apache.curator.x.discovery.ServiceProvider.
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: 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 #2
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 #3
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 #4
Source File: ZooKeeperDiscovery.java From soabase with Apache License 2.0 | 6 votes |
@Override public DiscoveryInstance getInstance(String serviceName) { ServiceInstance<Payload> instance; try { // TODO - validate service name ServiceProvider<Payload> provider = providers.get(serviceName); instance = provider.getInstance(); return toSoaInstance(instance); } catch ( Exception e ) { log.error("Could not service instance: " + serviceName, e); throw new RuntimeException(e); } }
Example #5
Source File: ZooKeeperDiscovery.java From soabase with Apache License 2.0 | 6 votes |
@Override public Collection<DiscoveryInstance> getAllInstances(String serviceName) { try { // TODO - validate service name ServiceProvider<Payload> provider = providers.get(serviceName); Collection<ServiceInstance<Payload>> allInstances = provider.getAllInstances(); return Collections2.transform(allInstances, new Function<ServiceInstance<Payload>, DiscoveryInstance>() { @Nullable @Override public DiscoveryInstance apply(@Nullable ServiceInstance<Payload> instance) { return toSoaInstance(instance); } }); } catch ( Exception e ) { log.error("Could not get service: " + serviceName, e); throw new RuntimeException(e); } }
Example #6
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 #7
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 #8
Source File: ZooKeeperDiscovery.java From soabase with Apache License 2.0 | 5 votes |
private FoundInstance findInstanceFromProvider(final String serviceName, final DiscoveryInstance instanceToFind) { ServiceInstance<Payload> foundInstance = null; ServiceProvider<Payload> provider = providers.getUnchecked(serviceName); if ( provider != null ) { try { foundInstance = Iterables.find ( provider.getAllInstances(), new Predicate<ServiceInstance<Payload>>() { @Override public boolean apply(ServiceInstance<Payload> instance) { SoaFeatures soaFeatures = SoaBundle.getFeatures(environment); return soaFeatures.getDeploymentGroupManager().isAnyGroupEnabled(serviceName, instance.getPayload().getDeploymentGroups()) && instanceToFind.getId().equals(instance.getId()); } }, null ); } catch ( Exception e ) { log.error("Could not find service: " + (serviceName + ":" + instanceToFind.getId()), e); throw new RuntimeException(e); } } return (foundInstance != null) ? new FoundInstance(foundInstance, provider) : null; }
Example #9
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 #10
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 #11
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 #12
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 #13
Source File: ServiceDiscoveryImpl.java From curator with Apache License 2.0 | 5 votes |
@Override public void close() throws IOException { ExceptionAccumulator accumulator = new ExceptionAccumulator(); for ( ServiceProvider<T> provider : Lists.newArrayList(providers) ) { CloseableUtils.closeQuietly(provider); } for ( Entry<T> entry : services.values() ) { try { internalUnregisterService(entry); } catch ( KeeperException.NoNodeException ignore ) { // ignore } catch ( Exception e ) { accumulator.add(e); log.error("Could not unregister instance: " + entry.service.getName(), e); } } client.getConnectionStateListenable().removeListener(connectionStateListener); accumulator.propagate(); }
Example #14
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 #15
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 #16
Source File: ServiceDiscoveryImpl.java From xian with Apache License 2.0 | 5 votes |
@Override public void close() throws IOException { for ( ServiceCache<T> cache : Lists.newArrayList(caches) ) { CloseableUtils.closeQuietly(cache); } for ( ServiceProvider<T> provider : Lists.newArrayList(providers) ) { CloseableUtils.closeQuietly(provider); } for ( Entry<T> entry : services.values() ) { try { internalUnregisterService(entry); } catch ( KeeperException.NoNodeException ignore ) { // ignore } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); log.error("Could not unregister instance: " + entry.service.getName(), e); } } client.getConnectionStateListenable().removeListener(connectionStateListener); }
Example #17
Source File: DiscoveryExample.java From xian 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 #18
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 #19
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 #20
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 #21
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 #22
Source File: ZooKeeperDiscovery.java From soabase with Apache License 2.0 | 4 votes |
@Override public void onRemoval(RemovalNotification<String, ServiceProvider<Payload>> notification) { CloseableUtils.closeQuietly(notification.getValue()); }
Example #23
Source File: DiscoveryExample.java From xian with Apache License 2.0 | 4 votes |
private static void processCommands(ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, CuratorFramework client) throws Exception { // More scaffolding that does a simple command line processor printHelp(); List<ExampleServer> servers = Lists.newArrayList(); try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); boolean done = false; while ( !done ) { System.out.print("> "); String line = in.readLine(); if ( line == null ) { break; } String command = line.trim(); String[] parts = command.split("\\s"); if ( parts.length == 0 ) { continue; } String operation = parts[0]; String args[] = Arrays.copyOfRange(parts, 1, parts.length); if ( operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?") ) { printHelp(); } else if ( operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit") ) { done = true; } else if ( operation.equals("add") ) { addInstance(args, client, command, servers); } else if ( operation.equals("delete") ) { deleteInstance(args, command, servers); } else if ( operation.equals("random") ) { listRandomInstance(args, serviceDiscovery, providers, command); } else if ( operation.equals("list") ) { listInstances(serviceDiscovery); } } } finally { for ( ExampleServer server : servers ) { CloseableUtils.closeQuietly(server); } } }
Example #24
Source File: ZooKeeperDiscovery.java From soabase with Apache License 2.0 | 4 votes |
FoundInstance(ServiceInstance<Payload> instance, ServiceProvider<Payload> provider) { this.instance = instance; this.provider = provider; }
Example #25
Source File: ZookeeperFetcher.java From Ratel with Apache License 2.0 | 4 votes |
public void close() throws IOException { for (ServiceProvider serviceProvider : providers.values()) { serviceProvider.close(); } }
Example #26
Source File: DiscoveryExample.java From curator with Apache License 2.0 | 4 votes |
private static void processCommands(ServiceDiscovery<InstanceDetails> serviceDiscovery, Map<String, ServiceProvider<InstanceDetails>> providers, CuratorFramework client) throws Exception { // More scaffolding that does a simple command line processor printHelp(); List<ExampleServer> servers = Lists.newArrayList(); try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); boolean done = false; while ( !done ) { System.out.print("> "); String line = in.readLine(); if ( line == null ) { break; } String command = line.trim(); String[] parts = command.split("\\s"); if ( parts.length == 0 ) { continue; } String operation = parts[0]; String args[] = Arrays.copyOfRange(parts, 1, parts.length); if ( operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?") ) { printHelp(); } else if ( operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit") ) { done = true; } else if ( operation.equals("add") ) { addInstance(args, client, command, servers); } else if ( operation.equals("delete") ) { deleteInstance(args, command, servers); } else if ( operation.equals("random") ) { listRandomInstance(args, serviceDiscovery, providers, command); } else if ( operation.equals("list") ) { listInstances(serviceDiscovery); } } } finally { for ( ExampleServer server : servers ) { CloseableUtils.closeQuietly(server); } } }
Example #27
Source File: ServiceProviderBuilderImpl.java From curator with Apache License 2.0 | 4 votes |
public ServiceProvider<T> build() { return new ServiceProviderImpl<T>(discovery, serviceName, providerStrategy, threadFactory, executorService, filters, downInstancePolicy); }
Example #28
Source File: ServiceProviderBuilderImpl.java From xian with Apache License 2.0 | 4 votes |
public ServiceProvider<T> build() { return new ServiceProviderImpl<T>(discovery, serviceName, providerStrategy, threadFactory, filters, downInstancePolicy); }
Example #29
Source File: ServiceDiscoveryImpl.java From curator with Apache License 2.0 | 4 votes |
void providerOpened(ServiceProvider<T> provider) { providers.add(provider); }
Example #30
Source File: ServiceDiscoveryImpl.java From curator with Apache License 2.0 | 4 votes |
void providerClosed(ServiceProvider<T> cache) { providers.remove(cache); }