big fix DUBBO-602 reference的init属性通过dubbo.properties和-D选项来设置时不生效
diff --git a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java
index 903ad13..10ada83 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/com/alibaba/dubbo/config/ReferenceConfig.java
@@ -45,7 +45,6 @@
import com.alibaba.dubbo.rpc.cluster.Cluster;
import com.alibaba.dubbo.rpc.cluster.directory.StaticDirectory;
import com.alibaba.dubbo.rpc.cluster.support.AvailableCluster;
-import com.alibaba.dubbo.rpc.cluster.support.SwitchCluster;
import com.alibaba.dubbo.rpc.cluster.support.ClusterUtils;
import com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol;
import com.alibaba.dubbo.rpc.service.GenericService;
@@ -158,16 +157,16 @@
ref = null;
}
- private void init() {
- if (initialized) {
- return;
- }
- initialized = true;
- if (interfaceName == null || interfaceName.length() == 0) {
- throw new IllegalStateException("<dubbo:reference interface=\"\" /> interface not allow null!");
- }
- // 获取消费者全局配置
- checkDefault();
+ private volatile Map<String, String> initedParameterMap;
+
+ protected synchronized void checkAndLoadConfig() {
+ if(initedParameterMap != null) return;
+
+ if (interfaceName == null || interfaceName.length() == 0) {
+ throw new IllegalStateException("<dubbo:reference interface=\"\" /> interface not allow null!");
+ }
+ // 获取消费者全局配置
+ checkDefault();
appendProperties(this);
if (getGeneric() == null && getConsumer() != null) {
setGeneric(getConsumer().getGeneric());
@@ -176,50 +175,50 @@
interfaceClass = GenericService.class;
} else {
try {
- interfaceClass = Class.forName(interfaceName, true, Thread.currentThread()
- .getContextClassLoader());
- } catch (ClassNotFoundException e) {
- throw new IllegalStateException(e.getMessage(), e);
- }
+ interfaceClass = Class.forName(interfaceName, true, Thread.currentThread()
+ .getContextClassLoader());
+ } catch (ClassNotFoundException e) {
+ throw new IllegalStateException(e.getMessage(), e);
+ }
checkInterfaceAndMethods(interfaceClass, methods);
}
String resolve = System.getProperty(interfaceName);
String resolveFile = null;
if (resolve == null || resolve.length() == 0) {
- resolveFile = System.getProperty("dubbo.resolve.file");
- if (resolveFile == null || resolveFile.length() == 0) {
- File userResolveFile = new File(new File(System.getProperty("user.home")), "dubbo-resolve.properties");
- if (userResolveFile.exists()) {
- resolveFile = userResolveFile.getAbsolutePath();
- }
- }
- if (resolveFile != null && resolveFile.length() > 0) {
- Properties properties = new Properties();
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(new File(resolveFile));
- properties.load(fis);
- } catch (IOException e) {
- throw new IllegalStateException("Unload " + resolveFile + ", cause: " + e.getMessage(), e);
- } finally {
- try {
+ resolveFile = System.getProperty("dubbo.resolve.file");
+ if (resolveFile == null || resolveFile.length() == 0) {
+ File userResolveFile = new File(new File(System.getProperty("user.home")), "dubbo-resolve.properties");
+ if (userResolveFile.exists()) {
+ resolveFile = userResolveFile.getAbsolutePath();
+ }
+ }
+ if (resolveFile != null && resolveFile.length() > 0) {
+ Properties properties = new Properties();
+ FileInputStream fis = null;
+ try {
+ fis = new FileInputStream(new File(resolveFile));
+ properties.load(fis);
+ } catch (IOException e) {
+ throw new IllegalStateException("Unload " + resolveFile + ", cause: " + e.getMessage(), e);
+ } finally {
+ try {
if(null != fis) fis.close();
} catch (IOException e) {
logger.warn(e.getMessage(), e);
}
- }
- resolve = properties.getProperty(interfaceName);
- }
+ }
+ resolve = properties.getProperty(interfaceName);
+ }
}
if (resolve != null && resolve.length() > 0) {
- url = resolve;
- if (logger.isWarnEnabled()) {
- if (resolveFile != null && resolveFile.length() > 0) {
- logger.warn("Using default dubbo resolve file " + resolveFile + " replace " + interfaceName + "" + resolve + " to p2p invoke remote service.");
- } else {
- logger.warn("Using -D" + interfaceName + "=" + resolve + " to p2p invoke remote service.");
- }
- }
+ url = resolve;
+ if (logger.isWarnEnabled()) {
+ if (resolveFile != null && resolveFile.length() > 0) {
+ logger.warn("Using default dubbo resolve file " + resolveFile + " replace " + interfaceName + "" + resolve + " to p2p invoke remote service.");
+ } else {
+ logger.warn("Using -D" + interfaceName + "=" + resolve + " to p2p invoke remote service.");
+ }
+ }
}
if (consumer != null) {
if (application == null) {
@@ -290,7 +289,18 @@
}
//attributes通过系统context进行存储.
StaticContext.getSystemContext().putAll(attributes);
- ref = createProxy(map);
+
+ initedParameterMap = map;
+ }
+
+ private void init() {
+ if (initialized) {
+ return;
+ }
+ initialized = true;
+
+ checkAndLoadConfig();
+ ref = createProxy();
}
private static void checkAndConvertImplicitConfig(MethodConfig method, Map<String,String> map, Map<Object,Object> attributes){
@@ -327,8 +337,8 @@
}
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
- private T createProxy(Map<String, String> map) {
- URL tmpUrl = new URL("temp", "localhost", 0, map);
+ private T createProxy() {
+ URL tmpUrl = new URL("temp", "localhost", 0, initedParameterMap);
final boolean isJvmRefer;
if (isInjvm() == null) {
if (url != null && url.length() > 0) { //指定URL的情况下,不做本地引用
@@ -344,7 +354,7 @@
}
if (isJvmRefer) {
- URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(map);
+ URL url = new URL(Constants.LOCAL_PROTOCOL, NetUtils.LOCALHOST, 0, interfaceClass.getName()).addParameters(initedParameterMap);
invoker = refprotocol.refer(interfaceClass, url);
if (logger.isInfoEnabled()) {
logger.info("Using injvm service " + interfaceClass.getName());
@@ -359,9 +369,9 @@
url = url.setPath(interfaceName);
}
if (Constants.REGISTRY_PROTOCOL.equals(url.getProtocol())) {
- urls.add(url.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(map)));
+ urls.add(url.addParameterAndEncoded(Constants.REFER_KEY, StringUtils.toQueryString(initedParameterMap)));
} else {
- urls.add(ClusterUtils.mergeUrl(url, map));
+ urls.add(ClusterUtils.mergeUrl(url, initedParameterMap));
}
}
}
@@ -371,9 +381,9 @@
for (URL u : us) {
URL monitorUrl = loadMonitor(u);
if (monitorUrl != null) {
- map.put(Constants.MONITOR_KEY, URL.encode(monitorUrl.toFullString()));
+ initedParameterMap.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(initedParameterMap)));
}
}
if (urls == null || urls.size() == 0) {
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/ReferenceBean.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/ReferenceBean.java
index d149448..a958a24 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/ReferenceBean.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/ReferenceBean.java
@@ -164,6 +164,8 @@
}
}
}
+
+ checkAndLoadConfig();
Boolean b = isInit();
if (b == null && getConsumer() != null) {
b = getConsumer().isInit();