本文整理汇总了Java中org.apache.curator.x.discovery.ServiceDiscovery.queryForInstances方法的典型用法代码示例。如果您正苦于以下问题:Java ServiceDiscovery.queryForInstances方法的具体用法?Java ServiceDiscovery.queryForInstances怎么用?Java ServiceDiscovery.queryForInstances使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.curator.x.discovery.ServiceDiscovery
的用法示例。
在下文中一共展示了ServiceDiscovery.queryForInstances方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
}
示例2: 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;
}
示例3: 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);
}
}
示例4: 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);
}
}
示例5: getWorkers
import org.apache.curator.x.discovery.ServiceDiscovery; //导入方法依赖的package包/类
public Collection<ServiceInstance<MetaData>> getWorkers(String serviceName) throws Exception {
Collection<ServiceInstance<MetaData>> instances;
ServiceDiscovery<MetaData> serviceDiscovery = ServiceUtil.getServiceDiscovery(curatorFramework, basePath);
try {
instances = serviceDiscovery.queryForInstances(serviceName);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw Throwables.propagate(e);
}
return instances;
}
示例6: registerLocalService
import org.apache.curator.x.discovery.ServiceDiscovery; //导入方法依赖的package包/类
/**
* Advertise availability.
*
* @return the registration client
*/
public CuratorManager registerLocalService() throws Exception {
if (active.getAndSet(true)) {
throw new IllegalStateException("This client instance is already available.");
}
try {
if (curatorFramework.getState() != CuratorFrameworkState.STARTED) {
curatorFramework.start();
}
for (Map.Entry<String, Integer> entry : services.entrySet()) {
String serviceName = entry.getKey();
int port = entry.getValue().intValue();
ServiceDiscovery<MetaData> serviceDiscovery = ServiceUtil.getServiceDiscovery(curatorFramework, basePath);
ServiceInstance<MetaData> service = ServiceUtil.configureServiceInstance(serviceName, port, listenAddress, parameters);
/* * Having >1 instance with of the same name with same listenAddress + listPort is
* bad. Incur some overhead to look for duplicates and explode appropriately
*/
Collection<ServiceInstance<MetaData>> candidates = serviceDiscovery.queryForInstances(serviceName);
for (ServiceInstance<MetaData> worker : candidates) {
outputInstance(worker);
if ((worker.getAddress().equals(service.getAddress())) && (worker.getPort() == port)) {
log.error("An instance of " + service + " already exists at: " +
service.getAddress() + ":" + port);
throw new IllegalStateException("Duplicate service being registered. for service: " +
serviceName + " at: " + basePath);
}
}
log.debug("registering service: " + serviceName);
serviceDiscovery.registerService(service);
discoveryMap.put(serviceDiscovery, service);
log.info("registered service: " + serviceName);
}
} catch (RuntimeException rte) {
throw rte;
} catch (Exception e) {
throw Throwables.propagate(e);
}
return this;
}
示例7: main
import org.apache.curator.x.discovery.ServiceDiscovery; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
CuratorFramework client = CuratorFrameworkFactory
.newClient("ec2-54-211-43-119.compute-1.amazonaws.com:2181,ec2-54-198-130-168.compute-1.amazonaws.com,127.0.0.1:2181",
new ExponentialBackoffRetry(1000, 3));
client.start();
ServiceDetails deployment = new ServiceDetails()
.setId("deployment")
.setName("deployment")
.setEndpoint("http://localhost:8080/excalibur/deployment/v1/")
.setVersion("v1.0")
.setProvider("aws")
.setPort(8080);
ServiceDetails workflow = new ServiceDetails()
.setId("workflow")
.setName("workflow")
.setEndpoint("http://localhost:8080/excalibur/workflow/v1/")
.setVersion("1")
.setProvider("aws")
.setPort(8080);
ObjectMapper mapper = new ObjectMapper();
// String data = mapper.writeValueAsString(service);
// client.create().forPath("/services");
// client.create().withMode(CreateMode.EPHEMERAL).forPath("/services/deployment", data.getBytes());
// mapper.readValue(client.getData().forPath("/services/deployment"), ServiceDetails.class);
ServiceDiscovery<ServiceDetails> discovery = ServiceDiscoveryBuilder.builder(ServiceDetails.class)
.client(client)
.basePath("/services")
.build();
InstanceServer server = new InstanceServer(client, "/services", deployment);
server.start();
new InstanceServer(client, "/services", workflow).start();
new InstanceServer(client, "/services", deployment).start();
// ServiceInstance<ServiceDetails> instance = ServiceInstance.<ServiceDetails>builder()
// .address(service.getEndpoint())
// .id(service.getId())
// .name(service.getName())
// .payload(service)
// .serviceType(ServiceType.DYNAMIC).build();
//
// discovery.registerService(instance);
Collection<ServiceInstance<ServiceDetails>> services = discovery.queryForInstances("deployment");
// ServiceProvider<ServiceDetails> provider = discovery.serviceProviderBuilder()
// .serviceName("workflow")
//// .providerStrategy(new ProviderStrategy<ServiceDetails>()
//// {
//// @Override
//// public ServiceInstance<ServiceDetails> getInstance(InstanceProvider<ServiceDetails> instanceProvider) throws Exception
//// {
//// List<ServiceInstance<ServiceDetails>> instances = instanceProvider.getInstances();
//// System.err.println(instances.size());
////
//// return null;
//// }
//// })
// .build();
// provider.start();
// ServiceInstance<ServiceDetails> instance2 = provider.getInstance();
for (ServiceInstance<ServiceDetails> serviceInstance : services)
{
System.out.println(serviceInstance);
}
server.close();
}