Search in sources :

Example 11 with IpPort

use of io.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.INSTANCE.get();
    StringBuilder url = new StringBuilder(Const.MS_API_PATH);
    url.append(Const.MICROSERVICE_PATH).append("/").append(microserviceId).append(Const.PROPERTIES_PATH);
    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);
        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 {} failed", microserviceId, 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 12 with IpPort

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

the class ServiceRegistryClientImpl method isSchemaExist.

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

Example 13 with IpPort

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

the class IpPortManager method nextDefaultIpPort.

public IpPort nextDefaultIpPort() {
    List<IpPort> addresses = getDefaultIpPortList();
    if (addresses == null || addresses.size() == 0) {
        LOGGER.warn("not exist any service center address");
        return null;
    }
    synchronized (lockObj) {
        //轮询一遍结束,暂不考虑并发场景
        int id = indexForDefault.get();
        if (id == addresses.size() - 1) {
            indexForDefault.set(0);
            LOGGER.warn("service center has no available instance");
            return null;
        }
        indexForDefault.getAndIncrement();
        LOGGER.info("service center address {}:{} is unreachable, retry another address {}:{}", addresses.get(id).getHostOrIp(), addresses.get(id).getPort(), addresses.get(indexForDefault.get()).getHostOrIp(), addresses.get(indexForDefault.get()).getPort());
        return addresses.get(indexForDefault.get());
    }
}
Also used : IpPort(io.servicecomb.foundation.common.net.IpPort) CacheEndpoint(io.servicecomb.serviceregistry.cache.CacheEndpoint)

Example 14 with IpPort

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

the class ServiceRegistryClientImpl method retry.

private static boolean retry(RequestContext requestContext, Handler<RestResponse> responseHandler) {
    IpPort ipPort = IpPortManager.INSTANCE.next();
    if (ipPort == null) {
        return false;
    }
    requestContext.setIpPort(IpPortManager.INSTANCE.get());
    RestUtils.httpDo(requestContext, responseHandler);
    return true;
}
Also used : IpPort(io.servicecomb.foundation.common.net.IpPort)

Example 15 with IpPort

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

the class ServiceRegistryClientImpl method registerSchema.

@Override
public boolean registerSchema(String microserviceId, String schemaId, String schemaContent) {
    Holder<HttpClientResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    try {
        CreateSchemaRequest request = new CreateSchemaRequest();
        request.setSchema(schemaContent);
        byte[] body = JsonUtils.writeValueAsBytes(request);
        CountDownLatch countDownLatch = new CountDownLatch(1);
        RestUtils.put(ipPort, Const.MS_API_PATH + Const.MICROSERVICE_PATH + "/" + microserviceId + Const.SCHEMA_PATH + "/" + schemaId, new RequestParam().setBody(body), syncHandler(countDownLatch, HttpClientResponse.class, holder));
        countDownLatch.await();
        boolean result = false;
        if (holder.value != null) {
            result = holder.value.statusCode() == Status.OK.getStatusCode();
        }
        LOGGER.info("register schema {}/{}, result {}", microserviceId, schemaId, result);
        return result;
    } catch (Exception e) {
        LOGGER.error("query schema exist {}/{} fail", microserviceId, schemaId, e);
    }
    return false;
}
Also used : CreateSchemaRequest(io.servicecomb.serviceregistry.api.request.CreateSchemaRequest) Holder(javax.xml.ws.Holder) HttpClientResponse(io.vertx.core.http.HttpClientResponse) IpPort(io.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

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