Search in sources :

Example 21 with IpPort

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);
}
Also used : JksOptions(io.vertx.core.net.JksOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) HttpServer(io.vertx.core.http.HttpServer) Router(io.vertx.ext.web.Router) IpPort(io.servicecomb.foundation.common.net.IpPort)

Example 22 with IpPort

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);
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClient(io.vertx.core.http.HttpClient) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Example 23 with IpPort

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);
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) Invocation(io.servicecomb.core.Invocation) RestOperationMeta(io.servicecomb.common.rest.definition.RestOperationMeta) HttpClient(io.vertx.core.http.HttpClient) IpPort(io.servicecomb.foundation.common.net.IpPort) AsyncResponse(io.servicecomb.core.AsyncResponse) Test(org.junit.Test)

Example 24 with IpPort

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;
        }
    };
}
Also used : HttpServer(io.vertx.core.http.HttpServer) Future(io.vertx.core.Future) MockUp(mockit.MockUp) IpPort(io.servicecomb.foundation.common.net.IpPort)

Example 25 with IpPort

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();
        }
    });
}
Also used : HttpClientRequest(io.vertx.core.http.HttpClientRequest) HttpClientWithContext(io.servicecomb.foundation.vertx.client.http.HttpClientWithContext) IpPort(io.servicecomb.foundation.common.net.IpPort) MultiMap(io.vertx.core.MultiMap) HashMap(java.util.HashMap) Map(java.util.Map) HttpMethod(io.vertx.core.http.HttpMethod)

Aggregations

IpPort (io.servicecomb.foundation.common.net.IpPort)35 ClientException (io.servicecomb.serviceregistry.client.ClientException)15 CountDownLatch (java.util.concurrent.CountDownLatch)14 Holder (javax.xml.ws.Holder)14 Test (org.junit.Test)10 HttpClientRequest (io.vertx.core.http.HttpClientRequest)7 AsyncResponse (io.servicecomb.core.AsyncResponse)6 Invocation (io.servicecomb.core.Invocation)6 HttpClientResponse (io.vertx.core.http.HttpClientResponse)6 RestOperationMeta (io.servicecomb.common.rest.definition.RestOperationMeta)5 HttpClient (io.vertx.core.http.HttpClient)4 OperationMeta (io.servicecomb.core.definition.OperationMeta)3 HttpClientWithContext (io.servicecomb.foundation.vertx.client.http.HttpClientWithContext)3 HttpServer (io.vertx.core.http.HttpServer)3 OperationProtobuf (io.servicecomb.codec.protobuf.definition.OperationProtobuf)2 UpdatePropertiesRequest (io.servicecomb.serviceregistry.api.request.UpdatePropertiesRequest)2 GetExistenceResponse (io.servicecomb.serviceregistry.api.response.GetExistenceResponse)2 CacheEndpoint (io.servicecomb.serviceregistry.cache.CacheEndpoint)2 ArrayList (java.util.ArrayList)2 Expectations (mockit.Expectations)2