use of com.alipay.sofa.rpc.api.context.RpcReferenceContext in project sofa-rpc by sofastack.
the class RpcContextTest method testAll.
@Test
public void testAll() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
CtxHelloServiceImpl helloServiceImpl = new CtxHelloServiceImpl();
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-server")).setRef(helloServiceImpl).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(30000).setRegister(false);
final HelloService helloService = consumerConfig.refer();
String str = helloService.sayHello("xxx", 123);
RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
Assert.assertNull(serviceContext);
Assert.assertNotNull(referenceContext);
serviceContext = helloServiceImpl.serviceContext;
Assert.assertNotNull(serviceContext);
Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
Assert.assertNotNull(referenceContext.getClientIP());
Assert.assertTrue(referenceContext.getClientPort() > 0);
}
{
final CountDownLatch latch = new CountDownLatch(1);
final String[] ret = { null };
ConsumerConfig<HelloService> consumerConfig2 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(2000).setInvokeType("callback").setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
ret[0] = (String) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
}).setRegister(false);
final HelloService helloServiceCallback = consumerConfig2.refer();
String ret0 = helloServiceCallback.sayHello("xxx", 22);
// 第一次返回null
Assert.assertNull(ret0);
RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
Assert.assertNull(serviceContext);
Assert.assertNotNull(referenceContext);
serviceContext = helloServiceImpl.serviceContext;
Assert.assertNotNull(serviceContext);
Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
Assert.assertNotNull(referenceContext.getClientIP());
Assert.assertTrue(referenceContext.getClientPort() > 0);
try {
latch.await(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertNotNull(ret[0]);
}
}
use of com.alipay.sofa.rpc.api.context.RpcReferenceContext in project sofa-rpc by sofastack.
the class RpcReferenceContextFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
ConsumerConfig config = (ConsumerConfig) invoker.getConfig();
RpcReferenceContext referenceCtx = new RpcReferenceContext();
referenceCtx.setGeneric(config.isGeneric());
referenceCtx.setInterfaceName(config.getInterfaceId());
referenceCtx.setUniqueId(config.getUniqueId());
referenceCtx.setServiceName(request.getTargetServiceUniqueName());
referenceCtx.setMethodName(request.getMethodName());
RpcInternalContext context = RpcInternalContext.getContext();
ProviderInfo providerInfo = context.getProviderInfo();
if (providerInfo != null) {
referenceCtx.setTargetAppName(providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
referenceCtx.setTargetUrl(providerInfo.getHost() + ":" + providerInfo.getPort());
}
referenceCtx.setProtocol(config.getProtocol());
referenceCtx.setInvokeType(request.getInvokeType());
referenceCtx.setRouteRecord((String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
RpcInvokeContext.getContext().put(RemotingConstants.INVOKE_CTX_RPC_REF_CTX, referenceCtx);
SofaResponse response = invoker.invoke(request);
// 调用后
InetSocketAddress local = context.getLocalAddress();
if (local != null) {
referenceCtx.setClientIP(NetUtils.toIpString(local));
referenceCtx.setClientPort(local.getPort());
}
Long ct = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME);
if (ct != null) {
referenceCtx.setConnEstablishedSpan(ct);
}
Integer qs = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE);
if (qs != null) {
referenceCtx.setRequestSize(qs.longValue());
}
Integer ps = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE);
if (ps != null) {
referenceCtx.setResponseSize(ps.longValue());
}
referenceCtx.setTraceId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID));
referenceCtx.setRpcId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID));
Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
if (ce != null) {
referenceCtx.setCostTime(ce);
}
return response;
}
Aggregations