Search in sources :

Example 61 with IpPort

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

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);
        restClientUtil.get(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ONE, serviceId, instanceId), new RequestParam().addHeader("X-ConsumerId", serviceId).addQueryParam("global", "true"), 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 : IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) MicroserviceInstanceResponse(org.apache.servicecomb.serviceregistry.api.response.MicroserviceInstanceResponse)

Example 62 with IpPort

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

the class ServiceRegistryClientImpl method watch.

public void watch(String selfMicroserviceId, AsyncResultCallback<MicroserviceInstanceChangedEvent> callback, AsyncResultCallback<Void> onOpen, AsyncResultCallback<Void> onClose) {
    Boolean alreadyWatch = watchServices.get(selfMicroserviceId);
    if (alreadyWatch == null) {
        synchronized (ServiceRegistryClientImpl.class) {
            alreadyWatch = watchServices.get(selfMicroserviceId);
            if (alreadyWatch == null) {
                watchServices.put(selfMicroserviceId, true);
                String url = String.format(Const.REGISTRY_API.MICROSERVICE_WATCH, selfMicroserviceId);
                IpPort ipPort = ipPortManager.getAvailableAddress();
                websocketClientUtil.open(ipPort, url, o -> {
                    onOpen.success(o);
                    LOGGER.info("watching microservice {} successfully, " + "the chosen service center address is {}:{}", selfMicroserviceId, ipPort.getHostOrIp(), ipPort.getPort());
                }, c -> {
                    watchErrorHandler(new ClientException("connection is closed accidentally"), selfMicroserviceId, callback);
                    onClose.success(null);
                }, bodyBuffer -> {
                    MicroserviceInstanceChangedEvent response;
                    try {
                        response = JsonUtils.readValue(bodyBuffer.getBytes(), MicroserviceInstanceChangedEvent.class);
                    } catch (Exception e) {
                        LOGGER.error("watcher handle microservice {} response failed, {}", selfMicroserviceId, bodyBuffer.toString());
                        return;
                    }
                    try {
                        callback.success(response);
                    } catch (Exception e) {
                        LOGGER.error("notify watcher failed, microservice {}", selfMicroserviceId, e);
                    }
                }, e -> {
                    watchErrorHandler(e, selfMicroserviceId, callback);
                    onClose.success(null);
                }, f -> {
                    watchErrorHandler(f, selfMicroserviceId, callback);
                });
            }
        }
    }
}
Also used : MicroserviceInstanceChangedEvent(org.apache.servicecomb.registry.api.event.MicroserviceInstanceChangedEvent) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 63 with IpPort

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

the class ServiceRegistryClientImpl method updateMicroserviceInstanceStatus.

@Override
public boolean updateMicroserviceInstanceStatus(String microserviceId, String instanceId, MicroserviceInstanceStatus status) {
    if (null == status) {
        throw new IllegalArgumentException("null status is now allowed");
    }
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    try {
        LOGGER.debug("update status of microservice instance: {}", status);
        String url = String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_STATUS, microserviceId, instanceId);
        Map<String, String[]> queryParams = new HashMap<>();
        queryParams.put("value", new String[] { status.toString() });
        CountDownLatch countDownLatch = new CountDownLatch(1);
        restClientUtil.put(ipPort, url, new RequestParam().setQueryParams(queryParams), 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 status of microservice instance {}/{} failed", microserviceId, instanceId, e);
    }
    return false;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HttpClientResponse(io.vertx.core.http.HttpClientResponse) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 64 with IpPort

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

the class ServiceRegistryClientImpl method getAllMicroservices.

@Override
public List<Microservice> getAllMicroservices() {
    Holder<GetAllServicesResponse> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.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) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(org.apache.servicecomb.serviceregistry.client.ClientException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 65 with IpPort

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

the class ServiceRegistryClientImpl method updateMicroserviceProperties.

@Override
public boolean updateMicroserviceProperties(String microserviceId, Map<String, String> serviceProperties) {
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    try {
        UpdatePropertiesRequest request = new UpdatePropertiesRequest();
        request.setProperties(serviceProperties);
        byte[] body = JsonUtils.writeValueAsBytes(request);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("update properties of microservice: {}", new String(body, Charset.defaultCharset()));
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        restClientUtil.put(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_PROPERTIES, microserviceId), 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 {} failed", microserviceId, e);
    }
    return false;
}
Also used : HttpClientResponse(io.vertx.core.http.HttpClientResponse) 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) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

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