Search in sources :

Example 16 with IpPort

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

the class ServiceRegistryClientImpl method getSchema.

@Override
public String getSchema(String microserviceId, String schemaId) {
    Holder<GetSchemaResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.get(ipPort, Const.MS_API_PATH + Const.MICROSERVICE_PATH + "/" + microserviceId + Const.SCHEMA_PATH + "/" + schemaId, new RequestParam(), syncHandler(countDownLatch, GetSchemaResponse.class, holder));
    try {
        countDownLatch.await();
    } catch (Exception e) {
        LOGGER.error("query schema exist {}/{} failed", microserviceId, schemaId, e);
    }
    if (holder.value != null) {
        return holder.value.getSchema();
    }
    return null;
}
Also used : Holder(javax.xml.ws.Holder) IpPort(io.servicecomb.foundation.common.net.IpPort) GetSchemaResponse(io.servicecomb.serviceregistry.api.response.GetSchemaResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 17 with IpPort

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

the class ServiceRegistryClientImpl method findServiceInstance.

@Override
public List<MicroserviceInstance> findServiceInstance(String selfMicroserviceId, String appId, String serviceName, String versionRule) {
    Holder<FindInstancesResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    StringBuilder url = new StringBuilder(Const.MS_API_PATH);
    url.append(Const.INSTANCES_PATH);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.get(ipPort, url.toString(), new RequestParam().addQueryParam("appId", appId).addQueryParam("serviceName", serviceName).addQueryParam("version", versionRule).addHeader("X-ConsumerId", selfMicroserviceId), syncHandler(countDownLatch, FindInstancesResponse.class, holder));
    try {
        countDownLatch.await();
        if (holder.value == null) {
            // error
            return null;
        }
        List<MicroserviceInstance> list = holder.value.getInstances();
        if (list == null) {
            return new ArrayList<>();
        }
        return list;
    } catch (Exception e) {
        LOGGER.error("find microservice instance {}/{}/{} failed", appId, serviceName, versionRule, e);
    }
    return null;
}
Also used : Holder(javax.xml.ws.Holder) ArrayList(java.util.ArrayList) MicroserviceInstance(io.servicecomb.serviceregistry.api.registry.MicroserviceInstance) IpPort(io.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) FindInstancesResponse(io.servicecomb.serviceregistry.api.response.FindInstancesResponse) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 18 with IpPort

use of io.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.INSTANCE.get();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.get(ipPort, Const.MS_API_PATH + Const.MICROSERVICE_PATH, 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 null;
}
Also used : GetAllServicesResponse(io.servicecomb.serviceregistry.api.response.GetAllServicesResponse) Holder(javax.xml.ws.Holder) IpPort(io.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 19 with IpPort

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

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.INSTANCE.get();
    StringBuilder url = new StringBuilder(Const.MS_API_PATH);
    url.append(Const.MICROSERVICE_PATH).append("/").append(microserviceId).append(Const.INSTANCES_PATH).append("/").append(microserviceInstanceId).append(Const.PROPERTIES_PATH);
    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, url.toString(), 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 : Holder(javax.xml.ws.Holder) HttpClientResponse(io.vertx.core.http.HttpClientResponse) IpPort(io.servicecomb.foundation.common.net.IpPort) UpdatePropertiesRequest(io.servicecomb.serviceregistry.api.request.UpdatePropertiesRequest) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 20 with IpPort

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

the class RestUtils method httpDo.

public static void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
    HttpClientWithContext vertxHttpClient = HttpClientPool.INSTANCE.getClient();
    vertxHttpClient.runOnContext(httpClient -> {
        IpPort ipPort = requestContext.getIpPort();
        HttpMethod httpMethod = requestContext.getMethod();
        RequestParam requestParam = requestContext.getParams();
        if (ipPort == null) {
            LOGGER.error("request address is null");
            responseHandler.handle(new RestResponse(requestContext, null));
            return;
        }
        StringBuilder url = new StringBuilder(requestContext.getUri());
        String queryParams = requestParam.getQueryParams();
        if (!queryParams.isEmpty()) {
            url.append(url.lastIndexOf("?") > 0 ? "&" : "?").append(queryParams);
        }
        HttpClientRequest httpClientRequest = httpClient.request(httpMethod, ipPort.getPort(), ipPort.getHostOrIp(), url.toString(), response -> {
            responseHandler.handle(new RestResponse(requestContext, response));
        });
        httpClientRequest.setTimeout(ServiceRegistryConfig.INSTANCE.getRequestTimeout()).exceptionHandler(e -> {
            LOGGER.error("{} {} fail, endpoint is {}:{}, message: {}", httpMethod, url.toString(), ipPort.getHostOrIp(), ipPort.getPort(), e.getMessage());
            responseHandler.handle(new RestResponse(requestContext, null));
        });
        addDefaultHeaders(httpClientRequest);
        if (requestParam.getHeaders() != null && requestParam.getHeaders().size() > 0) {
            for (Map.Entry<String, String> header : requestParam.getHeaders().entrySet()) {
                httpClientRequest.putHeader(header.getKey(), header.getValue());
            }
        }
        if (requestParam.getCookies() != null && requestParam.getCookies().size() > 0) {
            StringBuilder stringBuilder = new StringBuilder();
            for (Map.Entry<String, String> cookie : requestParam.getCookies().entrySet()) {
                stringBuilder.append(cookie.getKey()).append("=").append(cookie.getValue()).append("; ");
            }
            httpClientRequest.putHeader("Cookie", stringBuilder.toString());
        }
        if (httpMethod != HttpMethod.GET && requestParam.getBody() != null && requestParam.getBody().length > 0) {
            httpClientRequest.end(Buffer.buffer(requestParam.getBody()));
        } else {
            httpClientRequest.end();
        }
    });
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) IpPort(io.servicecomb.foundation.common.net.IpPort) MultiMap(io.vertx.core.MultiMap) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.vertx.core.http.HttpMethod)

Aggregations

IpPort (io.servicecomb.foundation.common.net.IpPort)35 ClientException (io.servicecomb.serviceregistry.client.ClientException)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 Holder (javax.xml.ws.Holder)14 Test (org.junit.Test)10 HttpClientRequest (io.vertx.core.http.HttpClientRequest)7 AsyncResponse (io.servicecomb.core.AsyncResponse)6 Invocation (io.servicecomb.core.Invocation)6 HttpClientResponse (io.vertx.core.http.HttpClientResponse)6 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)5 HttpClient (io.vertx.core.http.HttpClient)4 OperationMeta (io.servicecomb.core.definition.OperationMeta)3 HttpClientWithContext (io.servicecomb.foundation.vertx.client.http.HttpClientWithContext)3 HttpServer (io.vertx.core.http.HttpServer)3 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)2 UpdatePropertiesRequest (io.servicecomb.serviceregistry.api.request.UpdatePropertiesRequest)2 GetExistenceResponse (io.servicecomb.serviceregistry.api.response.GetExistenceResponse)2 CacheEndpoint (io.servicecomb.serviceregistry.cache.CacheEndpoint)2 ArrayList (java.util.ArrayList)2 Expectations (mockit.Expectations)2