Search in sources :

Example 1 with RpcReferenceContext

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]);
    }
}
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 RpcReferenceContext

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;
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) InetSocketAddress(java.net.InetSocketAddress) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RpcReferenceContext(com.alipay.sofa.rpc.api.context.RpcReferenceContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Aggregations

RpcReferenceContext (com.alipay.sofa.rpc.api.context.RpcReferenceContext)2 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)2 RpcServiceContext (com.alipay.sofa.rpc.api.context.RpcServiceContext)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)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 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)1 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)1 HelloService (com.alipay.sofa.rpc.test.HelloService)1 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1