mercyblitz 6 years ago
parent 4b111e4e46
commit e32b020198

@ -55,7 +55,7 @@ public class SpringCloudRegistry extends AbstractSpringCloudRegistry {
/** /**
* The parameter name of the services of Dubbo Provider * The parameter name of the services of Dubbo Provider
*/ */
public static final String DUBBO_PROVIDER_SERVICES_PARAM_NAME = "dubbo.provider-services"; public static final String DUBBO_PROVIDER_SERVICES_PARAM_NAME = "dubbo-provider-services";
/** /**
* All services of Dubbo Provider * All services of Dubbo Provider

@ -26,6 +26,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.HashMap; import java.util.HashMap;
@ -80,7 +81,17 @@ public class DubboNonWebApplicationEnvironmentPostProcessor implements Environme
return; return;
} }
resetServerPort(environment); MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> defaultProperties = createDefaultProperties(environment);
if (!CollectionUtils.isEmpty(defaultProperties)) {
addOrReplace(propertySources, defaultProperties);
}
}
private Map<String, Object> createDefaultProperties(ConfigurableEnvironment environment) {
Map<String, Object> defaultProperties = new HashMap<String, Object>();
resetServerPort(environment, defaultProperties);
return defaultProperties;
} }
/** /**
@ -88,8 +99,9 @@ public class DubboNonWebApplicationEnvironmentPostProcessor implements Environme
* or "dubbo.protcols.rest.port" * or "dubbo.protcols.rest.port"
* *
* @param environment * @param environment
* @param defaultProperties
*/ */
private void resetServerPort(ConfigurableEnvironment environment) { private void resetServerPort(ConfigurableEnvironment environment, Map<String, Object> defaultProperties) {
String serverPort = environment.getProperty(SERVER_PORT_PROPERTY_NAME, environment.getProperty(PORT_PROPERTY_NAME)); String serverPort = environment.getProperty(SERVER_PORT_PROPERTY_NAME, environment.getProperty(PORT_PROPERTY_NAME));
@ -103,8 +115,7 @@ public class DubboNonWebApplicationEnvironmentPostProcessor implements Environme
serverPort = getRestPortFromProtocolsProperties(environment); serverPort = getRestPortFromProtocolsProperties(environment);
} }
setServerPort(environment, serverPort); setServerPort(environment, serverPort, defaultProperties);
} }
private String getRestPortFromProtocolProperty(ConfigurableEnvironment environment) { private String getRestPortFromProtocolProperty(ConfigurableEnvironment environment) {
@ -147,17 +158,14 @@ public class DubboNonWebApplicationEnvironmentPostProcessor implements Environme
return index > -1 ? propertyName.substring(0, index) : null; return index > -1 ? propertyName.substring(0, index) : null;
} }
private void setServerPort(ConfigurableEnvironment environment, String serverPort) { private void setServerPort(ConfigurableEnvironment environment, String serverPort,
Map<String, Object> defaultProperties) {
if (serverPort == null) { if (serverPort == null) {
return; return;
} }
MutablePropertySources propertySources = environment.getPropertySources(); defaultProperties.put(SERVER_PORT_PROPERTY_NAME, serverPort);
Map<String, Object> properties = new HashMap<>();
properties.put(SERVER_PORT_PROPERTY_NAME, String.valueOf(serverPort));
addOrReplace(propertySources, properties);
} }
/** /**

@ -1,7 +1,9 @@
dubbo: dubbo:
registry: registry:
# The Spring Cloud Dubbo's registry extension # The Spring Cloud Dubbo's registry extension
address: spring-cloud://localhost ## the default value of dubbo-provider-services is "*", that means to subscribe all providers,
## thus it's optimized if subscriber specifies the required providers.
address: spring-cloud://localhost?dubbo-provider-services=${provider.application.name}
# The traditional Dubbo's registry # The traditional Dubbo's registry
# address: zookeeper://127.0.0.1:2181 # address: zookeeper://127.0.0.1:2181
server: server:

Loading…
Cancel
Save