use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class GrpcVerticle method startListen.
protected void startListen(Future<Void> startFuture) {
// 如果本地未配置grpc地址,则表示不必监听,只需要作为客户端使用即可
if (StringUtils.isEmpty(this.endpoint)) {
LOGGER.warn("grpc listen address is not configured, will not listen.");
startFuture.complete();
return;
}
Router mainRouter = Router.router(vertx);
mainRouter.route().handler(new GrpcBodyHandler());
new GrpcServer(mainRouter);
HttpServerOptions serverOptions = new HttpServerOptions();
serverOptions.setAcceptBacklog(ACCEPT_BACKLOG);
serverOptions.setSendBufferSize(SEND_BUFFER_SIZE);
serverOptions.setReceiveBufferSize(RECEIVE_BUFFER_SIZE);
serverOptions.setUsePooledBuffers(true);
String key = System.getProperty("store.key");
if (key != null && !key.isEmpty()) {
serverOptions.setUseAlpn(true);
serverOptions.setSsl(true);
serverOptions.setKeyStoreOptions(new JksOptions().setPath(System.getProperty("store.key")).setPassword(System.getProperty("store.pass")));
}
HttpServer server = vertx.createHttpServer(serverOptions).requestHandler(mainRouter::accept);
IpPort ipPort = NetUtils.parseIpPortFromURI(this.endpoint);
if (ipPort == null) {
LOGGER.error("wrong grpc listen address {}", this.endpoint);
return;
}
startListen(server, ipPort, startFuture);
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class TestVertxGetMethod method testVertxGetMethod.
@Test
public void testVertxGetMethod() {
HttpClient client = Mockito.mock(HttpClient.class);
Invocation invocation = Mockito.mock(Invocation.class);
IpPort ipPort = Mockito.mock(IpPort.class);
Mockito.when(ipPort.getPort()).thenReturn(10);
assertEquals(10, ipPort.getPort());
Mockito.when(ipPort.getHostOrIp()).thenReturn("ever");
assertEquals("ever", ipPort.getHostOrIp());
RestOperationMeta operation = Mockito.mock(RestOperationMeta.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
HttpClientRequest obj = VertxGetMethod.INSTANCE.createRequest(client, invocation, ipPort, "good", operation, asyncResp);
Assert.assertNull(obj);
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class TestVertxPostMethod method testVertxPostMethod.
@Test
public void testVertxPostMethod() {
Invocation invocation = Mockito.mock(Invocation.class);
HttpClient client = Mockito.mock(HttpClient.class);
IpPort ipPort = Mockito.mock(IpPort.class);
RestOperationMeta operation = Mockito.mock(RestOperationMeta.class);
AsyncResponse asyncResp = Mockito.mock(AsyncResponse.class);
Mockito.when(ipPort.getPort()).thenReturn(23);
assertEquals(23, ipPort.getPort());
Mockito.when(ipPort.getHostOrIp()).thenReturn("testCall");
assertNotNull("testCall", ipPort.getHostOrIp());
HttpClientRequest obj = VertxPostMethod.INSTANCE.createRequest(client, invocation, ipPort, "test", operation, asyncResp);
Assert.assertNull(obj);
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class MockForRestServerVerticle method mockRestServerVerticle.
public void mockRestServerVerticle() {
final HttpServer server = Mockito.mock(HttpServer.class);
new MockUp<RestServerVerticle>() {
@Mock
private void startListen(HttpServer server, IpPort ipPort, Future<Void> startFuture) {
}
@Mock
private HttpServer createHttpServer(boolean isHttp_2) {
return server;
}
};
}
use of io.servicecomb.foundation.common.net.IpPort in project java-chassis by ServiceComb.
the class RestUtils method httpDo.
public static void httpDo(RequestContext requestContext, Handler<RestResponse> responseHandler) {
HttpClientWithContext vertxHttpClient = HttpClientPool.INSTANCE.getClient();
vertxHttpClient.runOnContext(httpClient -> {
IpPort ipPort = requestContext.getIpPort();
HttpMethod httpMethod = requestContext.getMethod();
RequestParam requestParam = requestContext.getParams();
if (ipPort == null) {
LOGGER.error("request address is null");
responseHandler.handle(new RestResponse(requestContext, null));
return;
}
StringBuilder url = new StringBuilder(requestContext.getUri());
String queryParams = requestParam.getQueryParams();
if (!queryParams.isEmpty()) {
url.append(url.lastIndexOf("?") > 0 ? "&" : "?").append(queryParams);
}
HttpClientRequest httpClientRequest = httpClient.request(httpMethod, ipPort.getPort(), ipPort.getHostOrIp(), url.toString(), response -> {
responseHandler.handle(new RestResponse(requestContext, response));
});
httpClientRequest.setTimeout(ServiceRegistryConfig.INSTANCE.getRequestTimeout()).exceptionHandler(e -> {
LOGGER.error("{} {} fail, endpoint is {}:{}, message: {}", httpMethod, url.toString(), ipPort.getHostOrIp(), ipPort.getPort(), e.getMessage());
responseHandler.handle(new RestResponse(requestContext, null));
});
addDefaultHeaders(httpClientRequest);
if (requestParam.getHeaders() != null && requestParam.getHeaders().size() > 0) {
for (Map.Entry<String, String> header : requestParam.getHeaders().entrySet()) {
httpClientRequest.putHeader(header.getKey(), header.getValue());
}
}
if (requestParam.getCookies() != null && requestParam.getCookies().size() > 0) {
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, String> cookie : requestParam.getCookies().entrySet()) {
stringBuilder.append(cookie.getKey()).append("=").append(cookie.getValue()).append("; ");
}
httpClientRequest.putHeader("Cookie", stringBuilder.toString());
}
if (httpMethod != HttpMethod.GET && requestParam.getBody() != null && requestParam.getBody().length > 0) {
httpClientRequest.end(Buffer.buffer(requestParam.getBody()));
} else {
httpClientRequest.end();
}
});
}
Aggregations