Search in sources :

Example 41 with DefaultRequest

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

the class ConfigurableWeightLoadBalanceTest method testDoSelect.

@Test
public void testDoSelect() {
    int[] groupWeight = new int[] { 2, 3, 5 };
    Map<String, AtomicInteger> counter = generate(3, groupWeight, new int[] { 3, 4, 5 });
    for (int j = 0; j < 100; j++) {
        int size = 100;
        for (int i = 0; i < size; i++) {
            Referer referer = balance.doSelect(new DefaultRequest());
            String group = referer.getServiceUrl().getGroup();
            counter.get(group).incrementAndGet();
        }
        for (String key : counter.keySet()) {
            float total = size * (j + 1);
            float ratio = counter.get(key).get() * 10 / total;
            int weight = groupWeight[Integer.parseInt(key.substring("group".length()))];
            // 权重误差不超过阈值。
            Assert.assertTrue(Math.abs(weight - ratio) < 2);
        }
    }
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Referer(com.weibo.api.motan.rpc.Referer) MockReferer(com.weibo.api.motan.mock.MockReferer) Test(org.junit.Test)

Example 42 with DefaultRequest

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

the class DefaultRpcCodecTest method testObjectArrayRequest.

@Test
public void testObjectArrayRequest() throws Exception {
    DefaultRequest request = getRequest("com.weibo.api.motan.protocol.example.Model[]", new Object[] { new Model[] { new Model("hello", 11, Model.class), new Model("world", 12, Model.class) } });
    testCodecRequest(request);
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Model(com.weibo.api.motan.protocol.example.Model) Test(org.junit.Test)

Example 43 with DefaultRequest

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

the class DefaultRpcCodecTest method requestSize.

/**
 * 不带参数的Request大小
 */
private static byte[] requestSize(DefaultRpcCodec codec, Channel channel, Object data) throws Exception {
    DefaultRequest request = new DefaultRequest();
    request.setInterfaceName(IHello.class.getName());
    request.setMethodName("hello");
    if (data != null) {
        request.setParamtersDesc("java.util.HashSet");
        request.setArguments(new Object[] { data });
    } else {
        request.setParamtersDesc("void");
    }
    request.setRequestId(System.currentTimeMillis());
    request.setAttachment("application", "helloApplication");
    request.setAttachment("module", "helloModule");
    request.setAttachment("version", "1.0");
    request.setAttachment("graph", "yf-rpc");
    byte[] bytes = codec.encode(channel, request);
    return bytes;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 44 with DefaultRequest

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

the class OpenTracingFilterTest method setUp.

@Before
public void setUp() throws Exception {
    OTFilter = new OpenTracingFilter();
    tracer = new MockTracer();
    OpenTracingContext.tracerFactory = new TracerFactory() {

        @Override
        public Tracer getTracer() {
            return tracer;
        }
    };
    URL url = new URL("motan", "localhost", 8002, "HelloService");
    request = new DefaultRequest();
    request.setInterfaceName("HelloService");
    request.setAttachment(URLParamType.group.name(), "test");
    request.setMethodName("sayHello");
    request.setParamtersDesc("java.lang.String");
    response = new DefaultResponse();
    refer = new AbstractReferer<HelloService>(HelloService.class, url) {

        @Override
        public void destroy() {
        }

        @Override
        public boolean isAvailable() {
            return true;
        }

        @Override
        protected Response doCall(Request request) {
            return response;
        }

        @Override
        protected boolean doInit() {
            return true;
        }
    };
    provider = new DefaultProvider<HelloService>(new HelloServiceImpl(), url, HelloService.class) {

        @Override
        public Response call(Request request) {
            return response;
        }
    };
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Tracer(io.opentracing.Tracer) MockTracer(io.opentracing.mock.MockTracer) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) MockTracer(io.opentracing.mock.MockTracer) URL(com.weibo.api.motan.rpc.URL) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) Before(org.junit.Before)

Example 45 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest 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

DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)45 Test (org.junit.Test)26 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)8 Request (com.weibo.api.motan.rpc.Request)7 Model (com.weibo.api.motan.protocol.example.Model)5 Response (com.weibo.api.motan.rpc.Response)5 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)4 Channel (com.weibo.api.motan.transport.Channel)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IHello (com.weibo.api.motan.protocol.example.IHello)3 URL (com.weibo.api.motan.rpc.URL)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 HashMap (java.util.HashMap)3 MockChannel (com.weibo.api.motan.mock.MockChannel)2 MockReferer (com.weibo.api.motan.mock.MockReferer)2 Referer (com.weibo.api.motan.rpc.Referer)2 Listener (io.grpc.ServerCall.Listener)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Codec (com.weibo.api.motan.codec.Codec)1 Serialization (com.weibo.api.motan.codec.Serialization)1