Skip to content

Commit

Permalink
Merge pull request #2614, follow up for issue #195.
Browse files Browse the repository at this point in the history
  • Loading branch information
beiwei30 authored and chickenlj committed Mar 8, 2019
1 parent 172d694 commit a8b28cf
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,7 @@ private ConsumerModel buildConsumerModel(Map<String, Object> attributes) {

@SuppressWarnings({"unchecked", "rawtypes", "deprecation"})
private T createProxy(Map<String, String> 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()) {
Expand Down Expand Up @@ -396,14 +383,7 @@ private T createProxy(Map<String, String> 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());
Expand All @@ -424,6 +404,55 @@ private T createProxy(Map<String, String> 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<String, String> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit a8b28cf

Please sign in to comment.