提交 7895c791 编写于 作者: H haocao

Add configuration map support 3th.

上级 3c02330a
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingjdbc.orchestration.spring.boot;
import com.google.common.base.Preconditions;
......@@ -52,7 +69,7 @@ public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
? OrchestrationShardingDataSourceFactory.createDataSource(dataSourceMap,
shardingProperties.getShardingRuleConfiguration(), orchestrationProperties.getOrchestrationConfiguration(), shardingProperties.getData(), shardingProperties.getProps())
: OrchestrationMasterSlaveDataSourceFactory.createDataSource(dataSourceMap,
masterSlaveProperties.getMasterSlaveRuleConfiguration(), orchestrationProperties.getOrchestrationConfiguration());
masterSlaveProperties.getMasterSlaveRuleConfiguration(), orchestrationProperties.getOrchestrationConfiguration(), masterSlaveProperties.getData());
}
@Override
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingjdbc.orchestration.spring.boot.masterslave;
import io.shardingjdbc.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingjdbc.orchestration.spring.boot.orchestration;
import io.shardingjdbc.orchestration.yaml.YamlOrchestrationConfiguration;
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingjdbc.orchestration.spring.boot.sharding;
import io.shardingjdbc.core.yaml.sharding.YamlShardingRuleConfiguration;
......
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingjdbc.spring.boot.type;
import io.shardingjdbc.core.api.ConfigMapContext;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.spring.boot.util.EmbedTestingServer;
import org.apache.commons.dbcp.BasicDataSource;
......@@ -13,6 +31,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
......@@ -38,5 +58,8 @@ public class OrchestrationSpringBootMasterSlaveTest {
for (DataSource each : ((MasterSlaveDataSource) dataSource).getAllDataSources().values()) {
assertThat(((BasicDataSource) each).getMaxActive(), is(16));
}
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getConfigMap(), is(configMap));
}
}
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingjdbc.spring.boot.type;
import io.shardingjdbc.core.api.ConfigMapContext;
import io.shardingjdbc.core.jdbc.core.ShardingContext;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.spring.boot.util.EmbedTestingServer;
......@@ -15,6 +33,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
......@@ -43,6 +63,9 @@ public class OrchestrationSpringBootShardingTest {
for (DataSource each : shardingContext.getShardingRule().getDataSourceMap().values()) {
assertThat(((BasicDataSource) each).getMaxActive(), is(16));
}
//assertTrue(shardingContext.isShowSQL());
assertTrue(shardingContext.isShowSQL());
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getConfigMap(), is(configMap));
}
}
......@@ -29,3 +29,5 @@ sharding.jdbc.config.orchestration.name=demo_spring_boot_ds_ms
sharding.jdbc.config.orchestration.overwrite=true
sharding.jdbc.config.orchestration.registryCenter.namespace=orchestration-spring-boot-master-slave-test
sharding.jdbc.config.orchestration.registryCenter.server-lists=localhost:3181
sharding.jdbc.config.masterslave.data.key1=value1
......@@ -39,3 +39,5 @@ sharding.jdbc.config.orchestration.name=demo_spring_boot_ds_sharding
sharding.jdbc.config.orchestration.overwrite=true
sharding.jdbc.config.orchestration.registryCenter.namespace=orchestration-spring-boot-sharding-test
sharding.jdbc.config.orchestration.registryCenter.server-lists=localhost:3181
sharding.jdbc.config.masterslave.data.key1=value1
......@@ -41,8 +41,9 @@ public class OrchestrationSpringMasterSlaveDataSource extends MasterSlaveDataSou
private final MasterSlaveRuleConfiguration masterSlaveRuleConfig;
public OrchestrationSpringMasterSlaveDataSource(final String name, final boolean overwrite, final CoordinatorRegistryCenter registryCenter,
final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig) throws SQLException {
super(masterSlaveRuleConfig.build(dataSourceMap));
final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig,
final Map<String, Object> configMap) throws SQLException {
super(masterSlaveRuleConfig.build(dataSourceMap), configMap);
this.dataSourceMap = dataSourceMap;
this.masterSlaveRuleConfig = masterSlaveRuleConfig;
config = new OrchestrationConfiguration(name, registryCenter, overwrite);
......
......@@ -24,6 +24,7 @@ import io.shardingjdbc.core.rule.MasterSlaveRule;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
/**
......@@ -35,16 +36,16 @@ public class SpringMasterSlaveDataSource extends MasterSlaveDataSource {
public SpringMasterSlaveDataSource(final String name, final String masterDataSourceName,
final DataSource masterDataSource, final Map<String, DataSource> slaveDataSourceMap) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap));
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap), Collections.<String, Object>emptyMap());
}
public SpringMasterSlaveDataSource(final String name, final String masterDataSourceName,
final DataSource masterDataSource, final Map<String, DataSource> slaveDataSourceMap, final MasterSlaveLoadBalanceAlgorithm strategy) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategy));
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategy), Collections.<String, Object>emptyMap());
}
public SpringMasterSlaveDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
final Map<String, DataSource> slaveDataSourceMap, final MasterSlaveLoadBalanceAlgorithmType strategyType) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategyType.getAlgorithm()));
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategyType.getAlgorithm()), Collections.<String, Object>emptyMap());
}
}
......@@ -24,14 +24,17 @@ import io.shardingjdbc.core.api.config.MasterSlaveRuleConfiguration;
import io.shardingjdbc.orchestration.spring.datasource.OrchestrationSpringMasterSlaveDataSource;
import io.shardingjdbc.orchestration.spring.datasource.SpringMasterSlaveDataSource;
import io.shardingjdbc.orchestration.spring.namespace.constants.MasterSlaveDataSourceBeanDefinitionParserTag;
import io.shardingjdbc.orchestration.spring.namespace.constants.ShardingDataSourceBeanDefinitionParserTag;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import java.util.Collections;
import java.util.List;
import java.util.Map;
......@@ -66,6 +69,7 @@ public class OrchestrationMasterSlaveDataSourceBeanDefinitionParser extends Abst
} else {
factory.addConstructorArgValue(parseStrategyType(element));
}
factory.addConstructorArgValue(parseConfigMap(element, parserContext, factory.getBeanDefinition()));
return factory.getBeanDefinition();
}
......@@ -141,4 +145,9 @@ public class OrchestrationMasterSlaveDataSourceBeanDefinitionParser extends Abst
String result = element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.STRATEGY_TYPE_ATTRIBUTE);
return Strings.isNullOrEmpty(result) ? MasterSlaveLoadBalanceAlgorithmType.getDefaultAlgorithmType() : MasterSlaveLoadBalanceAlgorithmType.valueOf(result);
}
private Map parseConfigMap(final Element element, final ParserContext parserContext, final BeanDefinition beanDefinition) {
Element dataElement = DomUtils.getChildElementByTagName(element, ShardingDataSourceBeanDefinitionParserTag.DATA_TAG);
return null == dataElement ? Collections.<String, Class<?>>emptyMap() : parserContext.getDelegate().parseMapElement(dataElement, beanDefinition);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://shardingjdbc.io/schema/shardingjdbc/orchestration/masterslave"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://shardingjdbc.io/schema/shardingjdbc/orchestration/masterslave"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>
<xsd:element name="data-source">
<xsd:complexType>
<xsd:all>
<xsd:element ref="data" minOccurs="0" maxOccurs="1" />
</xsd:all>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="master-data-source-name" type="xsd:string" use="required" />
<xsd:attribute name="slave-data-source-names" type="xsd:string" use="required" />
......@@ -15,4 +19,11 @@
<xsd:attribute name="strategy-type" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:mapType" />
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
......@@ -17,6 +17,7 @@
package io.shardingjdbc.orchestration.spring;
import io.shardingjdbc.core.api.ConfigMapContext;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.algorithm.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm;
......@@ -31,6 +32,10 @@ import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
......@@ -76,4 +81,13 @@ public class OrchestrationMasterSlaveNamespaceTest extends AbstractJUnit4SpringC
MasterSlaveDataSource masterSlaveDataSource = this.applicationContext.getBean(masterSlaveDataSourceName, MasterSlaveDataSource.class);
return (MasterSlaveRule) FieldValueUtil.getFieldValue(masterSlaveDataSource, "masterSlaveRule", true);
}
@Test
public void assertConfigMapDataSource() {
Object masterSlaveDataSource = this.applicationContext.getBean("configMapDataSource");
Map<String, Object> configMap = new HashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getConfigMap(), is(configMap));
assertThat(masterSlaveDataSource, instanceOf(MasterSlaveDataSource.class));
}
}
......@@ -19,4 +19,10 @@
<master-slave:data-source id="roundRobinMasterSlaveDataSource" master-data-source-name="dbtbl_0_master" slave-data-source-names="dbtbl_0_slave_0, dbtbl_0_slave_1" strategy-type="ROUND_ROBIN" registry-center-ref="regCenter" />
<master-slave:data-source id="refMasterSlaveDataSource" master-data-source-name="dbtbl_1_master" slave-data-source-names="dbtbl_1_slave_0, dbtbl_1_slave_1" strategy-ref="randomStrategy" registry-center-ref="regCenter" />
<master-slave:data-source id="configMapDataSource" master-data-source-name="dbtbl_1_master" slave-data-source-names="dbtbl_1_slave_0, dbtbl_1_slave_1" strategy-ref="randomStrategy">
<master-slave:data>
<entry key="key1" value="value1" />
</master-slave:data>
</master-slave:data-source>
</beans>
......@@ -63,6 +63,25 @@ public final class OrchestrationMasterSlaveDataSourceFactory {
return result;
}
/**
* Create master-slave data source.
*
* @param dataSourceMap data source map
* @param masterSlaveRuleConfig master-slave rule configuration
* @param orchestrationConfig orchestration master-slave configuration
* @param configMap config map
*
* @return sharding data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(
final Map<String, DataSource> dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig,
final OrchestrationConfiguration orchestrationConfig, final Map<String, Object> configMap) throws SQLException {
MasterSlaveDataSource result = (MasterSlaveDataSource) MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig, configMap);
new OrchestrationFacade(orchestrationConfig).initMasterSlaveOrchestration(dataSourceMap, masterSlaveRuleConfig, result);
return result;
}
/**
* Create master-slave data source.
*
......
......@@ -54,6 +54,7 @@ public final class OrchestrationShardingDataSourceFactory {
* @param dataSourceMap data source map
* @param shardingRuleConfig sharding rule configuration
* @param orchestrationConfig orchestration master-slave configuration
* @param configMap config map
* @param props properties for data source
*
* @return sharding data source
......
......@@ -22,7 +22,6 @@ import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
/**
* Spring boot sharding and master-slave configuration.
......@@ -41,15 +40,13 @@ public class SpringBootConfiguration implements EnvironmentAware {
private final Map<String, DataSource> dataSourceMap = new HashMap<>();
private final Map<String, Object> configMap = new ConcurrentHashMap<>();
private final Properties props = new Properties();
@Bean
public DataSource dataSource() throws SQLException {
return null == masterSlaveProperties.getMasterDataSourceName()
? ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingProperties.getShardingRuleConfiguration(), configMap, shardingProperties.getProps())
: MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveProperties.getMasterSlaveRuleConfiguration());
? ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingProperties.getShardingRuleConfiguration(), shardingProperties.getData(), props)
: MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveProperties.getMasterSlaveRuleConfiguration(), masterSlaveProperties.getData());
}
@Override
......
package io.shardingjdbc.spring.boot.type;
import io.shardingjdbc.core.api.ConfigMapContext;
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
......@@ -11,6 +12,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
......@@ -31,5 +34,8 @@ public class SpringBootMasterSlaveTest {
for (DataSource each : ((MasterSlaveDataSource) dataSource).getAllDataSources().values()) {
assertThat(((BasicDataSource) each).getMaxActive(), is(16));
}
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getConfigMap(), is(configMap));
}
}
package io.shardingjdbc.spring.boot.type;
import io.shardingjdbc.core.api.ConfigMapContext;
import io.shardingjdbc.core.jdbc.core.ShardingContext;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import org.apache.commons.dbcp.BasicDataSource;
......@@ -13,6 +14,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
......@@ -37,5 +40,8 @@ public class SpringBootShardingTest {
assertThat(((BasicDataSource) each).getMaxActive(), is(16));
}
assertTrue(shardingContext.isShowSQL());
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getConfigMap(), is(configMap));
}
}
......@@ -24,3 +24,5 @@ sharding.jdbc.datasource.ds_slave_1.maxActive=16
sharding.jdbc.config.masterslave.name=ds_ms
sharding.jdbc.config.masterslave.masterDataSourceName=ds_master
sharding.jdbc.config.masterslave.slaveDataSourceNames=ds_slave_0,ds_slave_1
sharding.jdbc.config.masterslave.data.key1=value1
......@@ -34,3 +34,5 @@ sharding.jdbc.config.sharding.tables.t_order_item.keyGeneratorColumnName=order_i
sharding.jdbc.config.sharding.props.sql.show=true
sharding.jdbc.config.sharding.props.executor.size=100
sharding.jdbc.config.sharding.data.key1=value1
......@@ -34,17 +34,19 @@ import java.util.Map;
public class SpringMasterSlaveDataSource extends MasterSlaveDataSource {
public SpringMasterSlaveDataSource(final String name, final String masterDataSourceName,
final DataSource masterDataSource, final Map<String, DataSource> slaveDataSourceMap) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap));
final DataSource masterDataSource, final Map<String, DataSource> slaveDataSourceMap, final Map<String, Object> configMap) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap), configMap);
}
public SpringMasterSlaveDataSource(final String name, final String masterDataSourceName,
final DataSource masterDataSource, final Map<String, DataSource> slaveDataSourceMap, final MasterSlaveLoadBalanceAlgorithm strategy) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategy));
final DataSource masterDataSource, final Map<String, DataSource> slaveDataSourceMap,
final MasterSlaveLoadBalanceAlgorithm strategy, final Map<String, Object> configMap) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategy), configMap);
}
public SpringMasterSlaveDataSource(final String name, final String masterDataSourceName, final DataSource masterDataSource,
final Map<String, DataSource> slaveDataSourceMap, final MasterSlaveLoadBalanceAlgorithmType strategyType) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategyType.getAlgorithm()));
final Map<String, DataSource> slaveDataSourceMap, final MasterSlaveLoadBalanceAlgorithmType strategyType,
final Map<String, Object> configMap) throws SQLException {
super(new MasterSlaveRule(name, masterDataSourceName, masterDataSource, slaveDataSourceMap, strategyType.getAlgorithm()), configMap);
}
}
......@@ -17,19 +17,22 @@
package io.shardingjdbc.spring.namespace.parser;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmType;
import io.shardingjdbc.spring.datasource.SpringMasterSlaveDataSource;
import io.shardingjdbc.spring.namespace.constants.MasterSlaveDataSourceBeanDefinitionParserTag;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import io.shardingjdbc.spring.namespace.constants.ShardingDataSourceBeanDefinitionParserTag;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import java.util.Collections;
import java.util.List;
import java.util.Map;
......@@ -56,6 +59,7 @@ public class MasterSlaveDataSourceBeanDefinitionParser extends AbstractBeanDefin
} else {
factory.addConstructorArgValue(parseStrategyType(element));
}
factory.addConstructorArgValue(parseConfigMap(element, parserContext, factory.getBeanDefinition()));
return factory.getBeanDefinition();
}
......@@ -84,4 +88,9 @@ public class MasterSlaveDataSourceBeanDefinitionParser extends AbstractBeanDefin
String result = element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.STRATEGY_TYPE_ATTRIBUTE);
return Strings.isNullOrEmpty(result) ? MasterSlaveLoadBalanceAlgorithmType.getDefaultAlgorithmType() : MasterSlaveLoadBalanceAlgorithmType.valueOf(result);
}
private Map parseConfigMap(final Element element, final ParserContext parserContext, final BeanDefinition beanDefinition) {
Element dataElement = DomUtils.getChildElementByTagName(element, ShardingDataSourceBeanDefinitionParserTag.DATA_TAG);
return null == dataElement ? Collections.<String, Class<?>>emptyMap() : parserContext.getDelegate().parseMapElement(dataElement, beanDefinition);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://shardingjdbc.io/schema/shardingjdbc/masterslave"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://shardingjdbc.io/schema/shardingjdbc/masterslave"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>
<xsd:element name="data-source">
<xsd:complexType>
<xsd:all>
<xsd:element ref="data" minOccurs="0" maxOccurs="1" />
</xsd:all>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="master-data-source-name" type="xsd:string" use="required" />
<xsd:attribute name="slave-data-source-names" type="xsd:string" use="required" />
......@@ -14,4 +18,11 @@
<xsd:attribute name="strategy-type" type="xsd:string" use="optional" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:mapType" />
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
......@@ -17,10 +17,7 @@
package io.shardingjdbc.spring;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import io.shardingjdbc.core.api.ConfigMapContext;
import io.shardingjdbc.core.api.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm;
import io.shardingjdbc.core.api.algorithm.masterslave.RoundRobinMasterSlaveLoadBalanceAlgorithm;
......@@ -30,7 +27,14 @@ import io.shardingjdbc.core.rule.MasterSlaveRule;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.spring.datasource.SpringMasterSlaveDataSource;
import io.shardingjdbc.spring.util.FieldValueUtil;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
......@@ -87,4 +91,13 @@ public class MasterSlaveNamespaceTest extends AbstractJUnit4SpringContextTests {
MasterSlaveDataSource masterSlaveDataSource = this.applicationContext.getBean(masterSlaveDataSourceName, MasterSlaveDataSource.class);
return (MasterSlaveRule) FieldValueUtil.getFieldValue(masterSlaveDataSource, "masterSlaveRule", true);
}
@Test
public void assertConfigMapDataSource() {
Object masterSlaveDataSource = this.applicationContext.getBean("configMapDataSource");
Map<String, Object> configMap = new HashMap<>();
configMap.put("key1", "value1");
assertThat(ConfigMapContext.getInstance().getConfigMap(), is(configMap));
assertThat(masterSlaveDataSource, instanceOf(MasterSlaveDataSource.class));
}
}
......@@ -22,6 +22,12 @@
<master-slave:data-source id="refMasterSlaveDataSource" master-data-source-name="dbtbl_1_master" slave-data-source-names="dbtbl_1_slave_0, dbtbl_1_slave_1" strategy-ref="randomStrategy" />
<master-slave:data-source id="configMapDataSource" master-data-source-name="dbtbl_1_master" slave-data-source-names="dbtbl_1_slave_0, dbtbl_1_slave_1" strategy-ref="randomStrategy">
<master-slave:data>
<entry key="key1" value="value1" />
</master-slave:data>
</master-slave:data-source>
<sharding:data-source id="defaultShardingDataSource">
<sharding:sharding-rule data-source-names="randomMasterSlaveDataSource,refMasterSlaveDataSource" default-data-source-name="randomMasterSlaveDataSource">
<sharding:table-rules>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册