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);
}
}
}
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);
}
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;
}
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;
}
};
}
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);
}
Aggregations