use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.
the class TestClientHttp method testIpPortManager.
@Test
public void testIpPortManager(@Mocked InstanceCacheManager instanceCacheManager) throws Exception {
IpPortManager oManager = new IpPortManager(ServiceRegistryConfig.INSTANCE, instanceCacheManager);
IpPort oIPPort = oManager.getNextAvailableAddress(new IpPort("", 33));
Assert.assertEquals(oIPPort.getHostOrIp(), oManager.getAvailableAddress().getHostOrIp());
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.
the class TestRemoteServiceRegistry method testLifeCycle.
@Test
public void testLifeCycle(@Injectable ServiceRegistryConfig config, @Injectable MicroserviceDefinition definition, @Injectable ServiceRegistry registry) {
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() {
{
definition.getConfiguration();
result = ConfigUtil.createLocalConfig();
config.getIpPort();
result = ipPortList;
config.getTransport();
result = "rest";
config.isRegistryAutoDiscovery();
result = true;
config.getHeartbeatInterval();
result = 30;
config.getInstancePullInterval();
result = 30;
config.isWatch();
result = false;
}
};
ServiceRegistry oldRegistry = RegistryUtils.getServiceRegistry();
RegistryUtils.setServiceRegistry(registry);
EventBus bus = new EventBus();
RemoteServiceRegistry remote = new TestingRemoteServiceRegistry(bus, config, definition);
remote.init();
remote.run();
// includes complete tasks
Assert.assertTrue(2 <= remote.getTaskPool().getTaskCount());
bus.post(new ShutdownEvent());
remote.getTaskPool().schedule(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
}, 0, TimeUnit.SECONDS);
Assert.assertTrue(remote.getTaskPool().isShutdown());
RegistryUtils.setServiceRegistry(oldRegistry);
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class DefaultMonitorDataPublisher method doSend.
private void doSend(String endpoint, String jsonData, String url, IpPort host, int times) {
clientMgr.findThreadBindClientPool().runOnContext(client -> {
client.request(HttpMethod.POST, host.getPort(), host.getHostOrIp(), url).compose(request -> {
request.headers().add("environment", RegistryUtils.getMicroservice().getEnvironment());
request.setTimeout(MonitorConstant.getInterval() / MonitorConstant.MAX_RETRY_TIMES);
try {
SignRequest signReq = SignUtil.createSignRequest(request.getMethod().toString(), endpoint + url, new HashMap<>(), IOUtils.toInputStream(jsonData, "UTF-8"));
SignUtil.getAuthHeaderProviders().forEach(authHeaderProvider -> {
request.headers().addAll(authHeaderProvider.getSignAuthHeaders(signReq));
});
} catch (Exception e) {
LOGGER.error("sign request error!", e);
}
return request.send(jsonData).compose(rsp -> {
if (rsp.statusCode() != HttpResponseStatus.OK.code()) {
addressManager.recordSuccessState(endpoint);
if (times < MonitorConstant.MAX_RETRY_TIMES && rsp.statusCode() == HttpResponseStatus.BAD_GATEWAY.code()) {
doSend(endpoint, jsonData, url, host, times + 1);
return Future.succeededFuture();
}
return rsp.body().compose(buffer -> {
LOGGER.warn("Send data to url {} failed and status line is {}", url, rsp.statusCode());
LOGGER.warn("message: {}", buffer);
return Future.succeededFuture();
});
} else {
addressManager.recordSuccessState(endpoint);
EventManager.post(new MonitorSuccEvent());
}
return Future.succeededFuture();
}).onFailure(failure -> {
EventManager.post(new MonitorFailEvent("send monitor data fail."));
addressManager.recordFailState(endpoint);
LOGGER.warn("Send monitor data to {} failed , {}", endpoint, failure);
});
});
});
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class DefaultMonitorDataPublisher method publish.
@Override
public void publish(MonitorDaraProvider provider) {
Object data = provider.getData();
if (data == null) {
return;
}
String endpoint = addressManager.address();
if (endpoint == null) {
return;
}
String jasonData = Json.encode(data);
String url = provider.getURL();
IpPort host = NetUtils.parseIpPortFromURI(endpoint);
doSend(endpoint, jasonData, url, host, 0);
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class ServiceRegistryConfigCustomizer method addressListFromConfiguration.
public ServiceRegistryConfigCustomizer addressListFromConfiguration(String configuration) {
String address = DynamicPropertyFactory.getInstance().getStringProperty(configuration, null).get();
if (address == null) {
throw new IllegalStateException("service center address is required.");
}
String[] urls = address.split(",");
List<String> uriList = Arrays.asList(urls);
ArrayList<IpPort> ipPortList = new ArrayList<>();
uriList.forEach(anUriList -> {
try {
URI uri = new URI(anUriList.trim());
if ("https".equals(uri.getScheme())) {
this.original.setSsl(true);
}
ipPortList.add(NetUtils.parseIpPort(uri));
} catch (Exception e) {
throw new IllegalStateException("service center address is required.", e);
}
});
this.original.setIpPort(ipPortList);
return this;
}
Aggregations