@Override public URL getUrl(){ return configuratorUrl; }
3.1.2 configure
1: @Override 2: public URL configure(URL url){ 3: if (configuratorUrl.getHost() == null || url == null || url.getHost() == null) { 4: return url; 5: } 6: // If override url has port, means it is a provider address. We want to control a specific provider with this override url, it may take effect on the specific provider instance or on consumers holding this provider instance. 7: // 配置规则,URL 带有端口( port ),意图是控制提供者机器。可以在提供端生效 也可以在消费端生效 8: if (configuratorUrl.getPort() != 0) { 9: if (url.getPort() == configuratorUrl.getPort()) { 10: return configureIfMatch(url.getHost(), url); 11: } 12: // override url don't have a port, means the ip override url specify is a consumer address or 0.0.0.0 13: // 配置规则,URL 没有端口,override 输入消费端地址 或者 0.0.0.0 14: } else { 15: // 1.If it is a consumer ip address, the intention is to control a specific consumer instance, it must takes effect at the consumer side, any provider received this override url should ignore; 16: // 2.If the ip is 0.0.0.0, this override url can be used on consumer, and also can be used on provider 17: // 1. 如果是消费端地址,则意图是控制消费者机器,必定在消费端生效,提供端忽略; 18: // 2. 如果是0.0.0.0可能是控制提供端,也可能是控制提供端 19: if (url.getParameter(Constants.SIDE_KEY, Constants.PROVIDER).equals(Constants.CONSUMER)) { 20: // NetUtils.getLocalHost是消费端注册到zk的消费者地址 21: return configureIfMatch(NetUtils.getLocalHost(), url);// NetUtils.getLocalHost is the ip address consumer registered to registry. 22: } elseif (url.getParameter(Constants.SIDE_KEY, Constants.CONSUMER).equals(Constants.PROVIDER)) { 23: // 控制所有提供端,地址必定是0.0.0.0,否则就要配端口从而执行上面的if分支了 24: return configureIfMatch(Constants.ANYHOST_VALUE, url);// take effect on all providers, so address must be 0.0.0.0, otherwise it won't flow to this if branch 25: } 26: } 27: return url; 28: }
我们可以看到,【第 6 至 26 行】一共有三种情况的判断:
【第一种】第 8 行: configuratorUrl 带有端口( port ),意图是匹配指定一个服务提供者,因此使用 url.host 属性。
【第二种】第 19 行:url 的 side = consumer ,意图是匹配服务消费者,因此使用 NetUtils#getLocalHost() 属性。
1: @Override 2: public <T> Exporter<T> export(final Invoker<T> originInvoker)throws RpcException { 3: // 暴露服务 4: // export invoker 5: final ExporterChangeableWrapper<T> exporter = doLocalExport(originInvoker); 6: 7: // 获得注册中心 URL 8: URL registryUrl = getRegistryUrl(originInvoker); 9: 10: // 获得注册中心对象 11: // registry provider 12: final Registry registry = getRegistry(originInvoker); 13: 14: // 获得服务提供者 URL 15: final URL registedProviderUrl = getRegistedProviderUrl(originInvoker); 16: 17: //to judge to delay publish whether or not 18: boolean register = registedProviderUrl.getParameter("register", true); 19: 20: // 向注册中心订阅服务消费者 21: ProviderConsumerRegTable.registerProvider(originInvoker, registryUrl, registedProviderUrl); 22: 23: // 向注册中心注册服务提供者(自己) 24: if (register) { 25: register(registryUrl, registedProviderUrl); 26: ProviderConsumerRegTable.getProviderWrapper(originInvoker).setReg(true); // // 标记向本地注册表的注册服务提供者,已经注册 27: } 28: 29: // 使用 OverrideListener 对象,订阅配置规则 30: // Subscribe the override data 31: // FIXME When the provider subscribes, it will affect the scene : a certain JVM exposes the service and call the same service. Because the subscribed is cached key with the name of the service, it causes the subscription information to cover. 32: // 创建订阅配置规则的 URL 33: final URL overrideSubscribeUrl = getSubscribedOverrideUrl(registedProviderUrl); 34: // 创建 OverrideListener 对象,并添加到 `overrideListeners` 中 35: final OverrideListener overrideSubscribeListener = new OverrideListener(overrideSubscribeUrl, originInvoker); 36: overrideListeners.put(overrideSubscribeUrl, overrideSubscribeListener); 37: // 向注册中心,发起订阅 38: registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener); 39: //Ensure that a new exporter instance is returned every time export 40: returnnew DestroyableExporter<T>(exporter, originInvoker, overrideSubscribeUrl, registedProviderUrl); 41: }
private List<URL> getMatchedUrls(List<URL> configuratorUrls, URL currentSubscribe){ List<URL> result = new ArrayList<URL>(); for (URL url : configuratorUrls) { URL overrideUrl = url; // 【忽略】,兼容老版本 // Compatible with the old version if (url.getParameter(Constants.CATEGORY_KEY) == null && Constants.OVERRIDE_PROTOCOL.equals(url.getProtocol())) { overrideUrl = url.addParameter(Constants.CATEGORY_KEY, Constants.CONFIGURATORS_CATEGORY); } // 判断是否匹配 // Check whether url is to be applied to the current service if (UrlUtils.isMatch(currentSubscribe, overrideUrl)) { result.add(url); } } return result; }