Search in sources :

Example 1 with CatalogServicesRequest

use of com.ecwid.consul.v1.catalog.CatalogServicesRequest in project spring-cloud-consul by spring-cloud.

the class ConsulReactiveDiscoveryClient method getServices.

@Override
public Flux<String> getServices() {
    return Flux.defer(() -> {
        CatalogServicesRequest request = CatalogServicesRequest.newBuilder().setToken(properties.getAclToken()).setQueryParams(QueryParams.DEFAULT).build();
        Response<Map<String, List<String>>> services = client.getCatalogServices(request);
        return services == null ? Flux.empty() : Flux.fromIterable(services.getValue().keySet());
    }).onErrorResume(exception -> {
        logger.error("Error getting services from Consul.", exception);
        return Flux.empty();
    }).subscribeOn(Schedulers.boundedElastic());
}
Also used : ConsulClient(com.ecwid.consul.v1.ConsulClient) Logger(org.slf4j.Logger) ReactiveDiscoveryClient(org.springframework.cloud.client.discovery.ReactiveDiscoveryClient) LoggerFactory(org.slf4j.LoggerFactory) HealthService(com.ecwid.consul.v1.health.model.HealthService) QueryParams(com.ecwid.consul.v1.QueryParams) ArrayList(java.util.ArrayList) CatalogServicesRequest(com.ecwid.consul.v1.catalog.CatalogServicesRequest) Flux(reactor.core.publisher.Flux) List(java.util.List) Map(java.util.Map) ConsulServiceInstance(org.springframework.cloud.consul.discovery.ConsulServiceInstance) Schedulers(reactor.core.scheduler.Schedulers) Response(com.ecwid.consul.v1.Response) ServiceInstance(org.springframework.cloud.client.ServiceInstance) HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) Collections(java.util.Collections) ConsulDiscoveryProperties(org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties) CatalogServicesRequest(com.ecwid.consul.v1.catalog.CatalogServicesRequest) Map(java.util.Map)

Example 2 with CatalogServicesRequest

use of com.ecwid.consul.v1.catalog.CatalogServicesRequest in project spring-cloud-consul by spring-cloud.

the class ConsulCatalogWatch method catalogServicesWatch.

@Timed("consul.watch-catalog-services")
public void catalogServicesWatch() {
    try {
        long index = -1;
        if (this.catalogServicesIndex.get() != null) {
            index = this.catalogServicesIndex.get().longValue();
        }
        CatalogServicesRequest request = CatalogServicesRequest.newBuilder().setQueryParams(new QueryParams(this.properties.getCatalogServicesWatchTimeout(), index)).setToken(this.properties.getAclToken()).build();
        Response<Map<String, List<String>>> response = this.consul.getCatalogServices(request);
        Long consulIndex = response.getConsulIndex();
        if (consulIndex != null) {
            this.catalogServicesIndex.set(BigInteger.valueOf(consulIndex));
        }
        if (log.isTraceEnabled()) {
            log.trace("Received services update from consul: " + response.getValue() + ", index: " + consulIndex);
        }
        this.publisher.publishEvent(new HeartbeatEvent(this, consulIndex));
    } catch (Exception e) {
        log.error("Error watching Consul CatalogServices", e);
    }
}
Also used : HeartbeatEvent(org.springframework.cloud.client.discovery.event.HeartbeatEvent) CatalogServicesRequest(com.ecwid.consul.v1.catalog.CatalogServicesRequest) QueryParams(com.ecwid.consul.v1.QueryParams) Map(java.util.Map) Timed(io.micrometer.core.annotation.Timed)

Example 3 with CatalogServicesRequest

use of com.ecwid.consul.v1.catalog.CatalogServicesRequest in project polaris-java by polarismesh.

the class ConsulAPIConnector method syncGetServices.

@Override
public Services syncGetServices(ServiceUpdateTask serviceUpdateTask) {
    Services services = new ServicesByProto(new ArrayList<>());
    try {
        CatalogServicesRequest request = CatalogServicesRequest.newBuilder().setQueryParams(QueryParams.DEFAULT).build();
        ArrayList<String> serviceList = new ArrayList<>(this.consulClient.getCatalogServices(request).getValue().keySet());
        for (String s : serviceList) {
            ServiceInfo serviceInfo = new ServiceInfo();
            serviceInfo.setService(s);
            services.getServices().add(serviceInfo);
        }
    } catch (ConsulException e) {
        throw ServerErrorResponseException.build(ErrorCode.SERVER_USER_ERROR.ordinal(), String.format("Get services of %s instances sync failed.", serviceUpdateTask.getServiceEventKey().getServiceKey()));
    }
    return services;
}
Also used : ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) Services(com.tencent.polaris.api.pojo.Services) ConsulException(com.ecwid.consul.ConsulException) CatalogServicesRequest(com.ecwid.consul.v1.catalog.CatalogServicesRequest) ArrayList(java.util.ArrayList) ServicesByProto(com.tencent.polaris.client.pojo.ServicesByProto)

Aggregations

CatalogServicesRequest (com.ecwid.consul.v1.catalog.CatalogServicesRequest)3 QueryParams (com.ecwid.consul.v1.QueryParams)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ConsulException (com.ecwid.consul.ConsulException)1 ConsulClient (com.ecwid.consul.v1.ConsulClient)1 Response (com.ecwid.consul.v1.Response)1 HealthServicesRequest (com.ecwid.consul.v1.health.HealthServicesRequest)1 HealthService (com.ecwid.consul.v1.health.model.HealthService)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 Services (com.tencent.polaris.api.pojo.Services)1 ServicesByProto (com.tencent.polaris.client.pojo.ServicesByProto)1 Timed (io.micrometer.core.annotation.Timed)1 Collections (java.util.Collections)1 List (java.util.List)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 ReactiveDiscoveryClient (org.springframework.cloud.client.discovery.ReactiveDiscoveryClient)1 HeartbeatEvent (org.springframework.cloud.client.discovery.event.HeartbeatEvent)1