Search in sources :

Example 56 with IpPort

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

the class TestClientHttp method testRequestContext.

@Test
public void testRequestContext() {
    RequestContext oContext = new RequestContext();
    oContext.setUri("//test");
    oContext.setMethod(io.vertx.core.http.HttpMethod.POST);
    oContext.setIpPort(new IpPort("145.0.0.1", 8080));
    oContext.setParams(null);
    Assert.assertEquals("//test", oContext.getUri());
    Assert.assertEquals(io.vertx.core.http.HttpMethod.POST, oContext.getMethod());
    Assert.assertEquals(8080, oContext.getIpPort().getPort());
    Assert.assertNull(oContext.getParams());
    RestResponse oResponse = new RestResponse(null, null);
    oResponse.setRequestContext(oContext);
    Assert.assertEquals(oContext, oResponse.getRequestContext());
    Assert.assertNull(oResponse.getResponse());
}
Also used : IpPort(org.apache.servicecomb.foundation.common.net.IpPort) Test(org.junit.Test)

Example 57 with IpPort

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

the class TestClientHttp method testIpPortManager.

@Test
public void testIpPortManager() {
    IpPortManager oManager = new IpPortManager(ServiceRegistryConfig.INSTANCE);
    IpPort oIPPort = oManager.getAvailableAddress();
    Assert.assertEquals(oIPPort.getHostOrIp(), oManager.getAvailableAddress().getHostOrIp());
}
Also used : IpPort(org.apache.servicecomb.foundation.common.net.IpPort) IpPortManager(org.apache.servicecomb.serviceregistry.client.IpPortManager) Test(org.junit.Test)

Example 58 with IpPort

use of org.apache.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) {
    Configuration configuration = ConfigUtil.createLocalConfig();
    configuration.setProperty(BootStrapProperties.CONFIG_SERVICE_APPLICATION, "app");
    configuration.setProperty(BootStrapProperties.CONFIG_SERVICE_NAME, "ms");
    IpPort ipPort = new IpPort("127.0.0.1", 8853);
    new Expectations() {

        {
            manager.getAvailableAddress();
            result = ipPort;
        }
    };
    new MockUp<RegistryUtils>() {

        @Mock
        Microservice getMicroservice() {
            return microservice;
        }
    };
    new MockUp<CountDownLatch>() {

        @Mock
        public void await() throws InterruptedException {
        }
    };
    new MockUp<RestClientUtil>() {

        @Mock
        void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
        }
    };
    new MockUp<WebsocketClientUtil>() {

        @Mock
        void open(IpPort ipPort, String url, Handler<Void> onOpen, Handler<Void> onClose, Handler<Buffer> onMessage, Handler<Throwable> onException, Handler<Throwable> onConnectFailed) {
        }
    };
    // mock up this two client pool, since this UT case doesn't require the client pool actually boot up.
    new MockUp<HttpClientPool>() {

        @Mock
        void create() {
        }
    };
    new MockUp<WebsocketClientPool>() {

        @Mock
        void create() {
        }
    };
    MicroserviceFactory microserviceFactory = new MicroserviceFactory();
    Microservice microservice = microserviceFactory.create(configuration);
    ServiceRegistryClientImpl oClient = new ServiceRegistryClientImpl(ServiceRegistryConfig.INSTANCE);
    oClient.init();
    oClient.registerMicroservice(microservice);
    oClient.registerMicroserviceInstance(microservice.getInstance());
    Assert.assertNull(oClient.getMicroservice(microservice.getServiceId()));
    Assert.assertNull(oClient.getMicroserviceInstance("testConsumerID", "testproviderID"));
    Assert.assertNull(oClient.findServiceInstance(microservice.getServiceId(), microservice.getAppId(), microservice.getServiceName(), microservice.getVersion()));
    Assert.assertNull(oClient.findServiceInstances(microservice.getServiceId(), microservice.getAppId(), microservice.getServiceName(), microservice.getVersion(), "0"));
    Assert.assertNull(oClient.getMicroserviceId(microservice.getAppId(), microservice.getServiceName(), microservice.getVersion(), microservice.getEnvironment()));
    Assert.assertNull(oClient.heartbeat(microservice.getServiceId(), microservice.getInstance().getInstanceId()));
    oClient.watch("", Mockito.mock(AsyncResultCallback.class));
    Assert.assertFalse(oClient.unregisterMicroserviceInstance(microservice.getServiceId(), microservice.getInstance().getInstanceId()));
}
Also used : Expectations(mockit.Expectations) Microservice(org.apache.servicecomb.registry.api.registry.Microservice) Configuration(org.apache.commons.configuration.Configuration) Handler(io.vertx.core.Handler) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) MockUp(mockit.MockUp) MicroserviceFactory(org.apache.servicecomb.registry.api.registry.MicroserviceFactory) AsyncResultCallback(org.apache.servicecomb.foundation.vertx.AsyncResultCallback) Test(org.junit.Test)

Example 59 with IpPort

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

the class ServiceRegistryClientImpl method registerMicroservice.

@Override
public String registerMicroservice(Microservice microservice) {
    Holder<CreateServiceResponse> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    try {
        CreateServiceRequest request = new CreateServiceRequest();
        request.setService(microservice);
        byte[] body = JsonUtils.writeValueAsBytes(request);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("register microservice: {}", new String(body, Charset.defaultCharset()));
        }
        CountDownLatch countDownLatch = new CountDownLatch(1);
        restClientUtil.post(ipPort, Const.REGISTRY_API.MICROSERVICE_OPERATION_ALL, new RequestParam().setBody(body), syncHandler(countDownLatch, CreateServiceResponse.class, holder));
        countDownLatch.await();
        if (holder.value != null) {
            return holder.value.getServiceId();
        }
    } catch (Exception e) {
        LOGGER.error("register microservice {}/{}/{} failed", microservice.getAppId(), microservice.getServiceName(), microservice.getVersion(), e);
    }
    return null;
}
Also used : CreateServiceRequest(org.apache.servicecomb.serviceregistry.api.request.CreateServiceRequest) CreateServiceResponse(org.apache.servicecomb.serviceregistry.api.response.CreateServiceResponse) 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 60 with IpPort

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

the class ServiceRegistryClientImpl method getServiceCenterInfo.

@Override
public ServiceCenterInfo getServiceCenterInfo() {
    Holder<ServiceCenterInfo> holder = new Holder<>();
    IpPort ipPort = ipPortManager.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.get(ipPort, Const.REGISTRY_API.SERVICECENTER_VERSION, new RequestParam(), syncHandler(countDownLatch, ServiceCenterInfo.class, holder));
    try {
        countDownLatch.await();
        if (holder.value != null) {
            return holder.value;
        }
    } catch (Exception e) {
        LOGGER.error("query servicecenter version info failed.", e);
    }
    return null;
}
Also used : ServiceCenterInfo(org.apache.servicecomb.serviceregistry.api.registry.ServiceCenterInfo) 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)

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