Skip to content

Commit a8b28cf

Browse files
beiwei30chickenlj
authored andcommitted
Merge pull request #2614, follow up for issue #195.
1 parent 172d694 commit a8b28cf

File tree

7 files changed

+82
-44
lines changed

7 files changed

+82
-44
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public abstract class AbstractReferenceConfig extends AbstractInterfaceConfig {
3636
/**
3737
* Check if service provider exists, if not exists, it will be fast fail
3838
*/
39-
protected Boolean check = true;
39+
protected Boolean check;
4040

4141
/**
4242
* Whether to eagle-init
4343
*/
44-
protected Boolean init = false;
44+
protected Boolean init;
4545

4646
/**
4747
* Whether to use generic interface
@@ -51,7 +51,7 @@ public abstract class AbstractReferenceConfig extends AbstractInterfaceConfig {
5151
/**
5252
* Whether to find reference's instance from the current JVM
5353
*/
54-
protected Boolean injvm = false;
54+
protected Boolean injvm;
5555

5656
/**
5757
* Lazy create connection

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public abstract class AbstractServiceConfig extends AbstractInterfaceConfig {
5757
/**
5858
* Whether to export the service
5959
*/
60-
protected Boolean export = true;
60+
protected Boolean export;
6161

6262
/**
6363
* The service weight

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

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,7 @@ private ConsumerModel buildConsumerModel(Map<String, Object> attributes) {
322322

323323
@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
324324
private T createProxy(Map<String, String> map) {
325-
URL tmpUrl = new URL("temp", "localhost", 0, map);
326-
final boolean isJvmRefer;
327-
if (isInjvm() == null) {
328-
if (url != null && url.length() > 0) { // if a url is specified, don't do local reference
329-
isJvmRefer = false;
330-
} else {
331-
// by default, reference local service if there is
332-
isJvmRefer = InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl);
333-
}
334-
} else {
335-
isJvmRefer = isInjvm();
336-
}
337-
338-
if (isJvmRefer) {
325+
if (shouldJvmRefer(map)) {
339326
URL url = new URL(Constants.LOCAL_PROTOCOL, Constants.LOCALHOST_VALUE, 0, interfaceClass.getName()).addParameters(map);
340327
invoker = refprotocol.refer(interfaceClass, url);
341328
if (logger.isInfoEnabled()) {
@@ -396,14 +383,7 @@ private T createProxy(Map<String, String> map) {
396383
}
397384
}
398385

399-
Boolean c = check;
400-
if (c == null && consumer != null) {
401-
c = consumer.isCheck();
402-
}
403-
if (c == null) {
404-
c = true; // default true
405-
}
406-
if (c && !invoker.isAvailable()) {
386+
if (shouldCheck() && !invoker.isAvailable()) {
407387
// make it possible for consumer to retry later if provider is temporarily unavailable
408388
initialized = false;
409389
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<String, String> map) {
424404
return (T) proxyFactory.getProxy(invoker);
425405
}
426406

407+
/**
408+
* Figure out should refer the service in the same JVM from configurations. The default behavior is true
409+
* 1. if injvm is specified, then use it
410+
* 2. then if a url is specified, then assume it's a remote call
411+
* 3. otherwise, check scope parameter
412+
* 4. if scope is not specified but the target service is provided in the same JVM, then prefer to make the local
413+
* call, which is the default behavior
414+
*/
415+
protected boolean shouldJvmRefer(Map<String, String> map) {
416+
URL tmpUrl = new URL("temp", "localhost", 0, map);
417+
boolean isJvmRefer;
418+
if (isInjvm() == null) {
419+
// if a url is specified, don't do local reference
420+
if (url != null && url.length() > 0) {
421+
isJvmRefer = false;
422+
} else {
423+
// by default, reference local service if there is
424+
isJvmRefer = InjvmProtocol.getInjvmProtocol().isInjvmRefer(tmpUrl);
425+
}
426+
} else {
427+
isJvmRefer = isInjvm();
428+
}
429+
return isJvmRefer;
430+
}
431+
432+
protected boolean shouldCheck() {
433+
Boolean shouldCheck = isCheck();
434+
if (shouldCheck == null && getConsumer()!= null) {
435+
shouldCheck = getConsumer().isCheck();
436+
}
437+
if (shouldCheck == null) {
438+
// default true
439+
shouldCheck = true;
440+
}
441+
return shouldCheck;
442+
}
443+
444+
protected boolean shouldInit() {
445+
Boolean shouldInit = isInit();
446+
if (shouldInit == null && getConsumer() != null) {
447+
shouldInit = getConsumer().isInit();
448+
}
449+
if (shouldInit == null) {
450+
// default is false
451+
return false;
452+
}
453+
return shouldInit;
454+
}
455+
427456
private void checkDefault() {
428457
createConsumerIfAbsent();
429458
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,39 @@ public void checkAndUpdateSubConfigs() {
325325
public synchronized void export() {
326326
checkAndUpdateSubConfigs();
327327

328-
if (provider != null) {
329-
if (export == null) {
330-
export = provider.getExport();
331-
}
332-
if (delay == null) {
333-
delay = provider.getDelay();
334-
}
335-
}
336-
if (export != null && !export) {
328+
if (!shouldExport()) {
337329
return;
338330
}
339331

340-
if (delay != null && delay > 0) {
332+
if (shouldDelay()) {
341333
delayExportExecutor.schedule(this::doExport, delay, TimeUnit.MILLISECONDS);
342334
} else {
343335
doExport();
344336
}
345337
}
346338

339+
private boolean shouldExport() {
340+
Boolean shouldExport = getExport();
341+
if (shouldExport == null && provider != null) {
342+
shouldExport = provider.getExport();
343+
}
344+
345+
// default value is true
346+
if (shouldExport == null) {
347+
return true;
348+
}
349+
350+
return shouldExport;
351+
}
352+
353+
private boolean shouldDelay() {
354+
Integer delay = getDelay();
355+
if (delay == null && provider != null) {
356+
delay = provider.getDelay();
357+
}
358+
return delay != null && delay > 0;
359+
}
360+
347361
protected synchronized void doExport() {
348362
if (unexported) {
349363
throw new IllegalStateException("The service " + interfaceClass.getName() + " has already unexported!");

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/annotation/Reference.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
boolean generic() default false;
7272

7373
/**
74-
* When enable, prefer to call local service in the same JVM if it's present, default value is false
74+
* When enable, prefer to call local service in the same JVM if it's present, default value is true
7575
*/
76-
boolean injvm() default false;
76+
boolean injvm() default true;
7777

7878
/**
7979
* Check if service provider is available during boot up, default value is true

dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/ServiceConfigTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public void testExport() throws Exception {
134134
assertThat(url.getParameters(), hasKey(Constants.BIND_IP_KEY));
135135
assertThat(url.getParameters(), hasKey(Constants.BIND_PORT_KEY));
136136
assertThat(url.getParameters(), hasEntry(Constants.DEFAULT_KEY + "." + Constants.EXPORT_KEY, "true"));
137-
assertThat(url.getParameters(), hasEntry(Constants.EXPORT_KEY, "true"));
138137
assertThat(url.getParameters(), hasEntry("echo.0.callback", "false"));
139138
assertThat(url.getParameters(), hasEntry(Constants.GENERIC_KEY, "false"));
140139
assertThat(url.getParameters(), hasEntry(Constants.INTERFACE_KEY, DemoService.class.getName()));

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ReferenceBean.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,7 @@ public void afterPropertiesSet() throws Exception {
216216
}
217217
}
218218

219-
Boolean b = isInit();
220-
if (b == null && getConsumer() != null) {
221-
b = getConsumer().isInit();
222-
}
223-
if (b != null && b) {
219+
if (shouldInit()) {
224220
getObject();
225221
}
226222
}

0 commit comments

Comments
 (0)