1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| public abstract class AbstractDirectory<T> implements Directory<T> {
// 日志输出 private static final Logger logger = LoggerFactory.getLogger(AbstractDirectory.class); //服务url private final URL url ; private volatile boolean destroyed = false; //消费者url private volatile URL consumerUrl ; //路由 private volatile List<Router> routers; public AbstractDirectory(URL url) { this(url, null); } public AbstractDirectory(URL url, List<Router> routers) { this(url, url, routers); } public AbstractDirectory(URL url, URL consumerUrl, List<Router> routers) { if (url == null) throw new IllegalArgumentException("url == null"); this.url = url; this.consumerUrl = consumerUrl; setRouters(routers); } //对list方法的默认实现 public List<Invoker<T>> list(Invocation invocation) throws RpcException { if (destroyed){ throw new RpcException("Directory already destroyed .url: "+ getUrl()); } //获取Invoker列表的具体实现由具体子类实现 List<Invoker<T>> invokers = doList(invocation); //路由 List<Router> localRouters = this.routers; // local reference if (localRouters != null && localRouters.size() > 0) { for (Router router: localRouters){ try { if (router.getUrl() == null || router.getUrl().getParameter(Constants.RUNTIME_KEY, true)) { //路由 invokers = router.route(invokers, getConsumerUrl(), invocation); } } catch (Throwable t) { logger.error("Failed to execute router: " + getUrl() + ", cause: " + t.getMessage(), t); } } } return invokers; } public URL getUrl() { return url; } public List<Router> getRouters(){ return routers; }
public URL getConsumerUrl() { return consumerUrl; }
public void setConsumerUrl(URL consumerUrl) { this.consumerUrl = consumerUrl; } //构造中调用的设置路由的方法 protected void setRouters(List<Router> routers){ // copy list routers = routers == null ? new ArrayList<Router>() : new ArrayList<Router>(routers); // append url router String routerkey = url.getParameter(Constants.ROUTER_KEY); //指定了router,就使用制定的router来获取扩展实现 if (routerkey != null && routerkey.length() > 0) { RouterFactory routerFactory = ExtensionLoader.getExtensionLoader(RouterFactory.class).getExtension(routerkey); routers.add(routerFactory.getRouter(url)); } // append mock invoker selector routers.add(new MockInvokersSelector()); Collections.sort(routers); this.routers = routers; }
public boolean isDestroyed() { return destroyed; }
public void destroy(){ destroyed = true; } //子类实现具体的获取invoker列表 protected abstract List<Invoker<T>> doList(Invocation invocation) throws RpcException ;
}
|