com.orbitz.consul.HealthClient Java Examples

The following examples show how to use com.orbitz.consul.HealthClient. 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: PropertyConfig.java    From kafka-topic-exporter with Apache License 2.0 6 votes vote down vote up
ArrayList<String> getBootStrapServersFromConsul(String consulServer, String consulKafkaServicename) {
    ArrayList<String> bootstrapServers = null;
    LOG.info("CONSUL: Consul server used: [" + consulServer + "]");
    LOG.info("CONSUL: Consul service retrieved: [" + consulKafkaServicename + "]");
    try {
        Consul consul = Consul.builder().withUrl(consulServer).build();
        HealthClient healthClient = consul.healthClient();

        // discover only "passing" nodes
        List<ServiceHealth> nodes = healthClient.getHealthyServiceInstances(consulKafkaServicename).getResponse();
        for (ServiceHealth kafkaNode : nodes) {
            String address = kafkaNode.getService().getAddress();
            String port = kafkaNode.getService().getPort() + "";
            if (bootstrapServers == null) {
                bootstrapServers = new ArrayList<String>();
            }
            bootstrapServers.add(address + ":" + port);
        }
    } catch (Exception e){
        LOG.error("CONSUL: " + e.toString());
    }
    LOG.info("CONSUL: Kafka services found through Consul server: " + (bootstrapServers==null?"None available":bootstrapServers.toString()));
    return bootstrapServers;
}
 
Example #2
Source File: ConsulCoordinator.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public List<RemoteInstance> queryRemoteNodes() {
    HealthClient healthClient = client.healthClient();

    // Discover only "passing" nodes
    List<ServiceHealth> nodes = healthClient.getHealthyServiceInstances(serviceName).getResponse();

    List<RemoteInstance> remoteInstances = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(nodes)) {
        nodes.forEach(node -> {
            if (!Strings.isNullOrEmpty(node.getService().getAddress())) {
                Address address = new Address(node.getService().getAddress(), node.getService().getPort(), false);
                if (address.equals(selfAddress)) {
                    address.setSelf(true);
                }
                remoteInstances.add(new RemoteInstance(address));
            }
        });
    }
    return remoteInstances;
}
 
Example #3
Source File: ConsulModule.java    From nano-framework with Apache License 2.0 6 votes vote down vote up
@Override
public void configure(final Binder binder) {
    loading();

    cfgs.forEach((id, cfg) -> {
        final Consul consul = build(cfg);
        binder.bind(Consul.class).annotatedWith(Names.named(CONSUL_PREFIX + id)).toInstance(consul);
        binder.bind(AgentClient.class).annotatedWith(Names.named(CONSUL_AGENT_CLIENT_PREFIX + id)).toInstance(consul.agentClient());
        binder.bind(HealthClient.class).annotatedWith(Names.named(CONSUL_HEALTH_CLIENT_PREFIX + id)).toInstance(consul.healthClient());
        binder.bind(KeyValueClient.class).annotatedWith(Names.named(CONSUL_KEY_VALUE_CLIENT_PREFIX + id)).toInstance(consul.keyValueClient());
        binder.bind(CatalogClient.class).annotatedWith(Names.named(CONSUL_CATALOG_CLIENT_PREFIX + id)).toInstance(consul.catalogClient());
        binder.bind(StatusClient.class).annotatedWith(Names.named(CONSUL_STATUS_CLIENT_PREFIX + id)).toInstance(consul.statusClient());
        binder.bind(SessionClient.class).annotatedWith(Names.named(CONSUL_SESSION_CLIENT_PREFIX + id)).toInstance(consul.sessionClient());
        binder.bind(EventClient.class).annotatedWith(Names.named(CONSUL_EVENT_CLIENT_PREFIX + id)).toInstance(consul.eventClient());
        binder.bind(PreparedQueryClient.class).annotatedWith(Names.named(CONSUL_PREPARED_QUERY_CLIENT_PREFIX + id))
                .toInstance(consul.preparedQueryClient());
        binder.bind(CoordinateClient.class).annotatedWith(Names.named(CONSUL_COORDINATE_CLIENT_PREFIX + id))
                .toInstance(consul.coordinateClient());
        binder.bind(OperatorClient.class).annotatedWith(Names.named(CONSUL_OPERATOR_CLIENT + id)).toInstance(consul.operatorClient());
    });

}
 
