Search in sources :

Example 6 with IpPort

use of io.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.assertEquals(null, oContext.getParams());
    RestResponse oResponse = new RestResponse(null, null);
    oResponse.setRequestContext(oContext);
    Assert.assertEquals(oContext, oResponse.getRequestContext());
    Assert.assertEquals(null, oResponse.getResponse());
}
Also used : IpPort(io.servicecomb.foundation.common.net.IpPort) Test(org.junit.Test)

Example 7 with IpPort

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

the class TestClienthttp method testIpPortManager.

@Test
public void testIpPortManager() throws Exception {
    IpPortManager oManager = new IpPortManager();
    ArrayList<IpPort> oIPPort = oManager.getDefaultIpPortList();
    oManager.next();
    Assert.assertEquals(oIPPort.get(0).getHostOrIp(), oManager.get().getHostOrIp());
    try {
        //This will return Null as the address cache is not able to get the address of the microservice which is registered above. 
        Assert.assertNull(oManager.next());
    } catch (Exception e) {
        // TODO: Currently the address cache is failing because of absence of Thread
        // TODO: Need to find out a way to Assert it properly
        Assert.assertEquals("/ by zero", e.getMessage());
    }
}
Also used : IpPort(io.servicecomb.foundation.common.net.IpPort) IpPortManager(io.servicecomb.serviceregistry.client.IpPortManager) Test(org.junit.Test)

Example 8 with IpPort

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

the class RegistryUtils method getPublishAddress.

public static String getPublishAddress(String schema, String address) {
    if (address == null) {
        return address;
    }
    try {
        String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get();
        publicAddressSetting = publicAddressSetting.trim();
        URI originalURI = new URI(schema + "://" + address);
        IpPort ipPort = NetUtils.parseIpPort(originalURI.getAuthority());
        if (ipPort == null) {
            LOGGER.warn("address {} not valid.", address);
            return null;
        }
        InetSocketAddress socketAddress = ipPort.getSocketAddress();
        String host = socketAddress.getAddress().getHostAddress();
        if (publicAddressSetting.isEmpty()) {
            if (socketAddress.getAddress().isAnyLocalAddress()) {
                host = NetUtils.getHostAddress();
                LOGGER.warn("address {}, auto select a host address to publish {}:{}, maybe not the correct one", address, host, socketAddress.getPort());
                URI newURI = new URI(originalURI.getScheme(), originalURI.getUserInfo(), host, originalURI.getPort(), originalURI.getPath(), originalURI.getQuery(), originalURI.getFragment());
                return newURI.toString();
            } else {
                return originalURI.toString();
            }
        }
        if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) {
            publicAddressSetting = NetUtils.getHostAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1));
        }
        String publishPortKey = PUBLISH_PORT.replace("{transport_name}", originalURI.getScheme());
        int publishPortSetting = DynamicPropertyFactory.getInstance().getIntProperty(publishPortKey, 0).get();
        int publishPort = publishPortSetting == 0 ? originalURI.getPort() : publishPortSetting;
        URI newURI = new URI(originalURI.getScheme(), originalURI.getUserInfo(), publicAddressSetting, publishPort, originalURI.getPath(), originalURI.getQuery(), originalURI.getFragment());
        return newURI.toString();
    } catch (URISyntaxException e) {
        LOGGER.warn("address {} not valid.", address);
        return null;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IpPort(io.servicecomb.foundation.common.net.IpPort) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 9 with IpPort

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

the class ServiceRegistryClientImpl method unregisterMicroserviceInstance.

@Override
public boolean unregisterMicroserviceInstance(String microserviceId, String microserviceInstanceId) {
    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);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.delete(ipPort, url.toString(), new RequestParam(), syncHandler(countDownLatch, HttpClientResponse.class, holder));
    try {
        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("unregister 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) CountDownLatch(java.util.concurrent.CountDownLatch) ClientException(io.servicecomb.serviceregistry.client.ClientException)

Example 10 with IpPort

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

the class ServiceRegistryClientImpl method getMicroserviceInstance.

@Override
public List<MicroserviceInstance> getMicroserviceInstance(String consumerId, String providerId) {
    Holder<GetInstancesResponse> holder = new Holder<>();
    IpPort ipPort = IpPortManager.INSTANCE.get();
    StringBuilder url = new StringBuilder(Const.MS_API_PATH);
    url.append(Const.MICROSERVICE_PATH).append("/").append(providerId).append(Const.INSTANCES_PATH);
    CountDownLatch countDownLatch = new CountDownLatch(1);
    RestUtils.get(ipPort, url.toString(), new RequestParam().addHeader("X-ConsumerId", consumerId), syncHandler(countDownLatch, GetInstancesResponse.class, holder));
    try {
        countDownLatch.await();
        if (holder.value != null) {
            return holder.value.getInstances();
        }
    } catch (Exception e) {
        LOGGER.error("query microservice instances {} failed", providerId, e);
    }
    return null;
}
Also used : Holder(javax.xml.ws.Holder) IpPort(io.servicecomb.foundation.common.net.IpPort) CountDownLatch(java.util.concurrent.CountDownLatch) GetInstancesResponse(io.servicecomb.serviceregistry.api.response.GetInstancesResponse) 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