dubbo中ConsumerContextFilter的作用是什么
这期内容当中小编将会给大家带来有关dubbo中ConsumerContextFilter的作用是什么,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
景泰ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
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"); Invokerinvoker = new MyInvoker (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
上述就是小编为大家分享的dubbo中ConsumerContextFilter的作用是什么了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
分享题目:dubbo中ConsumerContextFilter的作用是什么
网页网址:http://hbruida.cn/article/jiicjo.html