Search in sources :

Example 26 with IpPort

use of io.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 = Const.MS_API_PATH + Const.MICROSERVICE_PATH + "/" + selfMicroserviceId + Const.WATCHER_PATH;
                IpPort ipPort = IpPortManager.INSTANCE.get();
                if (ipPort == null) {
                    LOGGER.error("request address is null, watch microservice {}", selfMicroserviceId);
                    watchErrorHandler(new Exception("request address is null"), selfMicroserviceId, callback);
                    return;
                }
                WebsocketUtils.open(ipPort, url, o -> {
                    onOpen.success(o);
                    LOGGER.info("watching microservice {} successfully, " + "the chosen service center address is {}:{}", selfMicroserviceId, ipPort.getHostOrIp(), ipPort.getPort());
                }, c -> {
                    LOGGER.warn("watching microservice {} connection is closed accidentally", selfMicroserviceId);
                    watchErrorHandler(new ClientException("connection is closed accidentally"), selfMicroserviceId, callback);
                    onClose.success(null);
                }, bodyBuffer -> {
                    MicroserviceInstanceChangedEvent response = null;
                    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 -> {
                    LOGGER.error("watcher read microservice {} message from service center failed," + " {}", selfMicroserviceId, e.getMessage());
                }, f -> {
                    if (!watchServices.containsKey(selfMicroserviceId)) {
                        return;
                    }
                    LOGGER.error("watcher connect to service center server failed, microservice {}, {}", selfMicroserviceId, f.getMessage());
                    watchErrorHandler(f, selfMicroserviceId, callback);
                });
            }
        }
    }
}
Also used : MicroserviceInstanceChangedEvent(io.servicecomb.serviceregistry.api.response.MicroserviceInstanceChangedEvent) IpPort(io.servicecomb.foundation.common.net.IpPort) ClientException(io.servicecomb.serviceregistry.client.ClientException) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 27 with IpPort

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

the class ServiceRegistryClientImpl method getMicroserviceId.

@Override
public String getMicroserviceId(String appId, String microserviceName, String versionRule) {
    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", "microservice").addQueryParam("appId", appId).addQueryParam("serviceName", microserviceName).addQueryParam("version", versionRule), syncHandler(countDownLatch, GetExistenceResponse.class, holder));
    try {
        countDownLatch.await();
        if (holder.value != null) {
            return holder.value.getServiceId();
        }
    } catch (Exception e) {
        LOGGER.error("query microservice id {}/{}/{} fail", appId, microserviceName, versionRule, e);
    }
    return 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 28 with IpPort

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

the class TestClienthttp method testServiceRegistryClientImpl.

@SuppressWarnings("unchecked")
@Test
public void testServiceRegistryClientImpl(@Mocked IpPortManager manager) {
    IpPort ipPort = new IpPort("127.0.0.1", 8853);
    new Expectations() {

        {
            manager.get();
            result = ipPort;
            manager.next();
            result = null;
        }
    };
    Microservice oInstance = RegistryUtils.getMicroservice();
    RegistryThread oThread = new RegistryThread();
    oThread.start();
    ServiceRegistryClientImpl oClient = (ServiceRegistryClientImpl) RegistryClientFactory.getRegistryClient();
    oClient.registerMicroservice(oInstance);
    oClient.registerMicroserviceInstance(RegistryUtils.getMicroserviceInstance());
    try {
        oClient.init();
    } catch (Exception e) {
        Assert.assertEquals(null, e.getMessage());
    }
    Assert.assertEquals(null, oClient.getMicroservice(RegistryUtils.getMicroservice().getServiceId()));
    Assert.assertEquals(null, oClient.getMicroserviceInstance("testConsumerID", "testproviderID"));
    Assert.assertEquals(null, oClient.findServiceInstance(RegistryUtils.getMicroservice().getServiceId(), RegistryUtils.getMicroservice().getAppId(), RegistryUtils.getMicroservice().getServiceName(), RegistryUtils.getMicroservice().getVersion()));
    Assert.assertEquals(null, oClient.getMicroserviceId(RegistryUtils.getMicroservice().getAppId(), RegistryUtils.getMicroservice().getServiceName(), RegistryUtils.getMicroservice().getVersion()));
    Assert.assertEquals(null, oClient.heartbeat(RegistryUtils.getMicroservice().getServiceId(), RegistryUtils.getMicroserviceInstance().getInstanceId()));
    oClient.watch("", Mockito.mock(AsyncResultCallback.class));
    Assert.assertEquals(false, oClient.unregisterMicroserviceInstance(RegistryUtils.getMicroservice().getServiceId(), RegistryUtils.getMicroserviceInstance().getInstanceId()));
}
Also used : Expectations(mockit.Expectations) Microservice(io.servicecomb.serviceregistry.api.registry.Microservice) RegistryThread(io.servicecomb.serviceregistry.RegistryThread) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResultCallback(io.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 29 with IpPort

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

the class IpPortManager method getDefaultIpPort.

public IpPort getDefaultIpPort() {
    List<IpPort> addresses = getDefaultIpPortList();
    if (addresses == null || addresses.size() == 0) {
        LOGGER.warn("not exist any service center address");
        return null;
    }
    int id = indexForDefault.get();
    return addresses.get(id);
}
Also used : IpPort(io.servicecomb.foundation.common.net.IpPort) CacheEndpoint(io.servicecomb.serviceregistry.cache.CacheEndpoint)

Example 30 with IpPort

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

the class ServiceRegistryConfig method getIpPort.

public ArrayList<IpPort> getIpPort() {
    DynamicStringProperty property = DynamicPropertyFactory.getInstance().getStringProperty("cse.service.registry.address", "https://127.0.0.1:30100");
    List<String> uriLsit = Arrays.asList(property.get().split(","));
    ArrayList<IpPort> ipPortList = new ArrayList<IpPort>();
    for (int i = 0; i < uriLsit.size(); i++) {
        try {
            URI uri = new URI(uriLsit.get(i));
            StringBuilder sb = new StringBuilder(uri.getHost());
            sb.append(':').append(uri.getPort() < 0 ? PROTOCOL_HTTP_PORT : uri.getPort());
            this.ssl = uri.getScheme().startsWith("https");
            ipPortList.add(NetUtils.parseIpPort(sb.toString()));
        } catch (Exception e) {
            LOGGER.error("cse.service.registry.address invalid : {}", uriLsit.get(i), e);
        }
    }
    return ipPortList;
}
Also used : DynamicStringProperty(com.netflix.config.DynamicStringProperty) ArrayList(java.util.ArrayList) IpPort(io.servicecomb.foundation.common.net.IpPort) URI(java.net.URI)

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