Search in sources :

Example 31 with IpPort

use of org.apache.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.getAvailableAddress();
    CountDownLatch countDownLatch = new CountDownLatch(1);
    restClientUtil.delete(ipPort, String.format(Const.REGISTRY_API.MICROSERVICE_INSTANCE_OPERATION_ONE, microserviceId, microserviceInstanceId), 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 : HttpClientResponse(io.vertx.core.http.HttpClientResponse) 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 32 with IpPort

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

the class RegistrationManager method getPublishAddress.

/**
 * In the case that listening address configured as 0.0.0.0, the publish address will be determined
 * by the query result for the net interfaces.
 *
 * @return the publish address, or {@code null} if the param {@code address} is null.
 */
public static String getPublishAddress(String schema, String address) {
    if (address == null) {
        return address;
    }
    try {
        URI originalURI = new URI(schema + "://" + address);
        IpPort ipPort = NetUtils.parseIpPort(originalURI);
        if (ipPort == null) {
            LOGGER.warn("address {} not valid.", address);
            return null;
        }
        IpPort publishIpPort = genPublishIpPort(schema, ipPort);
        URIBuilder builder = new URIBuilder(originalURI);
        return builder.setHost(publishIpPort.getHostOrIp()).setPort(publishIpPort.getPort()).build().toString();
    } catch (URISyntaxException e) {
        LOGGER.warn("address {} not valid.", address);
        return null;
    }
}
Also used : IpPort(org.apache.servicecomb.foundation.common.net.IpPort) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 33 with IpPort

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

the class TestIpPortManager method testGetAvailableAddress.

@Test
public void testGetAvailableAddress(@Injectable ServiceRegistryConfig config, @Injectable InstanceCacheManager cacheManager, @Injectable InstanceCache cache, @Injectable ClassificationAddress classificationAddress) {
    ArrayList<IpPort> ipPortList = new ArrayList<>();
    ipPortList.add(new IpPort("127.0.0.1", 9980));
    ipPortList.add(new IpPort("127.0.0.1", 9981));
    new Expectations() {

        {
            config.getIpPort();
            result = ipPortList;
            config.getTransport();
            result = "rest";
            config.isRegistryAutoDiscovery();
            result = true;
        }
    };
    IpPortManager manager = new IpPortManager(config);
    manager.instanceCacheManager = cacheManager;
    IpPort address1 = manager.getAvailableAddress();
    // test initial
    Assert.assertEquals("127.0.0.1", address1.getHostOrIp());
    Assert.assertTrue(address1.getPort() == 9980 || address1.getPort() == 9981);
    // test getAvailableAddress()
    IpPort address2 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address2.getHostOrIp());
    if (address1.getPort() == 9980) {
        Assert.assertEquals(9981, address2.getPort());
    } else {
        Assert.assertEquals(9980, address2.getPort());
    }
    // test getAvailableAddress() when reaching the end
    IpPort address3 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address3.getHostOrIp());
    Assert.assertEquals(address1.getPort(), address3.getPort());
    // mock endpoint list
    Map<String, List<CacheEndpoint>> addresses = new HashMap<>();
    List<CacheEndpoint> instances = new ArrayList<>();
    instances.add(new CacheEndpoint("http://127.0.0.1:9982", null));
    addresses.put("rest", instances);
    ClassificationAddress classificationAddres = new ClassificationAddress(config, cacheManager);
    manager.classificationAddress = classificationAddres;
    new Expectations() {

        {
            cacheManager.getOrCreate("default", "SERVICECENTER", "latest");
            result = cache;
            cache.getOrCreateTransportMap();
            result = addresses;
        }
    };
    // test getAvailableAddress() when auto discovery is disabled
    IpPort address4 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address4.getHostOrIp());
    if (address1.getPort() == 9980) {
        address4 = manager.getAvailableAddress();
    }
    Assert.assertEquals(9980, address4.getPort());
    IpPort address5 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address5.getHostOrIp());
    Assert.assertEquals(9981, address5.getPort());
    // mock RegistrationManager.INSTANCE
    String instanceId = "e8a04b54cf2711e7b701286ed488fc20";
    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
    microserviceInstance.setInstanceId(instanceId);
    Map<String, String> properties = new HashMap<>();
    microserviceInstance.setProperties(properties);
    new Expectations(RegistrationManager.INSTANCE) {

        {
            RegistrationManager.INSTANCE.getMicroserviceInstance();
            result = microserviceInstance;
        }
    };
    // test getAvailable address when auto discovery is enabled
    manager.initAutoDiscovery();
    manager.setAutoDiscoveryInited(true);
    IpPort address6 = manager.getAvailableAddress();
    Assert.assertEquals("127.0.0.1", address6.getHostOrIp());
    Assert.assertEquals(9982, address6.getPort());
}
Also used : Expectations(mockit.Expectations) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClassificationAddress(org.apache.servicecomb.serviceregistry.refresh.ClassificationAddress) MicroserviceInstance(org.apache.servicecomb.registry.api.registry.MicroserviceInstance) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) CacheEndpoint(org.apache.servicecomb.registry.cache.CacheEndpoint) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 34 with IpPort

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

the class TestServiceRegistryClientImpl method undateMicroserviceInstanceStatus.

@SuppressWarnings("deprecation")
@Test
public void undateMicroserviceInstanceStatus() {
    HttpClientResponse httpClientResponse = new MockUp<HttpClientResponse>() {

        @Mock
        int statusCode() {
            return 200;
        }
    }.getMockInstance();
    new MockUp<RestClientUtil>() {

        @Mock
        void put(IpPort ipPort, String uri, RequestParam requestParam, Handler<RestResponse> responseHandler) {
            Holder<HttpClientResponse> holder = Deencapsulation.getField(responseHandler, "arg$4");
            holder.value = httpClientResponse;
        }
    };
    boolean result = oClient.undateMicroserviceInstanceStatus("svcId", "instanceId", "UP");
    Assert.assertTrue(result);
}
Also used : HttpClientResponse(io.vertx.core.http.HttpClientResponse) Handler(io.vertx.core.Handler) MockUp(mockit.MockUp) IpPort(org.apache.servicecomb.foundation.common.net.IpPort) Mock(mockit.Mock) Test(org.junit.Test)

Example 35 with IpPort

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

the class AddressManagerTest method addressManagerTest.

@Test
public void addressManagerTest() {
    IpPort ipPort = new IpPort("127.0.0.1", 30103);
    addresses.add(ipPort.toString());
    addressManager1 = new AddressManager(addresses, new EventBus());
    addressManager2 = new AddressManager(addresses, new EventBus());
    Assert.assertNotNull(addressManager1);
    Assert.assertNotNull(addressManager2);
    List<String> addresses = Deencapsulation.getField(addressManager1, "addresses");
    Assert.assertEquals(1, addresses.size());
    Assert.assertEquals("127.0.0.1:30103", addresses.get(0));
    Assert.assertEquals("127.0.0.1:30103", addressManager1.address());
    ipPort = addressManager2.getAvailableIpPort();
    Assert.assertEquals("127.0.0.1:30103", ipPort.toString());
    Assert.assertEquals("127.0.0.1", ipPort.getHostOrIp());
    Assert.assertEquals(30103, ipPort.getPort());
}
Also used : IpPort(org.apache.servicecomb.foundation.common.net.IpPort) EventBus(com.google.common.eventbus.EventBus) Test(org.junit.jupiter.api.Test)

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