Search in sources :

Example 1 with ServiceHealth

use of com.orbitz.consul.model.health.ServiceHealth in project wildfly-swarm by wildfly-swarm.

the class ServiceCacheListener method notify.

@Override
public void notify(Map<HostAndPort, ServiceHealth> newValues) {
    Set<Registration> previousEntries = topologyManager.registrationsForService(this.name);
    Set<Registration> newEntries = newValues.values().stream().map(e -> new Registration("consul", this.name, e.getService().getAddress(), e.getService().getPort()).addTags(e.getService().getTags())).collect(Collectors.toSet());
    previousEntries.stream().filter(e -> !newEntries.contains(e)).forEach(e -> {
        this.topologyManager.unregister(e);
    });
    newEntries.stream().filter(e -> !previousEntries.contains(e)).forEach(e -> {
        this.topologyManager.register(e);
    });
}
Also used : Registration(org.wildfly.swarm.topology.runtime.Registration) ServiceHealth(com.orbitz.consul.model.health.ServiceHealth) Listener(com.orbitz.consul.cache.ConsulCache.Listener) Map(java.util.Map) Set(java.util.Set) TopologyManager(org.wildfly.swarm.topology.runtime.TopologyManager) Collectors(java.util.stream.Collectors) HostAndPort(com.google.common.net.HostAndPort) Registration(org.wildfly.swarm.topology.runtime.Registration)

Example 2 with ServiceHealth

use of com.orbitz.consul.model.health.ServiceHealth in project jim-framework by jiangmin168168.

the class ConsulDiscoveryService method getUrls.

@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;
}
Also used : ConsulResponse(com.orbitz.consul.model.ConsulResponse) ServiceHealth(com.orbitz.consul.model.health.ServiceHealth) ImmutableServiceHealth(com.orbitz.consul.model.health.ImmutableServiceHealth) HealthClient(com.orbitz.consul.HealthClient) Consul(com.orbitz.consul.Consul) ImmutableServiceHealth(com.orbitz.consul.model.health.ImmutableServiceHealth) RpcURL(com.jim.framework.rpc.common.RpcURL) ServiceHealthCache(com.orbitz.consul.cache.ServiceHealthCache) ServiceHealthKey(com.orbitz.consul.cache.ServiceHealthKey) ConsulCache(com.orbitz.consul.cache.ConsulCache) List(java.util.List)

Aggregations

ServiceHealth (com.orbitz.consul.model.health.ServiceHealth)2 HostAndPort (com.google.common.net.HostAndPort)1 RpcURL (com.jim.framework.rpc.common.RpcURL)1 Consul (com.orbitz.consul.Consul)1 HealthClient (com.orbitz.consul.HealthClient)1 ConsulCache (com.orbitz.consul.cache.ConsulCache)1 Listener (com.orbitz.consul.cache.ConsulCache.Listener)1 ServiceHealthCache (com.orbitz.consul.cache.ServiceHealthCache)1 ServiceHealthKey (com.orbitz.consul.cache.ServiceHealthKey)1 ConsulResponse (com.orbitz.consul.model.ConsulResponse)1 ImmutableServiceHealth (com.orbitz.consul.model.health.ImmutableServiceHealth)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Registration (org.wildfly.swarm.topology.runtime.Registration)1 TopologyManager (org.wildfly.swarm.topology.runtime.TopologyManager)1