sequence

This article focuses on Dubbo’s ConsumerContextFilter

ConsumerContextFilter

Dubbo – 2.7.2 / dubbo – RPC/dubbo xml-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();
            returninvoker.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) { } } }Copy the code
  • ConsumerContextFilter inherits ListenableFilter, The invoke method will set rpcContext.getContext () to invoker, Invocation, localAddress, remoteAddress and remove serverContext before invoking; Remove the current context after invoke The listener is ConsumerContextListener, when onResponse will give RpcContext. GetServerContext () sets the attachments

The instance

Dubbo – 2.7.2 / dubbo – RPC/dubbo xml-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());
            returnresult; }); }}Copy the code
  • Here use asyncResult. ThenApplyWithContext to investigate RpcContext. GetContext () the invoker, invocation, localhost, remoteAddress values

summary

ConsumerContextFilter inherits ListenableFilter, The invoke method will set rpcContext.getContext () to invoker, Invocation, localAddress, remoteAddress and remove serverContext before invoking; Remove the current context after invoke The listener is ConsumerContextListener, when onResponse will give RpcContext. GetServerContext () sets the attachments

doc

  • ConsumerContextFilter