Change History

5.0.0-alpha

Replica Query

Configuration Item Explanation

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/replica-query/replica-query-5.0.0.xsd

<replica-query:rule />

NameTypeDescription
idAttributeSpring Bean Id
data-source-rule (+)TagReplica query data source rule configuration

<replica-query:data-source-rule />

NameTypeDescription
idAttributePrimary-replica data source rule name
primary-data-source-nameAttributePrimary data source name
replica-data-source-namesAttributeReplica data source names, multiple data source names separated with comma
load-balance-algorithm-refAttributeLoad balance algorithm name

<replica-query:load-balance-algorithm />

NameTypeDescription
idAttributeLoad balance algorithm name
typeAttributeLoad balance algorithm type
props (?)TagLoad balance algorithm properties

Please refer to Built-in Load Balance Algorithm List for more details about type of algorithm.

4.x

Readwrite-splitting

Configuration Item Explanation

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/masterslave/master-slave.xsd

<master-slave:data-source />

NameTypeExplanation
idAttributeSpring Bean id
master-data-source-nameAttributeBean id of data source in master database
slave-data-source-namesAttributeBean id list of data source in slave database; multiple Beans are separated by commas
strategy-ref (?)AttributeSlave database load balance algorithm reference; the class needs to implement MasterSlaveLoadBalanceAlgorithm interface
strategy-type (?)AttributeLoad balance algorithm type of slave database; optional value: ROUND_ROBIN and RANDOM; if there is load-balance-algorithm-class-name, the configuration can be omitted
config-map (?)TagUsers’ self-defined configurations
props (?)TagAttribute configurations

<master-slave:props />

NameTypeExplanation
sql.show (?)AttributeShow SQL or not; default value: false
executor.size (?)AttributeExecuting thread number; default value: CPU core number
max.connections.size.per.query (?)AttributeThe maximum connection number that each physical database allocates to each query; default value: 1
check.table.metadata.enabled (?)AttributeWhether to check meta-data consistency of sharding table when it initializes; default value: false

<master-slave:load-balance-algorithm />

4.0.0-RC2 version added

NameTypeExplanation
idAttributeSpring Bean Id
typeAttributeType of load balance algorithm, ‘RANDOM’或’ROUND_ROBIN’, support custom extension
props-ref (?)AttributeProperties of load balance algorithm

3.x

Readwrite-splitting

Configuration Item Explanation

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:master-slave="http://shardingsphere.io/schema/shardingsphere/masterslave"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx.xsd
  14. http://shardingsphere.io/schema/shardingsphere/masterslave
  15. http://shardingsphere.io/schema/shardingsphere/masterslave/master-slave.xsd">
  16. <context:annotation-config />
  17. <context:component-scan base-package="io.shardingsphere.example.spring.namespace.jpa" />
  18. <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  19. <property name="dataSource" ref="masterSlaveDataSource" />
  20. <property name="jpaVendorAdapter">
  21. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="MYSQL" />
  22. </property>
  23. <property name="packagesToScan" value="io.shardingsphere.example.spring.namespace.jpa.entity" />
  24. <property name="jpaProperties">
  25. <props>
  26. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  27. <prop key="hibernate.hbm2ddl.auto">create</prop>
  28. <prop key="hibernate.show_sql">true</prop>
  29. </props>
  30. </property>
  31. </bean>
  32. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
  33. <tx:annotation-driven />
  34. <bean id="ds_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  35. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  36. <property name="url" value="jdbc:mysql://localhost:3306/ds_master" />
  37. <property name="username" value="root" />
  38. <property name="password" value="" />
  39. </bean>
  40. <bean id="ds_slave0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  41. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  42. <property name="url" value="jdbc:mysql://localhost:3306/ds_slave0" />
  43. <property name="username" value="root" />
  44. <property name="password" value="" />
  45. </bean>
  46. <bean id="ds_slave1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  47. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  48. <property name="url" value="jdbc:mysql://localhost:3306/ds_slave1" />
  49. <property name="username" value="root" />
  50. <property name="password" value="" />
  51. </bean>
  52. <bean id="randomStrategy" class="io.shardingsphere.api.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm" />
  53. <master-slave:data-source id="masterSlaveDataSource" master-data-source-name="ds_master" slave-data-source-names="ds_slave0, ds_slave1" strategy-ref="randomStrategy">
  54. <master-slave:props>
  55. <prop key="sql.show">${sql_show}</prop>
  56. <prop key="executor.size">10</prop>
  57. <prop key="foo">bar</prop>
  58. </master-slave:props>
  59. </master-slave:data-source>
  60. </beans>

2.x

Readwrite-splitting

