use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.
the class ConfigCenterClient method refreshMembers.
private void refreshMembers(MemberDiscovery memberDiscovery) {
if (CONFIG_CENTER_CONFIG.getAutoDiscoveryEnabled()) {
String configCenter = memberDiscovery.getConfigServer();
IpPort ipPort = NetUtils.parseIpPortFromURI(configCenter);
clientMgr.findThreadBindClientPool().runOnContext(client -> {
HttpClientRequest request = client.get(ipPort.getPort(), ipPort.getHostOrIp(), URIConst.MEMBERS, rsp -> {
if (rsp.statusCode() == HttpResponseStatus.OK.code()) {
rsp.bodyHandler(buf -> {
memberDiscovery.refreshMembers(buf.toJsonObject());
});
}
});
SignRequest signReq = createSignRequest(request.method().toString(), configCenter + URIConst.MEMBERS, new HashMap<>(), null);
if (ConfigCenterConfig.INSTANCE.getToken() != null) {
request.headers().add("X-Auth-Token", ConfigCenterConfig.INSTANCE.getToken());
}
authHeaderProviders.forEach(provider -> request.headers().addAll(provider.getSignAuthHeaders(signReq)));
request.end();
});
}
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project incubator-servicecomb-java-chassis by apache.
the class TestRestServletInitializer method setUp.
@Before
public void setUp() throws Exception {
Configuration configuration = (Configuration) DynamicPropertyFactory.getBackingConfigurationSource();
configuration.clearProperty(ServletConfig.KEY_SERVLET_URL_PATTERN);
configuration.setProperty(ServletConfig.KEY_CSE_REST_ADDRESS, LISTEN_ADDRESS);
PowerMockito.mockStatic(NetUtils.class);
PowerMockito.when(NetUtils.parseIpPortFromURI(anyString())).thenReturn(new IpPort(LISTEN_ADDRESS, TEST_PORT));
PowerMockito.when(NetUtils.canTcpListen(anyObject(), anyInt())).thenReturn(false);
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class TestServiceRegistryClientImpl method undateMicroserviceInstanceStatus_response_failure.
@SuppressWarnings("deprecation")
@Test
public void undateMicroserviceInstanceStatus_response_failure() {
HttpClientResponse httpClientResponse = new MockUp<HttpClientResponse>() {
@Mock
int statusCode() {
return 400;
}
}.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.assertFalse(result);
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class RestClientInvocation method invoke.
public void invoke(Invocation invocation, AsyncResponse asyncResp) throws Exception {
this.invocation = invocation;
this.asyncResp = asyncResp;
OperationMeta operationMeta = invocation.getOperationMeta();
restOperationMeta = operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
String path = this.createRequestPath(restOperationMeta);
IpPort ipPort = (IpPort) invocation.getEndpoint().getAddress();
Future<HttpClientRequest> requestFuture = createRequest(ipPort, path);
invocation.getInvocationStageTrace().startGetConnection();
requestFuture.compose(clientRequest -> {
invocation.getInvocationStageTrace().finishGetConnection();
this.clientRequest = clientRequest;
clientRequest.putHeader(org.apache.servicecomb.core.Const.TARGET_MICROSERVICE, invocation.getMicroserviceName());
RestClientRequestImpl restClientRequest = new RestClientRequestImpl(clientRequest, httpClientWithContext.context(), asyncResp, throwableHandler);
invocation.getHandlerContext().put(RestConst.INVOCATION_HANDLER_REQUESTCLIENT, restClientRequest);
Buffer requestBodyBuffer;
try {
requestBodyBuffer = restClientRequest.getBodyBuffer();
} catch (Exception e) {
return Future.failedFuture(e);
}
HttpServletRequestEx requestEx = new VertxClientRequestToHttpServletRequest(clientRequest, requestBodyBuffer);
invocation.getInvocationStageTrace().startClientFiltersRequest();
for (HttpClientFilter filter : httpClientFilters) {
if (filter.enabled()) {
filter.beforeSendRequest(invocation, requestEx);
}
}
// 从业务线程转移到网络线程中去发送
invocation.onStartSendRequest();
httpClientWithContext.runOnContext(httpClient -> {
clientRequest.setTimeout(operationMeta.getConfig().getMsRequestTimeout());
clientRequest.response().onComplete(asyncResult -> {
if (asyncResult.failed()) {
fail(asyncResult.cause());
return;
}
handleResponse(asyncResult.result());
});
processServiceCombHeaders(invocation, operationMeta);
restClientRequest.end().onComplete((t) -> invocation.getInvocationStageTrace().finishWriteToBuffer(System.nanoTime()));
});
return Future.succeededFuture();
}).onFailure(failure -> {
invocation.getTraceIdLogger().error(LOGGER, "Failed to send request, alreadyFailed:{}, local:{}, remote:{}, message={}.", alreadyFailed, getLocalAddress(), ipPort.getSocketAddress(), ExceptionUtils.getExceptionMessageWithoutTrace(failure));
throwableHandler.handle(failure);
});
}
use of org.apache.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class RegistrationManager method genPublishIpPort.
private static IpPort genPublishIpPort(String schema, IpPort ipPort) {
String publicAddressSetting = DynamicPropertyFactory.getInstance().getStringProperty(PUBLISH_ADDRESS, "").get();
publicAddressSetting = publicAddressSetting.trim();
String publishPortKey = PUBLISH_PORT.replace("{transport_name}", schema);
int publishPortSetting = DynamicPropertyFactory.getInstance().getIntProperty(publishPortKey, 0).get();
int publishPort = publishPortSetting == 0 ? ipPort.getPort() : publishPortSetting;
if (publicAddressSetting.isEmpty()) {
InetSocketAddress socketAddress = ipPort.getSocketAddress();
if (socketAddress.getAddress().isAnyLocalAddress()) {
String host = NetUtils.getHostAddress();
if (Inet6Address.class.isInstance(socketAddress.getAddress())) {
host = NetUtils.getIpv6HostAddress();
}
LOGGER.warn("address {}, auto select a host address to publish {}:{}, maybe not the correct one", socketAddress, host, publishPort);
return new IpPort(host, publishPort);
}
return ipPort;
}
if (publicAddressSetting.startsWith("{") && publicAddressSetting.endsWith("}")) {
publicAddressSetting = NetUtils.ensureGetInterfaceAddress(publicAddressSetting.substring(1, publicAddressSetting.length() - 1)).getHostAddress();
}
return new IpPort(publicAddressSetting, publishPort);
}
Aggregations