dubbo为consumer端的代理对象实现了EchoService接口。
使用示例:
<dubbo:reference id="hello" interface="com.zhang.HelloService" check="false" />
EchoService echoService = (EchoService) appCtx.getBean("hello");
Object str = echoService.$echo("zhang");
原理:
public interface EchoService { Object $echo(Object message); }
//AbstractProxyFactory public <T> T getProxy(Invoker<T> invoker) throws RpcException { Class<?>[] interfaces = null; String config = invoker.getUrl().getParameter("interfaces"); if (config != null && config.length() > 0) { String[] types = Constants.COMMA_SPLIT_PATTERN.split(config); if (types != null && types.length > 0) { interfaces = new Class<?>[types.length + 2]; interfaces[0] = invoker.getInterface(); interfaces[1] = EchoService.class; for (int i = 0; i < types.length; i ++) { interfaces[i + 1] = ReflectUtils.forName(types[i]); } } } if (interfaces == null) { //加入了EchoService接口 interfaces = new Class<?>[] {invoker.getInterface(), EchoService.class}; } //JavassistProxyFactory和JdkProxyFactory分别实现EchoService接口 return getProxy(invoker, interfaces); }
provider在EchoFilter中处理$echo请求:
@Activate(group = Constants.PROVIDER, order = -110000) public class EchoFilter implements Filter { public Result invoke(Invoker<?> invoker, Invocation inv) throws RpcException { if(inv.getMethodName().equals(Constants.$ECHO)
&& inv.getArguments() != null && inv.getArguments().length == 1) return new RpcResult(inv.getArguments()[0]); return invoker.invoke(inv); } }