Skip to content

Commit

Permalink
Polish apache#635 : [Infrastructure] Upgrade Dubbo 2.7.5
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Dec 16, 2019
1 parent f0d68c2 commit e0d0378
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
package org.apache.dubbo.spring.boot.autoconfigure;

import org.apache.dubbo.config.AbstractConfig;
import org.apache.dubbo.config.spring.context.properties.AbstractDubboConfigBinder;
import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.springframework.boot.context.properties.bind.BindHandler;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
Expand All @@ -27,8 +27,12 @@
import org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.UnboundElementsSourceFilter;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;

import java.util.Map;

import static java.util.Arrays.asList;
import static org.springframework.boot.context.properties.source.ConfigurationPropertySources.from;

/**
Expand All @@ -37,35 +41,36 @@
*
* @since 2.7.0
*/
class BinderDubboConfigBinder extends AbstractDubboConfigBinder {
class BinderDubboConfigBinder implements ConfigurationBeanBinder {

@Override
public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) {
public void bind(Map<String, Object> configurationProperties, boolean ignoreUnknownFields,
boolean ignoreInvalidFields, Object configurationBean) {

Iterable<PropertySource<?>> propertySources = getPropertySources();
Iterable<PropertySource<?>> propertySources = asList(new MapPropertySource("internal", configurationProperties));

// Converts ConfigurationPropertySources
Iterable<ConfigurationPropertySource> configurationPropertySources = from(propertySources);

// Wrap Bindable from DubboConfig instance
Bindable<C> bindable = Bindable.ofInstance(dubboConfig);
Bindable bindable = Bindable.ofInstance(configurationBean);

Binder binder = new Binder(configurationPropertySources, new PropertySourcesPlaceholdersResolver(propertySources));

// Get BindHandler
BindHandler bindHandler = getBindHandler();
BindHandler bindHandler = getBindHandler(ignoreUnknownFields, ignoreInvalidFields);

// Bind
binder.bind(prefix, bindable, bindHandler);

binder.bind("", bindable, bindHandler);
}

private BindHandler getBindHandler() {
private BindHandler getBindHandler(boolean ignoreUnknownFields,
boolean ignoreInvalidFields) {
BindHandler handler = BindHandler.DEFAULT;
if (isIgnoreInvalidFields()) {
if (ignoreInvalidFields) {
handler = new IgnoreErrorsBindHandler(handler);
}
if (!isIgnoreUnknownFields()) {
if (!ignoreUnknownFields) {
UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
handler = new NoUnboundElementsBindHandler(handler, filter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
package org.apache.dubbo.spring.boot.autoconfigure;

import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -34,7 +33,7 @@

import java.util.Map;

import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getPrefixedProperties;
import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties;
import static org.apache.dubbo.spring.boot.util.DubboUtils.BASE_PACKAGES_PROPERTY_RESOLVER_BEAN_NAME;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_PREFIX;
import static org.apache.dubbo.spring.boot.util.DubboUtils.DUBBO_SCAN_PREFIX;
Expand All @@ -58,18 +57,18 @@ public PropertyResolver dubboScanBasePackagesPropertyResolver(ConfigurableEnviro
ConfigurableEnvironment propertyResolver = new AbstractEnvironment() {
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
Map<String, Object> dubboScanProperties = getPrefixedProperties(environment.getPropertySources(), DUBBO_SCAN_PREFIX);
Map<String, Object> dubboScanProperties = getSubProperties(environment.getPropertySources(), DUBBO_SCAN_PREFIX);
propertySources.addLast(new MapPropertySource("dubboScanProperties", dubboScanProperties));
}
};
ConfigurationPropertySources.attach(propertyResolver);
return new DelegatingPropertyResolver(propertyResolver);
}

@ConditionalOnMissingBean(name = RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME, value = DubboConfigBinder.class)
@ConditionalOnMissingBean(name = RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME, value = ConfigurationBeanBinder.class)
@Bean(RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME)
@Scope(scopeName = SCOPE_PROTOTYPE)
public DubboConfigBinder relaxedDubboConfigBinder() {
public ConfigurationBeanBinder relaxedDubboConfigBinder() {
return new BinderDubboConfigBinder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,21 @@
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;

import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties;

/**
* {@link BinderDubboConfigBinder} Test
*
Expand All @@ -38,23 +45,28 @@
public class BinderDubboConfigBinderTest {

@Autowired
private BinderDubboConfigBinder dubboConfigBinder;
private ConfigurationBeanBinder dubboConfigBinder;

@Autowired
private ConfigurableEnvironment environment;

@Test
public void testBinder() {

ApplicationConfig applicationConfig = new ApplicationConfig();
dubboConfigBinder.bind("dubbo.application", applicationConfig);
Map<String, Object> properties = getSubProperties(environment.getPropertySources(), "dubbo.application");
dubboConfigBinder.bind(properties, true, true, applicationConfig);
Assert.assertEquals("hello", applicationConfig.getName());
Assert.assertEquals("world", applicationConfig.getOwner());

RegistryConfig registryConfig = new RegistryConfig();
dubboConfigBinder.bind("dubbo.registry", registryConfig);
properties = getSubProperties(environment.getPropertySources(), "dubbo.registry");
dubboConfigBinder.bind(properties, true, true, registryConfig);
Assert.assertEquals("10.20.153.17", registryConfig.getAddress());

ProtocolConfig protocolConfig = new ProtocolConfig();
dubboConfigBinder.bind("dubbo.protocol", protocolConfig);
properties = getSubProperties(environment.getPropertySources(), "dubbo.protocol");
dubboConfigBinder.bind(properties, true, true, protocolConfig);
Assert.assertEquals(Integer.valueOf(20881), protocolConfig.getPort());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
import org.apache.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor;
import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.ObjectProvider;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class DubboRelaxedBinding2AutoConfigurationTest {

@Autowired
@Qualifier(RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME)
private DubboConfigBinder dubboConfigBinder;
private ConfigurationBeanBinder dubboConfigBinder;

@Autowired
private ObjectProvider<ServiceAnnotationBeanPostProcessor> serviceAnnotationBeanPostProcessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
package org.apache.dubbo.spring.boot.autoconfigure;

import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -47,10 +46,10 @@ public PropertyResolver dubboScanBasePackagesPropertyResolver(Environment enviro
return new RelaxedPropertyResolver(environment, DUBBO_SCAN_PREFIX);
}

@ConditionalOnMissingBean(name = RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME, value = DubboConfigBinder.class)
@ConditionalOnMissingBean(name = RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME, value = ConfigurationBeanBinder.class)
@Bean(RELAXED_DUBBO_CONFIG_BINDER_BEAN_NAME)
@Scope(scopeName = SCOPE_PROTOTYPE)
public DubboConfigBinder relaxedDubboConfigBinder() {
public ConfigurationBeanBinder relaxedDubboConfigBinder() {
return new RelaxedDubboConfigBinder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,32 @@
*/
package org.apache.dubbo.spring.boot.autoconfigure;

import org.apache.dubbo.config.AbstractConfig;
import org.apache.dubbo.config.spring.context.properties.AbstractDubboConfigBinder;
import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.boot.bind.RelaxedDataBinder;

import java.util.Map;

import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getPrefixedProperties;

/**
* Spring Boot Relaxed {@link DubboConfigBinder} implementation
*
* @since 2.7.0
*/
class RelaxedDubboConfigBinder extends AbstractDubboConfigBinder {
class RelaxedDubboConfigBinder implements ConfigurationBeanBinder {

@Override
public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) {
RelaxedDataBinder relaxedDataBinder = new RelaxedDataBinder(dubboConfig);
public void bind(Map<String, Object> configurationProperties, boolean ignoreUnknownFields,
boolean ignoreInvalidFields, Object configurationBean) {
RelaxedDataBinder relaxedDataBinder = new RelaxedDataBinder(configurationBean);
// Set ignored*
relaxedDataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields());
relaxedDataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields());
relaxedDataBinder.setIgnoreInvalidFields(ignoreInvalidFields);
relaxedDataBinder.setIgnoreUnknownFields(ignoreUnknownFields);
// Get properties under specified prefix from PropertySources
Map<String, Object> properties = getPrefixedProperties(getPropertySources(), prefix);
// Convert Map to MutablePropertyValues
MutablePropertyValues propertyValues = new MutablePropertyValues(properties);
MutablePropertyValues propertyValues = new MutablePropertyValues(configurationProperties);
// Bind
relaxedDataBinder.bind(propertyValues);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ private boolean isValidPropertyName(AbstractConfig dubboConfigBean, String prope
// AbstractConfig.checkName(String,String)
Method method = findMethod(AbstractConfig.class, "checkName", String.class, String.class);
try {
if (!method.isAccessible()) {
method.setAccessible(true);
}
if (BeanUtils.getPropertyDescriptor(dubboConfigBean.getClass(), propertyName) != null) {
invokeMethod(method, null, propertyName, propertyValue);
if (method != null) {
if (!method.isAccessible()) {
method.setAccessible(true);
}
if (BeanUtils.getPropertyDescriptor(dubboConfigBean.getClass(), propertyName) != null) {
invokeMethod(method, null, propertyName, propertyValue);
}
}
} catch (IllegalStateException e) {
valid = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

import static com.alibaba.spring.util.BeanRegistrar.registerInfrastructureBean;
import static org.apache.dubbo.config.spring.context.config.NamePropertyDefaultValueDubboConfigBeanCustomizer.BEAN_NAME;
import static org.apache.dubbo.config.spring.util.BeanRegistrar.registerInfrastructureBean;

/**
* Override {@link BeanDefinitionRegistryPostProcessor}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder;

import com.alibaba.spring.context.config.ConfigurationBeanBinder;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Map;

import static com.alibaba.spring.util.PropertySourcesUtils.getSubProperties;

/**
* {@link RelaxedDubboConfigBinder} Test
*/
Expand All @@ -43,22 +49,28 @@
public class RelaxedDubboConfigBinderTest {

@Autowired
private DubboConfigBinder dubboConfigBinder;
private ConfigurationBeanBinder dubboConfigBinder;

@Autowired
private ConfigurableEnvironment environment;

@Test
public void testBinder() {

ApplicationConfig applicationConfig = new ApplicationConfig();
dubboConfigBinder.bind("dubbo.application", applicationConfig);
Map<String, Object> properties = getSubProperties(environment.getPropertySources(), "dubbo.application");
dubboConfigBinder.bind(properties, true, true, applicationConfig);
Assert.assertEquals("hello", applicationConfig.getName());
Assert.assertEquals("world", applicationConfig.getOwner());

RegistryConfig registryConfig = new RegistryConfig();
dubboConfigBinder.bind("dubbo.registry", registryConfig);
properties = getSubProperties(environment.getPropertySources(), "dubbo.registry");
dubboConfigBinder.bind(properties, true, true, registryConfig);
Assert.assertEquals("10.20.153.17", registryConfig.getAddress());

ProtocolConfig protocolConfig = new ProtocolConfig();
dubboConfigBinder.bind("dubbo.protocol", protocolConfig);
properties = getSubProperties(environment.getPropertySources(), "dubbo.protocol");
dubboConfigBinder.bind(properties, true, true, protocolConfig);
Assert.assertEquals(Integer.valueOf(20881), protocolConfig.getPort());

}
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<properties>
<maven_javadoc_version>3.0.1</maven_javadoc_version>
<maven_surefire_version>2.19.1</maven_surefire_version>
<revision>2.7.4.1</revision>
<revision>2.7.5</revision>
</properties>

<modules>
Expand Down Expand Up @@ -90,8 +90,8 @@
</mailingLists>
<developers>
<developer>
<id>Apache Dubbo </id>
<name>The Apache Dubbo Project Contributors</name>
<id>Apache Dubbo</id>
<name>The Apache Dubbo Project Contributors</name>
<email>dev@dubbo.apache.org</email>
<url>http://dubbo.apache.org</url>
</developer>
Expand Down Expand Up @@ -127,7 +127,7 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
Expand Down

0 comments on commit e0d0378

Please sign in to comment.