-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for #85 master slave spring namespace
- Loading branch information
Showing
21 changed files
with
387 additions
and
45 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...rdb/sharding/spring/namespace/constants/MasterSlaveDataSourceBeanDefinitionParserTag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 com.dangdang.ddframe.rdb.sharding.spring.namespace.constants; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* 读写分离解析标签. | ||
* | ||
* @author zhangliang | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class MasterSlaveDataSourceBeanDefinitionParserTag { | ||
|
||
public static final String MASTER_DATA_SOURCE_REF_ATTR = "master-data-source-ref"; | ||
|
||
public static final String SLAVE_DATA_SOURCES_REF_ATTR = "slave-data-sources-ref"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...frame/rdb/sharding/spring/namespace/parser/MasterSlaveDataSourceBeanDefinitionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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 com.dangdang.ddframe.rdb.sharding.spring.namespace.parser; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.jdbc.MasterSlaveDataSource; | ||
import com.dangdang.ddframe.rdb.sharding.spring.namespace.constants.MasterSlaveDataSourceBeanDefinitionParserTag; | ||
import com.google.common.base.Splitter; | ||
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.ManagedList; | ||
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; | ||
import org.springframework.beans.factory.xml.ParserContext; | ||
import org.w3c.dom.Element; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 基于Spring命名空间的读写分离数据源解析器. | ||
* | ||
* @author zhangliang | ||
*/ | ||
public class MasterSlaveDataSourceBeanDefinitionParser extends AbstractBeanDefinitionParser { | ||
|
||
@Override | ||
//CHECKSTYLE:OFF | ||
protected AbstractBeanDefinition parseInternal(final Element element, final ParserContext parserContext) { | ||
//CHECKSTYLE:ON | ||
BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(MasterSlaveDataSource.class); | ||
factory.addConstructorArgValue(element.getAttribute(ID_ATTRIBUTE)); | ||
factory.addConstructorArgReference(element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.MASTER_DATA_SOURCE_REF_ATTR)); | ||
factory.addConstructorArgValue(parseSlaveDataSources(element, parserContext)); | ||
return factory.getBeanDefinition(); | ||
} | ||
|
||
private List<BeanDefinition> parseSlaveDataSources(final Element element, final ParserContext parserContext) { | ||
List<String> slaveDataSources = Splitter.on(",").trimResults().splitToList(element.getAttribute(MasterSlaveDataSourceBeanDefinitionParserTag.SLAVE_DATA_SOURCES_REF_ATTR)); | ||
List<BeanDefinition> result = new ManagedList<>(slaveDataSources.size()); | ||
for (String each : slaveDataSources) { | ||
result.add(parserContext.getRegistry().getBeanDefinition(each)); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...dangdang/ddframe/rdb/sharding/spring/cases/namespace/WithNamespaceForMasterSlaveTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 com.dangdang.ddframe.rdb.sharding.spring.cases.namespace; | ||
|
||
import com.dangdang.ddframe.rdb.sharding.spring.AbstractShardingBothDataBasesAndTablesSpringDBUnitTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@ContextConfiguration(locations = "classpath:META-INF/rdb/namespace/withNamespaceForMasterSlave.xml") | ||
public final class WithNamespaceForMasterSlaveTest extends AbstractShardingBothDataBasesAndTablesSpringDBUnitTest { | ||
|
||
@Override | ||
protected List<String> getSchemaFiles() { | ||
return Arrays.asList("schema/dbtbl_0_master.sql", "schema/dbtbl_0_slave_0.sql", "schema/dbtbl_0_slave_1.sql", | ||
"schema/dbtbl_1_master.sql", "schema/dbtbl_1_slave_0.sql", "schema/dbtbl_1_slave_1.sql"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...g-jdbc-config-spring/src/test/resources/META-INF/rdb/datasource/masterSlaveDataSource.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/context | ||
http://www.springframework.org/schema/context/spring-context.xsd | ||
"> | ||
<context:property-placeholder location="classpath:conf/rdb/master_slave_conf.properties" ignore-unresolvable="true"/> | ||
|
||
<bean id="dbtbl_0_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> | ||
<property name="driverClassName" value="${dbtbl_0_master.driver}"/> | ||
<property name="url" value="${dbtbl_0_master.url}"/> | ||
<property name="username" value="${dbtbl_0_master.username}"/> | ||
<property name="password" value="${dbtbl_0_master.password}"/> | ||
</bean> | ||
|
||
<bean id="dbtbl_0_slave_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> | ||
<property name="driverClassName" value="${dbtbl_0_slave_0.driver}"/> | ||
<property name="url" value="${dbtbl_0_slave_0.url}"/> | ||
<property name="username" value="${dbtbl_0_slave_0.username}"/> | ||
<property name="password" value="${dbtbl_0_slave_0.password}"/> | ||
</bean> | ||
|
||
<bean id="dbtbl_0_slave_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> | ||
<property name="driverClassName" value="${dbtbl_0_slave_1.driver}"/> | ||
<property name="url" value="${dbtbl_0_slave_1.url}"/> | ||
<property name="username" value="${dbtbl_0_slave_1.username}"/> | ||
<property name="password" value="${dbtbl_0_slave_1.password}"/> | ||
</bean> | ||
|
||
|
||
<bean id="dbtbl_1_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> | ||
<property name="driverClassName" value="${dbtbl_0_master.driver}"/> | ||
<property name="url" value="${dbtbl_1_master.url}"/> | ||
<property name="username" value="${dbtbl_1_master.username}"/> | ||
<property name="password" value="${dbtbl_1_master.password}"/> | ||
</bean> | ||
|
||
<bean id="dbtbl_1_slave_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> | ||
<property name="driverClassName" value="${dbtbl_0_slave_0.driver}"/> | ||
<property name="url" value="${dbtbl_1_slave_0.url}"/> | ||
<property name="username" value="${dbtbl_1_slave_0.username}"/> | ||
<property name="password" value="${dbtbl_1_slave_0.password}"/> | ||
</bean> | ||
|
||
<bean id="dbtbl_1_slave_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> | ||
<property name="driverClassName" value="${dbtbl_1_slave_1.driver}"/> | ||
<property name="url" value="${dbtbl_1_slave_1.url}"/> | ||
<property name="username" value="${dbtbl_1_slave_1.username}"/> | ||
<property name="password" value="${dbtbl_1_slave_1.password}"/> | ||
</bean> | ||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...c-config-spring/src/test/resources/META-INF/rdb/namespace/withNamespaceForMasterSlave.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans | ||
http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.dangdang.com/schema/ddframe/rdb | ||
http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd | ||
"> | ||
<import resource="../datasource/masterSlaveDataSource.xml" /> | ||
|
||
<rdb:master-slave-data-source id="dbtbl_0" master-data-source-ref="dbtbl_0_master" slave-data-sources-ref="dbtbl_0_slave_0, dbtbl_0_slave_1" /> | ||
<rdb:master-slave-data-source id="dbtbl_1" master-data-source-ref="dbtbl_1_master" slave-data-sources-ref="dbtbl_1_slave_0, dbtbl_1_slave_1" /> | ||
|
||
<rdb:strategy id="databaseStrategy" sharding-columns="user_id" algorithm-expression="dbtbl_${user_id.longValue() % 2}"/> | ||
|
||
<rdb:strategy id="orderTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_${order_id.longValue() % 4}"/> | ||
|
||
<rdb:strategy id="orderItemTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_item_${order_id.longValue() % 4}"/> | ||
|
||
<rdb:data-source id="shardingDataSource"> | ||
<rdb:sharding-rule data-sources="dbtbl_0,dbtbl_1" default-data-source="dbtbl_0"> | ||
<rdb:table-rules> | ||
<rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}, t_order_${0..3}" data-source-names="dbtbl_0, dbtbl_1" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/> | ||
<rdb:table-rule logic-table="t_order_item" actual-tables="t_order_item_${0..3}" data-source-names="dbtbl_${0..1}" database-strategy="databaseStrategy" table-strategy="orderItemTableStrategy"/> | ||
</rdb:table-rules> | ||
</rdb:sharding-rule> | ||
</rdb:data-source> | ||
</beans> |
30 changes: 30 additions & 0 deletions
30
...rent/sharding-jdbc-config-spring/src/test/resources/conf/rdb/master_slave_conf.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
dbtbl_0_master.driver=org.h2.Driver | ||
dbtbl_0_master.url=jdbc:h2:mem:dbtbl_0_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL | ||
dbtbl_0_master.username=sa | ||
dbtbl_0_master.password= | ||
|
||
dbtbl_0_slave_0.driver=org.h2.Driver | ||
dbtbl_0_slave_0.url=jdbc:h2:mem:dbtbl_0_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL | ||
dbtbl_0_slave_0.username=sa | ||
dbtbl_0_slave_0.password= | ||
|
||
dbtbl_0_slave_1.driver=org.h2.Driver | ||
dbtbl_0_slave_1.url=jdbc:h2:mem:dbtbl_0_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL | ||
dbtbl_0_slave_1.username=sa | ||
dbtbl_0_slave_1.password= | ||
|
||
|
||
dbtbl_1_master.driver=org.h2.Driver | ||
dbtbl_1_master.url=jdbc:h2:mem:dbtbl_1_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL | ||
dbtbl_1_master.username=sa | ||
dbtbl_1_master.password= | ||
|
||
dbtbl_1_slave_0.driver=org.h2.Driver | ||
dbtbl_1_slave_0.url=jdbc:h2:mem:dbtbl_1_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL | ||
dbtbl_1_slave_0.username=sa | ||
dbtbl_1_slave_0.password= | ||
|
||
dbtbl_1_slave_1.driver=org.h2.Driver | ||
dbtbl_1_slave_1.url=jdbc:h2:mem:dbtbl_1_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL | ||
dbtbl_1_slave_1.username=sa | ||
dbtbl_1_slave_1.password= |
Oops, something went wrong.