Search in sources :

Example 1 with RpcServiceContext

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]);
    }
}
Also used : HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RpcServiceContext(com.alipay.sofa.rpc.api.context.RpcServiceContext) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RpcReferenceContext(com.alipay.sofa.rpc.api.context.RpcReferenceContext) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 2 with RpcServiceContext

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);
}
Also used : RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) RpcServiceContext(com.alipay.sofa.rpc.api.context.RpcServiceContext)

Aggregations

RpcServiceContext (com.alipay.sofa.rpc.api.context.RpcServiceContext)2 RpcReferenceContext (com.alipay.sofa.rpc.api.context.RpcReferenceContext)1 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)1 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)1 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)1 HelloService (com.alipay.sofa.rpc.test.HelloService)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1