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;
}
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;
}
}
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());
}
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);
}
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());
}
Aggregations