聊聊dubbo的ConsumerContextFilter

栏目: Java · 发布时间: 4年前

内容简介:本文主要研究一下dubbo的ConsumerContextFilterdubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.javadubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ConsumerContextFilterTest.java

本文主要研究一下dubbo的ConsumerContextFilter

ConsumerContextFilter

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/ConsumerContextFilter.java

@Activate(group = CONSUMER, order = -10000)
public class ConsumerContextFilter extends ListenableFilter {

    public ConsumerContextFilter() {
        super.listener = new ConsumerContextListener();
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        RpcContext.getContext()
                .setInvoker(invoker)
                .setInvocation(invocation)
                .setLocalAddress(NetUtils.getLocalHost(), 0)
                .setRemoteAddress(invoker.getUrl().getHost(),
                        invoker.getUrl().getPort());
        if (invocation instanceof RpcInvocation) {
            ((RpcInvocation) invocation).setInvoker(invoker);
        }
        try {
            RpcContext.removeServerContext();
            return invoker.invoke(invocation);
        } finally {
            RpcContext.removeContext();
        }
    }

    static class ConsumerContextListener implements Listener {
        @Override
        public void onResponse(Result appResponse, Invoker<?> invoker, Invocation invocation) {
            RpcContext.getServerContext().setAttachments(appResponse.getAttachments());
        }

        @Override
        public void onError(Throwable t, Invoker<?> invoker, Invocation invocation) {

        }
    }
}
  • ConsumerContextFilter继承了ListenableFilter,其invoke方法在invoke之前会给RpcContext.getContext()设置invoker、invocation、localAddress、remoteAddress并移除serverContext;在invoke之后移除当前context;其listener为ConsumerContextListener,在onResponse的时候,会给RpcContext.getServerContext()设置attachments

实例

dubbo-2.7.2/dubbo-rpc/dubbo-rpc-api/src/test/java/org/apache/dubbo/rpc/filter/ConsumerContextFilterTest.java

public class ConsumerContextFilterTest {
    Filter consumerContextFilter = new ConsumerContextFilter();

    @Test
    public void testSetContext() {
        URL url = URL.valueOf("test://test:11/test?group=dubbo&version=1.1");
        Invoker<DemoService> invoker = new MyInvoker<DemoService>(url);
        Invocation invocation = new MockInvocation();
        Result asyncResult = consumerContextFilter.invoke(invoker, invocation);
        asyncResult.thenApplyWithContext(result -> {
            assertEquals(invoker, RpcContext.getContext().getInvoker());
            assertEquals(invocation, RpcContext.getContext().getInvocation());
            assertEquals(NetUtils.getLocalHost() + ":0", RpcContext.getContext().getLocalAddressString());
            assertEquals("test:11", RpcContext.getContext().getRemoteAddressString());
            return result;
        });
    }
}
  • 这里使用asyncResult.thenApplyWithContext来验证一下RpcContext.getContext()的invoker、invocation、localhost、remoteAddress值

小结

ConsumerContextFilter继承了ListenableFilter,其invoke方法在invoke之前会给RpcContext.getContext()设置invoker、invocation、localAddress、remoteAddress并移除serverContext;在invoke之后移除当前context;其listener为ConsumerContextListener,在onResponse的时候,会给RpcContext.getServerContext()设置attachments

doc


以上所述就是小编给大家介绍的《聊聊dubbo的ConsumerContextFilter》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Cracking the Coding Interview

Cracking the Coding Interview

Gayle Laakmann McDowell / CareerCup / 2015-7-1 / USD 39.95

Cracking the Coding Interview, 6th Edition is here to help you through this process, teaching you what you need to know and enabling you to perform at your very best. I've coached and interviewed hund......一起来看看 《Cracking the Coding Interview》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

SHA 加密
SHA 加密

SHA 加密工具