Skip to content

Commit d11f204

Browse files
Leishunyubeiwei30
authored andcommitted
fixed injvm export and refer (#3857)
* fix injvm export and refer * Add default injvm protocol is only injvm protocol don't register * Multiple protocol registry filter injvm * isOnlyJvm
1 parent ceb930e commit d11f204

File tree

2 files changed

+55
-35
lines changed

2 files changed

+55
-35
lines changed

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,19 +354,22 @@ private T createProxy(Map<String, String> map) {
354354
}
355355
}
356356
} else { // assemble URL from register center's configuration
357-
checkRegistry();
358-
List<URL> us = loadRegistries(false);
359-
if (CollectionUtils.isNotEmpty(us)) {
360-
for (URL u : us) {
361-
URL monitorUrl = loadMonitor(u);
362-
if (monitorUrl != null) {
363-
map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
357+
// if protocols not injvm checkRegistry
358+
if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(getProtocol())){
359+
checkRegistry();
360+
List<URL> us = loadRegistries(false);
361+
if (CollectionUtils.isNotEmpty(us)) {
362+
for (URL u : us) {
363+
URL monitorUrl = loadMonitor(u);
364+
if (monitorUrl != null) {
365+
map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
366+
}
367+
urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
364368
}
365-
urls.add(u.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
366369
}
367-
}
368-
if (urls.isEmpty()) {
369-
throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config <dubbo:registry address=\"...\" /> to your spring config.");
370+
if (urls.isEmpty()) {
371+
throw new IllegalStateException("No such any registry to reference " + interfaceName + " on the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion() + ", please config <dubbo:registry address=\"...\" /> to your spring config.");
372+
}
370373
}
371374
}
372375

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,12 @@ public void checkAndUpdateSubConfigs() {
269269
// Config Center should always being started first.
270270
startConfigCenter();
271271
checkDefault();
272-
checkApplication();
273-
checkRegistry();
274272
checkProtocol();
273+
checkApplication();
274+
// if protocol is not injvm checkRegistry
275+
if (!isOnlyInJvm()) {
276+
checkRegistry();
277+
}
275278
this.refresh();
276279
checkMetadataReport();
277280

@@ -327,6 +330,15 @@ public void checkAndUpdateSubConfigs() {
327330
checkMock(interfaceClass);
328331
}
329332

333+
/**
334+
* Determine if it is injvm
335+
*
336+
* @return
337+
*/
338+
private boolean isOnlyInJvm() {
339+
return getProtocols().size() == 1 && Constants.LOCAL_PROTOCOL.equalsIgnoreCase(getProtocols().get(0).getName());
340+
}
341+
330342
public synchronized void export() {
331343
checkAndUpdateSubConfigs();
332344

@@ -539,11 +551,15 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
539551
}
540552
// export to remote if the config is not local (export to local only when config is local)
541553
if (!Constants.SCOPE_LOCAL.equalsIgnoreCase(scope)) {
542-
if (logger.isInfoEnabled()) {
554+
if (!isOnlyInJvm() && logger.isInfoEnabled()) {
543555
logger.info("Export dubbo service " + interfaceClass.getName() + " to url " + url);
544556
}
545557
if (CollectionUtils.isNotEmpty(registryURLs)) {
546558
for (URL registryURL : registryURLs) {
559+
//if protocol is only injvm ,not register
560+
if (Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
561+
continue;
562+
}
547563
url = url.addParameterIfAbsent(Constants.DYNAMIC_KEY, registryURL.getParameter(Constants.DYNAMIC_KEY));
548564
URL monitorUrl = loadMonitor(registryURL);
549565
if (monitorUrl != null) {
@@ -586,18 +602,19 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
586602
}
587603

588604
@SuppressWarnings({"unchecked", "rawtypes"})
605+
/**
606+
* always export injvm
607+
*/
589608
private void exportLocal(URL url) {
590-
if (!Constants.LOCAL_PROTOCOL.equalsIgnoreCase(url.getProtocol())) {
591-
URL local = URLBuilder.from(url)
592-
.setProtocol(Constants.LOCAL_PROTOCOL)
593-
.setHost(LOCALHOST_VALUE)
594-
.setPort(0)
595-
.build();
596-
Exporter<?> exporter = protocol.export(
597-
proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
598-
exporters.add(exporter);
599-
logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry");
600-
}
609+
URL local = URLBuilder.from(url)
610+
.setProtocol(Constants.LOCAL_PROTOCOL)
611+
.setHost(LOCALHOST_VALUE)
612+
.setPort(0)
613+
.build();
614+
Exporter<?> exporter = protocol.export(
615+
proxyFactory.getInvoker(ref, (Class) interfaceClass, local));
616+
exporters.add(exporter);
617+
logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry url : " + local);
601618
}
602619

603620
private Optional<String> getContextPath(ProtocolConfig protocolConfig) {
@@ -803,7 +820,7 @@ private void createProviderIfAbsent() {
803820
if (provider != null) {
804821
return;
805822
}
806-
setProvider (
823+
setProvider(
807824
ConfigManager.getInstance()
808825
.getDefaultProvider()
809826
.orElseGet(() -> {
@@ -834,15 +851,15 @@ private void convertProtocolIdsToProtocols() {
834851

835852
if (StringUtils.isEmpty(protocolIds)) {
836853
if (CollectionUtils.isEmpty(protocols)) {
837-
setProtocols(
838-
ConfigManager.getInstance().getDefaultProtocols()
839-
.filter(CollectionUtils::isNotEmpty)
840-
.orElseGet(() -> {
841-
ProtocolConfig protocolConfig = new ProtocolConfig();
842-
protocolConfig.refresh();
843-
return new ArrayList<>(Arrays.asList(protocolConfig));
844-
})
845-
);
854+
setProtocols(
855+
ConfigManager.getInstance().getDefaultProtocols()
856+
.filter(CollectionUtils::isNotEmpty)
857+
.orElseGet(() -> {
858+
ProtocolConfig protocolConfig = new ProtocolConfig();
859+
protocolConfig.refresh();
860+
return new ArrayList<>(Arrays.asList(protocolConfig));
861+
})
862+
);
846863
}
847864
} else {
848865
String[] arr = Constants.COMMA_SPLIT_PATTERN.split(protocolIds);

0 commit comments

Comments
 (0)