diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java index 4c379375b6e..5ecae9c0519 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java @@ -354,19 +354,22 @@ private T createProxy(Map map) { } } } else { // assemble URL from register center's configuration - checkRegistry(); - List us = loadRegistries(false); - if (CollectionUtils.isNotEmpty(us)) { - for (URL u : us) { - URL monitorUrl = loadMonitor(u); - if (monitorUrl != null) { - map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString())); + // if protocols not injvm checkRegistry + if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(getProtocol())){ + checkRegistry(); + List us = loadRegistries(false); + if (CollectionUtils.isNotEmpty(us)) { + for (URL u : us) { + URL monitorUrl = loadMonitor(u); + if (monitorUrl != null) { + map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString())); + } + urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map))); } - urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map))); } - } - if (urls.isEmpty()) { - throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config to your spring config."); + if (urls.isEmpty()) { + throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config to your spring config."); + } } } diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java index 0c38b2f135c..86c5ed45f9a 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java @@ -269,9 +269,12 @@ public void checkAndUpdateSubConfigs() { // Config Center should always being started first. startConfigCenter(); checkDefault(); - checkApplication(); - checkRegistry(); checkProtocol(); + checkApplication(); + // if protocol is not injvm checkRegistry + if (!isOnlyInJvm()) { + checkRegistry(); + } this.refresh(); checkMetadataReport(); @@ -327,6 +330,15 @@ public void checkAndUpdateSubConfigs() { checkMock(interfaceClass); } + /** + * Determine if it is injvm + * + * @return + */ + private boolean isOnlyInJvm() { + return getProtocols().size() == 1 && Constants.LOCAL_PROTOCOL.equalsIgnoreCase(getProtocols().get(0).getName()); + } + public synchronized void export() { checkAndUpdateSubConfigs(); @@ -539,11 +551,15 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r } // export to remote if the config is not local (export to local only when config is local) if (!Constants.SCOPE_LOCAL.equalsIgnoreCase(scope)) { - if (logger.isInfoEnabled()) { + if (!isOnlyInJvm() && logger.isInfoEnabled()) { logger.info("Export dubbo service " + interfaceClass.getName() + " to url " + url); } if (CollectionUtils.isNotEmpty(registryURLs)) { for (URL registryURL : registryURLs) { + //if protocol is only injvm ,not register + if (Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) { + continue; + } url = url.addParameterIfAbsent(Constants.DYNAMIC_KEY, registryURL.getParameter(Constants.DYNAMIC_KEY)); URL monitorUrl = loadMonitor(registryURL); if (monitorUrl != null) { @@ -586,18 +602,19 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List r } @SuppressWarnings({"unchecked", "rawtypes"}) + /** + * always export injvm + */ private void exportLocal(URL url) { - if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) { - URL local = URLBuilder.from(url) - .setProtocol(Constants.LOCAL_PROTOCOL) - .setHost(LOCALHOST_VALUE) - .setPort(0) - .build(); - Exporter exporter = protocol.export( - proxyFactory.getInvoker(ref, (Class) interfaceClass, local)); - exporters.add(exporter); - logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry"); - } + URL local = URLBuilder.from(url) + .setProtocol(Constants.LOCAL_PROTOCOL) + .setHost(LOCALHOST_VALUE) + .setPort(0) + .build(); + Exporter exporter = protocol.export( + proxyFactory.getInvoker(ref, (Class) interfaceClass, local)); + exporters.add(exporter); + logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry url : " + local); } private Optional getContextPath(ProtocolConfig protocolConfig) { @@ -803,7 +820,7 @@ private void createProviderIfAbsent() { if (provider != null) { return; } - setProvider ( + setProvider( ConfigManager.getInstance() .getDefaultProvider() .orElseGet(() -> { @@ -834,15 +851,15 @@ private void convertProtocolIdsToProtocols() { if (StringUtils.isEmpty(protocolIds)) { if (CollectionUtils.isEmpty(protocols)) { - setProtocols( - ConfigManager.getInstance().getDefaultProtocols() - .filter(CollectionUtils::isNotEmpty) - .orElseGet(() -> { - ProtocolConfig protocolConfig = new ProtocolConfig(); - protocolConfig.refresh(); - return new ArrayList<>(Arrays.asList(protocolConfig)); - }) - ); + setProtocols( + ConfigManager.getInstance().getDefaultProtocols() + .filter(CollectionUtils::isNotEmpty) + .orElseGet(() -> { + ProtocolConfig protocolConfig = new ProtocolConfig(); + protocolConfig.refresh(); + return new ArrayList<>(Arrays.asList(protocolConfig)); + }) + ); } } else { String[] arr = Constants.COMMA_SPLIT_PATTERN.split(protocolIds);