com.orbitz.consul.cache.ServiceHealthCache Java Examples

The following examples show how to use com.orbitz.consul.cache.ServiceHealthCache. 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: CatalogWatcher.java    From thorntail with Apache License 2.0 6 votes vote down vote up
private void setupWatcher(String serviceName) {
    if (watchers.containsKey(serviceName)) {
        return;
    }
    QueryOptions options = ImmutableQueryOptions.builder()
            .build();

    ServiceHealthCache healthCache = ServiceHealthCache.newCache(
            this.healthClientInjector.getValue(),
            serviceName,
            true,
            options,
            5
    );

    try {
        healthCache.addListener(new ServiceCacheListener(serviceName, this.topologyManagerInjector.getValue()));
        healthCache.start();
        healthCache.awaitInitialized(1, TimeUnit.SECONDS);
        this.watchers.put(serviceName, healthCache);
    } catch (Exception e) {
        ConsulTopologyMessages.MESSAGES.errorSettingUpCatalogWatcher(serviceName, e);
    }
}
 
Example #2
Source File: CatalogWatcher.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 6 votes vote down vote up
private void setupWatcher(String serviceName) {
    if (watchers.containsKey(serviceName)) {
        return;
    }

    CatalogOptions options = ImmutableCatalogOptions.builder()
            .build();

    ServiceHealthCache healthCache = ServiceHealthCache.newCache(
            this.healthClientInjector.getValue(),
            serviceName,
            true,
            options,
            5
    );


    try {
        healthCache.addListener(new ServiceCacheListener(serviceName, this.topologyManagerInjector.getValue()));
        healthCache.start();
        healthCache.awaitInitialized(1, TimeUnit.SECONDS);
        this.watchers.put(serviceName, healthCache);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: ConsulDiscoveryService.java    From jim-framework with Apache License 2.0 5 votes vote down vote up
@Override
public List<RpcURL> getUrls(String registryHost,int registryPort) {
    List<RpcURL> urls= Lists.newArrayList();
    Consul consul = this.buildConsul(registryHost,registryPort);
    HealthClient client = consul.healthClient();
    String name = CONSUL_NAME;
    ConsulResponse object= client.getAllServiceInstances(name);
    List<ImmutableServiceHealth> serviceHealths=(List<ImmutableServiceHealth>)object.getResponse();
    for(ImmutableServiceHealth serviceHealth:serviceHealths){
        RpcURL url=new RpcURL();
        url.setHost(serviceHealth.getService().getAddress());
        url.setPort(serviceHealth.getService().getPort());
        urls.add(url);
    }

    try {
        ServiceHealthCache serviceHealthCache = ServiceHealthCache.newCache(client, name);
        serviceHealthCache.addListener(new ConsulCache.Listener<ServiceHealthKey, ServiceHealth>() {
            @Override
            public void notify(Map<ServiceHealthKey, ServiceHealth> map) {
                logger.info("serviceHealthCache.addListener notify");
                RpcClientInvokerCache.clear();

            }
        });
        serviceHealthCache.start();
    } catch (Exception e) {
        logger.info("serviceHealthCache.start error:",e);
    }
    return urls;
}