use of com.alipay.sofa.rpc.api.context.RpcServiceContext 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.RpcServiceContext in project sofa-rpc by sofastack.
the class RpcServiceContextFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
RpcServiceContext serviceCtx = new RpcServiceContext();
RpcInternalContext internalCtx = RpcInternalContext.getContext();
serviceCtx.setServiceName(request.getTargetServiceUniqueName());
serviceCtx.setMethodName(request.getMethodName());
serviceCtx.setTraceId((String) internalCtx.getAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID));
serviceCtx.setRpcId((String) internalCtx.getAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID));
serviceCtx.setCallerAppName((String) request.getRequestProp(RemotingConstants.HEAD_APP_NAME));
serviceCtx.setCallerUrl(internalCtx.getRemoteHostName());
RpcInvokeContext.getContext().put(RemotingConstants.INVOKE_CTX_RPC_SER_CTX, serviceCtx);
return invoker.invoke(request);
}
Aggregations