ConsumerContextFilter implements Filter { @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 { RpcResult result = (RpcResult) invoker.invoke(invocation); RpcContext.getServerContext().setAttachments(result.getAttachments()); return result; } finally { RpcContext.getContext().clearAttachments(); } }  @Override Result invoke(Invoker<?> invoker, Invocation invocation);  }
@Test public void testSetContext() { URL url = URL.valueOf("test: Invoker<DemoService> invoker = new MyInvoker<DemoService>(url); Invocation invocation = new MockInvocation(); consumerContextFilter.invoke(invoker, invocation); assertEquals(invoker, RpcContext.getContext().getInvoker()); assertEquals(invocation, RpcContext.getContext().getInvocation()); assertEquals(NetUtils.getLocalHost() + ":0", RpcContext.getContext().getLocalAddressString()); assertEquals("test:11", RpcContext.getContext().getRemoteAddressString()); }