本文整理汇总了Java中org.apache.curator.x.discovery.ServiceDiscovery类的典型用法代码示例。如果您正苦于以下问题:Java ServiceDiscovery类的具体用法?Java ServiceDiscovery怎么用?Java ServiceDiscovery使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServiceDiscovery类属于org.apache.curator.x.discovery包,在下文中一共展示了ServiceDiscovery类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: registerResource
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
private ServiceInstance<String> registerResource(final RegisteredService registeredService,
final ServiceDiscovery<String> serviceDiscovery) {
try {
UriSpec uriSpec = new UriSpec("{scheme}://{address}:{port}" +
ResourcePaths.ROOT_PATH +
registeredService.getVersionedPath());
ServiceInstance<String> serviceInstance = ServiceInstance.<String>builder()
.serviceType(ServiceType.DYNAMIC) //==ephemeral zk nodes so instance will disappear if we lose zk conn
.uriSpec(uriSpec)
.name(registeredService.getVersionedServiceName(stroomPropertyService))
.address(advertisedHostNameOrIpAddress)
.port(advertisedPort)
.build();
LOGGER.info("Attempting to register '{}' with service discovery at {}",
registeredService.getVersionedServiceName(stroomPropertyService), serviceInstance.buildUriSpec());
Preconditions.checkNotNull(serviceDiscovery).registerService(serviceInstance);
LOGGER.info("Successfully registered '{}' service.", registeredService.getVersionedServiceName(stroomPropertyService));
return serviceInstance;
} catch (Exception e) {
throw new RuntimeException("Failed to register service " + registeredService.getVersionedServiceName(stroomPropertyService), e);
}
}
示例2: getGeolocationServiceProvider
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
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;
}
开发者ID:PacktPublishing,项目名称:Microservices-Deployment-Cookbook,代码行数:19,代码来源:ZookeeperServiceDiscovery.java
示例3: startZookeeper
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
@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);
}
示例4: buildServiceDiscoveryWith
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
private ServiceDiscovery<ZookeeperInstance> buildServiceDiscoveryWith(ZookeeperConfiguration configuration, CuratorFramework curator,
InstanceSerializer<ZookeeperInstance> serializer) {
try {
if (!CuratorFrameworkState.STARTED.equals(curator.getState())) {
curator.start();
}
ServiceDiscovery<ZookeeperInstance> serviceDiscovery = ServiceDiscoveryBuilder.builder(ZookeeperInstance.class)
.client(curator)
.basePath(configuration.root())
.serializer(serializer)
.build();
serviceDiscovery.start();
return serviceDiscovery;
} catch (Exception e) {
throw new ZookeeperServiceDiscoveryException("Error on create Zookeeper ServiceDiscovery", e);
}
}
示例5: verifyRegistrations
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
/**
* Verify services are registered.
*
* @throws Exception
*/
public void verifyRegistrations() throws Exception {
ServiceDiscovery<MetaData> serviceDiscovery = ServiceUtil.getServiceDiscovery(curatorFramework, basePath);
listInstances(serviceDiscovery);
for (Map.Entry<ServiceDiscovery<MetaData>, ServiceInstance<MetaData>> entry : discoveryMap.entrySet()) {
ServiceInstance<MetaData> instance = entry.getValue();
try {
ServiceInstance<MetaData> found = entry.getKey().queryForInstance(instance.getName(), instance.getId());
if (found == null) {
throw new RuntimeException("There is no instance for: " + instance.getName() + ":" + instance.getId() + " registered ");
}
log.debug(found.getName() + " is verified at: " + found.getAddress() + ":" + found.getPort());
} catch (Exception e) {
log.error("Could not find service: " + (instance.getName() + ":" + instance.getId()), e);
throw new RuntimeException(e);
}
}
}
示例6: unregisterService
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
/**
* unregister Availability.
*
* @return the registration client
*/
public CuratorManager unregisterService() {
active.set(false);
for (Map.Entry<ServiceDiscovery<MetaData>, ServiceInstance<MetaData>> entry : discoveryMap.entrySet()) {
ServiceInstance<MetaData> instance = entry.getValue();
String serviceName = instance.getName();
try {
log.debug("unregistering service: " + serviceName);
entry.getKey().unregisterService(instance);
log.info("service unregistered: " + serviceName);
} catch (Exception e) {
log.error("Unregistration exception: ", e);
} finally {
try {
entry.getKey().close();
} catch (IOException ignore) {
}
}
}
return this;
}
示例7: listInstances
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
public void listInstances(ServiceDiscovery<MetaData> serviceDiscovery) throws Exception {
// This shows how to query all the instances in service discovery
try {
Collection<String> serviceNames = serviceDiscovery.queryForNames();
for (String serviceName : serviceNames) {
Collection<ServiceInstance<MetaData>> instances = serviceDiscovery.queryForInstances(serviceName);
log.info("serviceName: " + serviceName);
for (ServiceInstance<MetaData> instance : instances) {
outputInstance(instance);
}
}
} catch (Exception e) {
/* * Something bad did happen, but carry on
*/
log.error(e.getMessage(), e);
}
}
示例8: listServiceInstance
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
public List<String> listServiceInstance() throws Exception {
List<String> list = new ArrayList<String>();
ServiceDiscovery<MetaData> serviceDiscovery = ServiceUtil.getServiceDiscovery(curatorFramework, basePath);
try {
Collection<String> serviceNames = serviceDiscovery.queryForNames();
for (String serviceName : serviceNames) {
Collection<ServiceInstance<MetaData>> instances = serviceDiscovery.queryForInstances(serviceName);
log.info("serviceName: " + serviceName);
for (ServiceInstance<MetaData> instance : instances) {
list.add(instance.getPayload().getListenAddress() + ":" + instance.getPayload().getListenPort());
outputInstance(instance);
}
}
} catch (Exception e) {
/* * Something bad did happen, but carry on
*/
log.error(e.getMessage(), e);
}
Collections.sort(list);
log.debug("listsize:" + list.size());
return list;
}
示例9: listInstances
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
private static void listInstances(ServiceDiscovery<InstanceDetails> serviceDiscovery) throws Exception
{
// This shows how to query all the instances in service discovery
try
{
Collection<String> serviceNames = serviceDiscovery.queryForNames();
System.out.println(serviceNames.size() + " type(s)");
for ( String serviceName : serviceNames )
{
Collection<ServiceInstance<InstanceDetails>> instances = serviceDiscovery.queryForInstances(serviceName);
System.out.println(serviceName);
for ( ServiceInstance<InstanceDetails> instance : instances )
{
outputInstance(instance);
}
}
}
finally
{
CloseableUtils.closeQuietly(serviceDiscovery);
}
}
示例10: createServiceProvider
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
synchronized private void createServiceProvider() {
ServiceDiscovery<Map> serviceDiscovery = ServiceDiscoveryBuilder.builder(Map.class)
.client(curator)
.basePath(Constants.SERVICES_PATH)
.serializer(new ServiceInstanceSerializer<>(Map.class))
.build();
serviceProvider = serviceDiscovery.serviceProviderBuilder()
.serviceName(serviceName)
.providerStrategy(new RoundRobinStrategy<>())
// .threadFactory()
// .additionalFilter()
// .downInstancePolicy()
.build();
try {
serviceProvider.start();
log.debug("service provider started working.");
} catch (Exception ex) {
throw new ApiCallException("Cannot start service provider working", ex);
}
}
示例11: testGetServersWithInstanceStatus
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void testGetServersWithInstanceStatus() throws Exception {
ArrayList<ServiceInstance<ZookeeperInstance>> instances = new ArrayList<>();
instances.add(serviceInstance(1, STATUS_UP));
instances.add(serviceInstance(2, STATUS_OUT_OF_SERVICE));
ServiceDiscovery<ZookeeperInstance> serviceDiscovery = mock(ServiceDiscovery.class);
when(serviceDiscovery.queryForInstances(nullable(String.class))).thenReturn(instances);
ZookeeperServerList serverList = new ZookeeperServerList(serviceDiscovery);
List<ZookeeperServer> servers = serverList.getInitialListOfServers();
assertThat(servers).hasSize(1);
assertThat(servers.get(0).getInstance().getPayload().getMetadata())
.contains(MapEntry.entry(INSTANCE_STATUS_KEY, STATUS_UP));
}
示例12: testBasic
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
@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);
}
}
示例13: testCleaning
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
@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);
}
}
示例14: main
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
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);
}
}
示例15: listRandomInstance
import org.apache.curator.x.discovery.ServiceDiscovery; //导入依赖的package包/类
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);
}
}