共计 2567 个字符,预计需要花费 7 分钟才能阅读完成。
序
本文主要研究一下 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
- ConsumerContextFilter
正文完