diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java index b4a732e1eb0..cf490ed4079 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractReferenceConfig.java @@ -36,12 +36,12 @@ public abstract class AbstractReferenceConfig extends AbstractInterfaceConfig { /** * Check if service provider exists, if not exists, it will be fast fail */ - protected Boolean check = true; + protected Boolean check; /** * Whether to eagle-init */ - protected Boolean init = false; + protected Boolean init; /** * Whether to use generic interface @@ -51,7 +51,7 @@ public abstract class AbstractReferenceConfig extends AbstractInterfaceConfig { /** * Whether to find reference's instance from the current JVM */ - protected Boolean injvm = false; + protected Boolean injvm; /** * Lazy create connection diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java index ac276447b4c..ba7f129355f 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/AbstractServiceConfig.java @@ -57,7 +57,7 @@ public abstract class AbstractServiceConfig extends AbstractInterfaceConfig { /** * Whether to export the service */ - protected Boolean export = true; + protected Boolean export; /** * The service weight 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 ceacdb2863b..a7bbf13b67f 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 @@ -322,20 +322,7 @@ private ConsumerModel buildConsumerModel(Map attributes) { @SuppressWarnings({"unchecked", "rawtypes", "deprecation"}) private T createProxy(Map map) { - URL tmpUrl = new URL("temp", "localhost", 0, map); - final boolean isJvmRefer; - if (isInjvm() == null) { - if (url != null && url.length() > 0) { // if a url is specified, don't do local reference - isJvmRefer = false; - } else { - // by default, reference local service if there is - isJvmRefer = InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl); - } - } else { - isJvmRefer = isInjvm(); - } - - if (isJvmRefer) { + if (shouldJvmRefer(map)) { URL url = new URL(Constants.LOCAL_PROTOCOL, Constants.LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map); invoker = refprotocol.refer(interfaceClass, url); if (logger.isInfoEnabled()) { @@ -396,14 +383,7 @@ private T createProxy(Map map) { } } - Boolean c = check; - if (c == null && consumer != null) { - c = consumer.isCheck(); - } - if (c == null) { - c = true; // default true - } - if (c && !invoker.isAvailable()) { + if (shouldCheck() && !invoker.isAvailable()) { // make it possible for consumer to retry later if provider is temporarily unavailable initialized = false; throw new IllegalStateException("Failed to check the status of the service " + interfaceName + ". No provider available for the service " + (group == null ? "" : group + "/") + interfaceName + (version == null ? "" : ":" + version) + " from the url " + invoker.getUrl() + " to the consumer " + NetUtils.getLocalHost() + " use dubbo version " + Version.getVersion()); @@ -424,6 +404,55 @@ private T createProxy(Map map) { return (T) proxyFactory.getProxy(invoker); } + /** + * Figure out should refer the service in the same JVM from configurations. The default behavior is true + * 1. if injvm is specified, then use it + * 2. then if a url is specified, then assume it's a remote call + * 3. otherwise, check scope parameter + * 4. if scope is not specified but the target service is provided in the same JVM, then prefer to make the local + * call, which is the default behavior + */ + protected boolean shouldJvmRefer(Map map) { + URL tmpUrl = new URL("temp", "localhost", 0, map); + boolean isJvmRefer; + if (isInjvm() == null) { + // if a url is specified, don't do local reference + if (url != null && url.length() > 0) { + isJvmRefer = false; + } else { + // by default, reference local service if there is + isJvmRefer = InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl); + } + } else { + isJvmRefer = isInjvm(); + } + return isJvmRefer; + } + + protected boolean shouldCheck() { + Boolean shouldCheck = isCheck(); + if (shouldCheck == null && getConsumer()!= null) { + shouldCheck = getConsumer().isCheck(); + } + if (shouldCheck == null) { + // default true + shouldCheck = true; + } + return shouldCheck; + } + + protected boolean shouldInit() { + Boolean shouldInit = isInit(); + if (shouldInit == null && getConsumer() != null) { + shouldInit = getConsumer().isInit(); + } + if (shouldInit == null) { + // default is false + return false; + } + return shouldInit; + } + private void checkDefault() { createConsumerIfAbsent(); } 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 45435651a9b..7cca8ccfbd2 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 @@ -325,25 +325,39 @@ public void checkAndUpdateSubConfigs() { public synchronized void export() { checkAndUpdateSubConfigs(); - if (provider != null) { - if (export == null) { - export = provider.getExport(); - } - if (delay == null) { - delay = provider.getDelay(); - } - } - if (export != null && !export) { + if (!shouldExport()) { return; } - if (delay != null && delay > 0) { + if (shouldDelay()) { delayExportExecutor.schedule(this::doExport, delay, TimeUnit.MILLISECONDS); } else { doExport(); } } + private boolean shouldExport() { + Boolean shouldExport = getExport(); + if (shouldExport == null && provider != null) { + shouldExport = provider.getExport(); + } + + // default value is true + if (shouldExport == null) { + return true; + } + + return shouldExport; + } + + private boolean shouldDelay() { + Integer delay = getDelay(); + if (delay == null && provider != null) { + delay = provider.getDelay(); + } + return delay != null && delay > 0; + } + protected synchronized void doExport() { if (unexported) { throw new IllegalStateException("The service " + interfaceClass.getName() + " has already unexported!"); diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java index 63e2cd5f1d1..9aaea67fc66 100644 --- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java +++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java @@ -71,9 +71,9 @@ boolean generic() default false; /** - * When enable, prefer to call local service in the same JVM if it's present, default value is false + * When enable, prefer to call local service in the same JVM if it's present, default value is true */ - boolean injvm() default false; + boolean injvm() default true; /** * Check if service provider is available during boot up, default value is true diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java index dc3d253a937..d2beb2f76a9 100644 --- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java +++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java @@ -134,7 +134,6 @@ public void testExport() throws Exception { assertThat(url.getParameters(), hasKey(Constants.BIND_IP_KEY)); assertThat(url.getParameters(), hasKey(Constants.BIND_PORT_KEY)); assertThat(url.getParameters(), hasEntry(Constants.DEFAULT_KEY + "." + Constants.EXPORT_KEY, "true")); - assertThat(url.getParameters(), hasEntry(Constants.EXPORT_KEY, "true")); assertThat(url.getParameters(), hasEntry("echo.0.callback", "false")); assertThat(url.getParameters(), hasEntry(Constants.GENERIC_KEY, "false")); assertThat(url.getParameters(), hasEntry(Constants.INTERFACE_KEY, DemoService.class.getName())); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java index 5e8cab22eae..3daec132ac5 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java @@ -216,11 +216,7 @@ public void afterPropertiesSet() throws Exception { } } - Boolean b = isInit(); - if (b == null && getConsumer() != null) { - b = getConsumer().isInit(); - } - if (b != null && b) { + if (shouldInit()) { getObject(); } }