Java Code Examples for com.ecwid.consul.v1.ConsulClient#getHealthServices()

The following examples show how to use com.ecwid.consul.v1.ConsulClient#getHealthServices() . 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: ConsulSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
@Override
public boolean sync(TaskDO taskDO) {
    try {
        ConsulClient consulClient = consulServerHolder.get(taskDO.getSourceClusterId(), null);
        NamingService destNamingService = nacosServerHolder.get(taskDO.getDestClusterId(), null);
        Response<List<HealthService>> response =
            consulClient.getHealthServices(taskDO.getServiceName(), true, QueryParams.DEFAULT);
        List<HealthService> healthServiceList = response.getValue();
        Set<String> instanceKeys = new HashSet<>();
        overrideAllInstance(taskDO, destNamingService, healthServiceList, instanceKeys);
        cleanAllOldInstance(taskDO, destNamingService, instanceKeys);
        specialSyncEventBus.subscribe(taskDO, this::sync);
    } catch (Exception e) {
        log.error("Sync task from consul to nacos was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.SYNC_ERROR);
        return false;
    }
    return true;
}
 
Example 2
Source File: ConsulConnection.java    From garmadon with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches healthy garmadon forwarder end points from consul.
 */
private List<HealthService> getHealthyEndPoints() {
    ConsulClient client = new ConsulClient("localhost");

    HealthServicesRequest request = HealthServicesRequest.newBuilder()
            .setPassing(true)
            .setQueryParams(QueryParams.DEFAULT)
            .build();
    Response<List<HealthService>> healthyServices = client.getHealthServices(serviceName, request);

    return healthyServices.getValue();
}
 
Example 3
Source File: HiveDriverConsul.java    From garmadon with Apache License 2.0 5 votes vote down vote up
/**
 * Fetches healthy service nodes
 */
private List<HealthService> getHealthyEndPoints(String serviceName) {
    ConsulClient client = new ConsulClient("localhost");

    HealthServicesRequest request = HealthServicesRequest.newBuilder()
        .setPassing(true)
        .setQueryParams(QueryParams.DEFAULT)
        .build();
    Response<List<HealthService>> healthyServices = client.getHealthServices(serviceName, request);

    return healthyServices.getValue();
}
 
Example 4
Source File: NacosSyncToConsulServiceImpl.java    From nacos-sync with Apache License 2.0 5 votes vote down vote up
@Override
public boolean delete(TaskDO taskDO) {
    try {

        NamingService sourceNamingService =
            nacosServerHolder.get(taskDO.getSourceClusterId(), taskDO.getGroupName());
        ConsulClient consulClient = consulServerHolder.get(taskDO.getDestClusterId(), taskDO.getGroupName());

        sourceNamingService.unsubscribe(taskDO.getServiceName(), nacosListenerMap.get(taskDO.getTaskId()));

        // 删除目标集群中同步的实例列表
        Response<List<HealthService>> serviceResponse =
            consulClient.getHealthServices(taskDO.getServiceName(), true, QueryParams.DEFAULT);
        List<HealthService> healthServices = serviceResponse.getValue();
        for (HealthService healthService : healthServices) {

            if (needDelete(ConsulUtils.transferMetadata(healthService.getService().getTags()), taskDO)) {
                consulClient.agentServiceDeregister(healthService.getService().getId());
            }
        }
    } catch (Exception e) {
        log.error("delete a task from nacos to nacos was failed, taskId:{}", taskDO.getTaskId(), e);
        metricsManager.recordError(MetricsStatisticsType.DELETE_ERROR);
        return false;
    }
    return true;
}