Search in sources :

Example 1 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.

the class ServiceRegistryClientImpl method getMicroserviceInstance.

@Override
public List<MicroserviceInstance> getMicroserviceInstance(String consumerId, String providerId) {
    Holder<GetInstancesResponse> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ALL, providerId), new RequestParam().addHeader("X-ConsumerId", consumerId), syncHandler(countDownLatch, GetInstancesResponse.class, holder));
    try {
        countDownLatch.await();
        if (holder.value != null) {
            return holder.value.getInstances();
        }
    } catch (Exception e) {
        LOGGER.error("query microservice instances {} failed", providerId, e);
    }
    return null;
}
Also used : Holder(javax.xml.ws.Holder) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) GetInstancesResponse(org.apache.servicecomb.serviceregistry.api.response.GetInstancesResponse) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException)

Example 2 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.

the class ServiceRegistryClientImpl method findServiceInstances.

@Override
public MicroserviceInstances findServiceInstances(String consumerId, String appId, String serviceName, String versionRule, String revision) {
    // must register self first, and then invoke findServiceInstances
    if (consumerId == null) {
        LOGGER.error("find microservice instance {}/{}/{} failed, not registered to serviceCenter.", appId, serviceName, versionRule);
        return null;
    }
    MicroserviceInstances microserviceInstances = new MicroserviceInstances();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RequestParam requestParam = new RequestParam().addQueryParam("appId", appId).addQueryParam("serviceName", serviceName).addQueryParam("version", versionRule).addHeader("X-ConsumerId", consumerId);
    if (revision != null) {
        requestParam.addQueryParam("rev", revision);
    }
    RestUtils.get(ipPort, Const.REGISTRY_API.MICROSERVICE_INSTANCES, requestParam, syncHandlerForInstances(countDownLatch, microserviceInstances));
    try {
        countDownLatch.await();
        if (!microserviceInstances.isNeedRefresh()) {
            return microserviceInstances;
        }
        if (microserviceInstances.getInstancesResponse() == null) {
            // error
            return null;
        }
        List<MicroserviceInstance> list = microserviceInstances.getInstancesResponse().getInstances();
        if (list == null) {
            microserviceInstances.getInstancesResponse().setInstances(new ArrayList<>());
        }
        return microserviceInstances;
    } catch (Exception e) {
        LOGGER.error("find microservice instance {}/{}/{} failed", appId, serviceName, versionRule, e);
    }
    return null;
}
Also used : MicroserviceInstance(org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException)

Example 3 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.

the class ServiceRegistryClientImpl method getAllMicroservices.

@Override
public List<Microservice> getAllMicroservices() {
    Holder<GetAllServicesResponse> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.get(ipPort, Const.REGISTRY_API.MICROSERVICE_OPERATION_ALL, new RequestParam(), syncHandler(countDownLatch, GetAllServicesResponse.class, holder));
    try {
        countDownLatch.await();
        if (holder.value != null) {
            return holder.value.getServices();
        }
    } catch (Exception e) {
        LOGGER.error("query all microservices failed", e);
    }
    return emptyList();
}
Also used : GetAllServicesResponse(org.apache.servicecomb.serviceregistry.api.response.GetAllServicesResponse) Holder(javax.xml.ws.Holder) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException)

Example 4 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.

the class ServiceRegistryClientImpl method updateInstanceProperties.

@Override
public boolean updateInstanceProperties(String microserviceId, String microserviceInstanceId, Map<String, String> instanceProperties) {
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    try {
        UpdatePropertiesRequest request = new UpdatePropertiesRequest();
        request.setProperties(instanceProperties);
        byte[] body = JsonUtils.writeValueAsBytes(request);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("update properties of microservice instance: {}", new String(body, Charset.defaultCharset()));
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        RestUtils.put(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_PROPERTIES, microserviceId, microserviceInstanceId), new RequestParam().setBody(body), syncHandler(countDownLatch, HttpClientResponse.class, holder));
        countDownLatch.await();
        if (holder.value != null) {
            if (holder.value.statusCode() == Status.OK.getStatusCode()) {
                return true;
            }
            LOGGER.warn(holder.value.statusMessage());
        }
    } catch (Exception e) {
        LOGGER.error("update properties of microservice instance {}/{} failed", microserviceId, microserviceInstanceId, e);
    }
    return false;
}
Also used : HttpClientResponse(io.vertx.core.http.HttpClientResponse) Holder(javax.xml.ws.Holder) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) UpdatePropertiesRequest(org.apache.servicecomb.serviceregistry.api.request.UpdatePropertiesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException)

Example 5 with IpPort

use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.

the class ServiceRegistryClientImpl method findServiceInstance.

@Override
public MicroserviceInstance findServiceInstance(String serviceId, String instanceId) {
    try {
        Holder<MicroserviceInstanceResponse> holder = new Holder<>();
        IpPort ipPort = ipPortManager.getAvailableAddress();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        RestUtils.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ONE, serviceId, instanceId), new RequestParam().addHeader("X-ConsumerId", serviceId), syncHandler(countDownLatch, MicroserviceInstanceResponse.class, holder));
        countDownLatch.await();
        if (null != holder.value) {
            return holder.value.getInstance();
        }
        return null;
    } catch (Exception e) {
        LOGGER.error("get instance from sc failed");
        return null;
    }
}
Also used : Holder(javax.xml.ws.Holder) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) MicroserviceInstanceResponse(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceResponse)

Aggregations

IpPort (org.apache.servicecomb.foundation.common.net.IpPort)69 ClientException (org.apache.servicecomb.serviceregistry.client.ClientException)36 CountDownLatch (java.util.concurrent.CountDownLatch)35 IOException (java.io.IOException)19 ExecutionException (java.util.concurrent.ExecutionException)19 Holder (javax.xml.ws.Holder)15 Test (org.junit.Test)14 HttpClientResponse (io.vertx.core.http.HttpClientResponse)12 ArrayList (java.util.ArrayList)10 Handler (io.vertx.core.Handler)6 URI (java.net.URI)6 HashMap (java.util.HashMap)6 List (java.util.List)6 Expectations (mockit.Expectations)6 MockUp (mockit.MockUp)6 Future (io.vertx.core.Future)5 HttpClientRequest (io.vertx.core.http.HttpClientRequest)5 HttpMethod (io.vertx.core.http.HttpMethod)5 SignRequest (org.apache.servicecomb.foundation.auth.SignRequest)4 HttpClientWithContext (org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext)4