Example #4
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;
}
 
Example #5
Source File: ConsulTopologyConnector.java    From thorntail with Apache License 2.0 5 votes vote down vote up
@Override
public void start(StartContext startContext) throws StartException {
    ServiceTarget target = startContext.getChildTarget();

    CatalogWatcher watcher = new CatalogWatcher();
    target.addService(CatalogWatcher.SERVICE_NAME, watcher)
            .addDependency(CatalogClientService.SERVICE_NAME, CatalogClient.class, watcher.getCatalogClientInjector())
            .addDependency(HealthClientService.SERIVCE_NAME, HealthClient.class, watcher.getHealthClientInjector())
            .addDependency(TopologyManagerActivator.SERVICE_NAME, TopologyManager.class, watcher.getTopologyManagerInjector())
            .install();


}
 
Example #6
Source File: ConsulTopologyConnector.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 5 votes vote down vote up
@Override
public void start(StartContext startContext) throws StartException {
    ServiceTarget target = startContext.getChildTarget();

    ConsulService consul = new ConsulService(this.url);
    target.addService(ConsulService.SERVICE_NAME, consul)
            .install();

    HealthClientService healthClient = new HealthClientService();
    target.addService(HealthClientService.SERIVCE_NAME, healthClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, healthClient.getConsulInjector())
            .install();

    AgentClientService agentClient = new AgentClientService();
    target.addService(AgentClientService.SERVICE_NAME, agentClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, agentClient.getConsulInjector())
            .install();

    CatalogClientService catalogClient = new CatalogClientService();
    target.addService(CatalogClientService.SERVICE_NAME, catalogClient)
            .addDependency(ConsulService.SERVICE_NAME, Consul.class, catalogClient.getConsulInjector())
            .install();


    this.advertiser = new Advertiser();
    target.addService(Advertiser.SERVICE_NAME, advertiser)
            .addDependency(AgentClientService.SERVICE_NAME, AgentClient.class, advertiser.getAgentClientInjector())
            .install();

    CatalogWatcher watcher = new CatalogWatcher();
    target.addService(CatalogWatcher.SERVICE_NAME, watcher)
            .addDependency(CatalogClientService.SERVICE_NAME, CatalogClient.class, watcher.getCatalogClientInjector())
            .addDependency(HealthClientService.SERIVCE_NAME, HealthClient.class, watcher.getHealthClientInjector())
            .addDependency(TopologyManager.SERVICE_NAME, TopologyManager.class, watcher.getTopologyManagerInjector())
            .install();
}
 
Example #7
Source File: CatalogWatcher.java    From thorntail with Apache License 2.0 4 votes vote down vote up
public Injector<HealthClient> getHealthClientInjector() {
    return this.healthClientInjector;
}
 
Example #8
Source File: HealthClientService.java    From thorntail with Apache License 2.0 4 votes vote down vote up
@Override
public HealthClient getValue() throws IllegalStateException, IllegalArgumentException {
    return this.healthClient;
}
 
Example #9
Source File: CatalogWatcher.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
public Injector<HealthClient> getHealthClientInjector() {
    return this.healthClientInjector;
}
 
Example #10
Source File: HealthClientService.java    From ARCHIVE-wildfly-swarm with Apache License 2.0 4 votes vote down vote up
@Override
public HealthClient getValue() throws IllegalStateException, IllegalArgumentException {
    return this.healthClient;
}
 
Example #11
Source File: ConsulCoordinatorTest.java    From skywalking with Apache License 2.0 3 votes vote down vote up
@Before
public void setUp() {

    consulConfig.setServiceName(SERVICE_NAME);

    coordinator = new ConsulCoordinator(consulConfig, consul);

    consulResponse = mock(ConsulResponse.class);

    HealthClient healthClient = mock(HealthClient.class);
    when(healthClient.getHealthyServiceInstances(anyString())).thenReturn(consulResponse);

    when(consul.healthClient()).thenReturn(healthClient);
    when(consul.agentClient()).thenReturn(agentClient);
}