Search in sources :

Example 1 with Provider

use of com.weibo.api.motan.rpc.Provider in project motan by weibocom.

the class YarMessageRouterTest method testHandle.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandle() {
    response = new DefaultResponse();
    response.setValue("test");
    response.setProcessTime(1);
    Provider provider = new DefaultProvider(null, null, AnnoService.class);
    router.addProvider(provider);
    YarRequest yarRequest = new YarRequest(1, "JSON", "hello", new Object[] { "params" });
    yarRequest.setRequestPath(requestPath);
    YarResponse yarResponse = (YarResponse) router.handle(null, yarRequest);
    assertEquals(YarProtocolUtil.convert(response, "JSON"), yarResponse);
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) YarRequest(com.weibo.yar.YarRequest) YarResponse(com.weibo.yar.YarResponse) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Provider(com.weibo.api.motan.rpc.Provider) Test(org.junit.Test)

Example 2 with Provider

use of com.weibo.api.motan.rpc.Provider in project motan by weibocom.

the class YarRpcProtocolTest method testInitRequestRouter.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testInitRequestRouter() {
    YarRpcProtocol protocol = new YarRpcProtocol();
    URL url = new URL("motan", "localhost", 8002, "urlpath");
    Provider provider = new DefaultProvider(null, url, MessageHandler.class);
    ProviderMessageRouter router = protocol.initRequestRouter(url, provider);
    assertNotNull(router);
    URL url2 = new URL("motan", "localhost", 8003, "urlpath2");
    Provider provider2 = new DefaultProvider(null, url2, MessageHandler.class);
    ProviderMessageRouter router2 = protocol.initRequestRouter(url2, provider2);
    assertNotNull(router2);
    assertFalse(router2.equals(router));
    URL url3 = new URL("motan", "localhost", 8002, "urlpath3");
    Provider provider3 = new DefaultProvider(null, url3, MessageHandler.class);
    ProviderMessageRouter router3 = protocol.initRequestRouter(url3, provider3);
    assertNotNull(router3);
    assertTrue(router3.equals(router));
    try {
        protocol.initRequestRouter(url, provider);
        assertTrue(false);
    } catch (Exception e) {
        assertTrue(e.getMessage().contains("duplicate yar provider"));
    }
}
Also used : ProviderMessageRouter(com.weibo.api.motan.transport.ProviderMessageRouter) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) URL(com.weibo.api.motan.rpc.URL) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Provider(com.weibo.api.motan.rpc.Provider) Test(org.junit.Test)

Example 3 with Provider

use of com.weibo.api.motan.rpc.Provider in project motan by weibocom.

the class YarMessageRouterTest method testAddProvider.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testAddProvider() {
    Provider provider = new DefaultProvider(null, null, AnnoService.class);
    router.addProvider(provider);
    assertTrue(router.checkProvider(requestPath));
    router.removeProvider(provider);
    assertFalse(router.checkProvider(requestPath));
    URL url = new URL("motan", "localhost", 8002, "urlpath");
    provider = new DefaultProvider(null, url, normalService.class);
    router.addProvider(provider);
    assertTrue(router.checkProvider(YarProtocolUtil.getYarPath(normalService.class, url)));
    router.removeProvider(provider);
    assertFalse(router.checkProvider(YarProtocolUtil.getYarPath(normalService.class, url)));
}
Also used : DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) URL(com.weibo.api.motan.rpc.URL) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Provider(com.weibo.api.motan.rpc.Provider) Test(org.junit.Test)

Example 4 with Provider

use of com.weibo.api.motan.rpc.Provider in project motan by weibocom.

the class GrpcServer method addExporter.

@SuppressWarnings("rawtypes")
public void addExporter(Exporter<?> exporter) throws Exception {
    Provider provider = exporter.getProvider();
    ServerServiceDefinition serviceDefine = GrpcUtil.getServiceDefByAnnotation(provider.getInterface());
    boolean urlShareChannel = exporter.getUrl().getBooleanParameter(URLParamType.shareChannel.getName(), URLParamType.shareChannel.getBooleanValue());
    synchronized (serviceDefinetions) {
        if (!(shareChannel && urlShareChannel) && !serviceDefinetions.isEmpty()) {
            URL url = serviceDefinetions.keySet().iterator().next();
            throw new MotanFrameworkException("url:" + exporter.getUrl() + " cannot share channel with url:" + url);
        }
        registry.addService(serviceDefine, provider);
        if (httpHandler != null) {
            httpHandler.addProvider(provider);
        }
        serviceDefinetions.put(exporter.getUrl(), serviceDefine);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ServerServiceDefinition(io.grpc.ServerServiceDefinition) URL(com.weibo.api.motan.rpc.URL) Provider(com.weibo.api.motan.rpc.Provider)

Example 5 with Provider

use of com.weibo.api.motan.rpc.Provider in project motan by weibocom.

the class NettyHttpRequestHandler method processHttpRequest.

@SuppressWarnings("rawtypes")
protected void processHttpRequest(ChannelHandlerContext ctx, FullHttpRequest httpRequest) {
    FullHttpResponse httpResponse = null;
    try {
        DefaultRequest rpcRequest = buildRpcRequest(httpRequest);
        String ip = NetUtils.getHostName(ctx.channel().remoteAddress());
        if (ip != null) {
            rpcRequest.setAttachment(URLParamType.host.getName(), ip);
        }
        Provider provider = providerMap.get(rpcRequest.getInterfaceName());
        if (provider == null) {
            httpResponse = buildErrorResponse("request service not exist. service:" + rpcRequest.getInterfaceName());
        } else {
            Response response = provider.call(rpcRequest);
            httpResponse = buildHttpResponse(response, HttpUtil.isKeepAlive(httpRequest));
        }
    } catch (Exception e) {
        LoggerUtil.error("NettyHttpHandler process http request fail.", e);
        httpResponse = buildErrorResponse(e.getMessage());
    } finally {
        httpRequest.content().release();
    }
    sendResponse(ctx, httpResponse);
}
Also used : Response(com.weibo.api.motan.rpc.Response) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Provider(com.weibo.api.motan.rpc.Provider)

Aggregations

Provider (com.weibo.api.motan.rpc.Provider)5 DefaultProvider (com.weibo.api.motan.rpc.DefaultProvider)3 URL (com.weibo.api.motan.rpc.URL)3 Test (org.junit.Test)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)1 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)1 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)1 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)1 Response (com.weibo.api.motan.rpc.Response)1 ProviderMessageRouter (com.weibo.api.motan.transport.ProviderMessageRouter)1 YarRequest (com.weibo.yar.YarRequest)1 YarResponse (com.weibo.yar.YarResponse)1 ServerServiceDefinition (io.grpc.ServerServiceDefinition)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1