The configuration example for Spring namespace

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:sharding="http://shardingsphere.io/schema/shardingjdbc/sharding"
  6. xmlns:masterslave="http://shardingsphere.io/schema/shardingjdbc/masterslave"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://shardingsphere.io/schema/shardingjdbc/sharding
  12. http://shardingsphere.io/schema/shardingjdbc/sharding/sharding.xsd
  13. http://shardingsphere.io/schema/shardingjdbc/masterslave
  14. http://shardingsphere.io/schema/shardingjdbc/masterslave/master-slave.xsd
  15. ">
  16. <!-- Actual source data Configuration -->
  17. <bean id="dbtbl_0_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  18. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  19. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0_master"/>
  20. <property name="username" value="root"/>
  21. <property name="password" value=""/>
  22. </bean>
  23. <bean id="dbtbl_0_slave_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  24. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  25. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0_slave_0"/>
  26. <property name="username" value="root"/>
  27. <property name="password" value=""/>
  28. </bean>
  29. <bean id="dbtbl_0_slave_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  30. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  31. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0_slave_1"/>
  32. <property name="username" value="root"/>
  33. <property name="password" value=""/>
  34. </bean>
  35. <bean id="dbtbl_1_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  36. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  37. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1_master"/>
  38. <property name="username" value="root"/>
  39. <property name="password" value=""/>
  40. </bean>
  41. <bean id="dbtbl_1_slave_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  42. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  43. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1_slave_0"/>
  44. <property name="username" value="root"/>
  45. <property name="password" value=""/>
  46. </bean>
  47. <bean id="dbtbl_1_slave_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  48. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  49. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1_slave_1"/>
  50. <property name="username" value="root"/>
  51. <property name="password" value=""/>
  52. </bean>
  53. <!-- Readwrite-splitting DataSource Configuration -->
  54. <master-slave:data-source id="dbtbl_0" master-data-source-name="dbtbl_0_master" slave-data-source-names="dbtbl_0_slave_0, dbtbl_0_slave_1" strategy-type="ROUND_ROBIN" />
  55. <master-slave:data-source id="dbtbl_1" master-data-source-name="dbtbl_1_master" slave-data-source-names="dbtbl_1_slave_0, dbtbl_1_slave_1" strategy-type="ROUND_ROBIN" />
  56. <sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="dbtbl_${user_id % 2}" />
  57. <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_${order_id % 4}" />
  58. <sharding:data-source id="shardingDataSource">
  59. <sharding:sharding-rule data-source-names="dbtbl_0, dbtbl_1">
  60. <sharding:table-rules>
  61. <sharding:table-rule logic-table="t_order" actual-data-nodes="dbtbl_${0..1}.t_order_${0..3}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy"/>
  62. </sharding:table-rules>
  63. </sharding:sharding-rule>
  64. </sharding:data-source>
  65. </beans>

1.x

Readwrite-splitting

The configuration example for Spring namespace

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:rdb="http://www.dangdang.com/schema/ddframe/rdb"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.dangdang.com/schema/ddframe/rdb
  11. http://www.dangdang.com/schema/ddframe/rdb/rdb.xsd
  12. ">
  13. <!-- 配置真实数据源 -->
  14. <bean id="dbtbl_0_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  15. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  16. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0_master"/>
  17. <property name="username" value="root"/>
  18. <property name="password" value=""/>
  19. </bean>
  20. <bean id="dbtbl_0_slave_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  21. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  22. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0_slave_0"/>
  23. <property name="username" value="root"/>
  24. <property name="password" value=""/>
  25. </bean>
  26. <bean id="dbtbl_0_slave_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  27. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  28. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_0_slave_1"/>
  29. <property name="username" value="root"/>
  30. <property name="password" value=""/>
  31. </bean>
  32. <bean id="dbtbl_1_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  33. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  34. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1_master"/>
  35. <property name="username" value="root"/>
  36. <property name="password" value=""/>
  37. </bean>
  38. <bean id="dbtbl_1_slave_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  39. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  40. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1_slave_0"/>
  41. <property name="username" value="root"/>
  42. <property name="password" value=""/>
  43. </bean>
  44. <bean id="dbtbl_1_slave_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  45. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  46. <property name="url" value="jdbc:mysql://localhost:3306/dbtbl_1_slave_1"/>
  47. <property name="username" value="root"/>
  48. <property name="password" value=""/>
  49. </bean>
  50. <!-- 定义读写分离数据源, 读写分离数据源实现了DataSource接口, 可直接当做数据源处理 -->
  51. <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" strategy-type="ROUND_ROBIN" />
  52. <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" strategy-type="ROUND_ROBIN" />
  53. <!-- 通过rdb:strategy和rdb:data-source继续构建分片数据源 -->
  54. <rdb:strategy id="databaseStrategy" sharding-columns="user_id" algorithm-expression="dbtbl_${user_id.longValue() % 2}"/>
  55. <rdb:strategy id="orderTableStrategy" sharding-columns="order_id" algorithm-expression="t_order_${order_id.longValue() % 4}"/>
  56. <rdb:data-source id="shardingDataSource">
  57. <rdb:sharding-rule data-sources="dbtbl_0, dbtbl_1">
  58. <rdb:table-rules>
  59. <rdb:table-rule logic-table="t_order" actual-tables="t_order_${0..3}" database-strategy="databaseStrategy" table-strategy="orderTableStrategy"/>
  60. </rdb:table-rules>
  61. </rdb:sharding-rule>
  62. </rdb:data-source>
  63. </beans>