Spring命名空间配置

注意事项

行表达式标识符可以使用${...}$->{...},但前者与Spring本身的属性文件占位符冲突,因此在Spring环境中使用行表达式标识符建议使用$->{...}

配置示例

详细example: shardingsphere-example

数据分片

  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:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://shardingsphere.apache.org/schema/shardingsphere/sharding
  11. http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16. <context:annotation-config />
  17. <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  18. <property name="dataSource" ref="shardingDataSource" />
  19. <property name="jpaVendorAdapter">
  20. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="MYSQL" />
  21. </property>
  22. <property name="packagesToScan" value="org.apache.shardingsphere.example.core.jpa.entity" />
  23. <property name="jpaProperties">
  24. <props>
  25. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  26. <prop key="hibernate.hbm2ddl.auto">create</prop>
  27. <prop key="hibernate.show_sql">true</prop>
  28. </props>
  29. </property>
  30. </bean>
  31. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
  32. <tx:annotation-driven />
  33. <bean id="ds0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  34. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  35. <property name="url" value="jdbc:mysql://localhost:3306/ds0" />
  36. <property name="username" value="root" />
  37. <property name="password" value="" />
  38. </bean>
  39. <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  40. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  41. <property name="url" value="jdbc:mysql://localhost:3306/ds1" />
  42. <property name="username" value="root" />
  43. <property name="password" value="" />
  44. </bean>
  45. <bean id="preciseModuloDatabaseShardingAlgorithm" class="org.apache.shardingsphere.example.algorithm.PreciseModuloShardingDatabaseAlgorithm" />
  46. <bean id="preciseModuloTableShardingAlgorithm" class="org.apache.shardingsphere.example.algorithm.PreciseModuloShardingTableAlgorithm" />
  47. <sharding:standard-strategy id="databaseShardingStrategy" sharding-column="user_id" precise-algorithm-ref="preciseModuloDatabaseShardingAlgorithm" />
  48. <sharding:standard-strategy id="tableShardingStrategy" sharding-column="order_id" precise-algorithm-ref="preciseModuloTableShardingAlgorithm" />
  49. <sharding:key-generator id="orderKeyGenerator" type="SNOWFLAKE" column="order_id" />
  50. <sharding:key-generator id="itemKeyGenerator" type="SNOWFLAKE" column="order_item_id" />
  51. <sharding:data-source id="shardingDataSource">
  52. <sharding:sharding-rule data-source-names="ds0,ds1">
  53. <sharding:table-rules>
  54. <sharding:table-rule logic-table="t_order" actual-data-nodes="ds$->{0..1}.t_order$->{0..1}" database-strategy-ref="databaseShardingStrategy" table-strategy-ref="tableShardingStrategy" key-generator-ref="orderKeyGenerator" />
  55. <sharding:table-rule logic-table="t_order_item" actual-data-nodes="ds$->{0..1}.t_order_item$->{0..1}" database-strategy-ref="databaseShardingStrategy" table-strategy-ref="tableShardingStrategy" key-generator-ref="itemKeyGenerator" />
  56. </sharding:table-rules>
  57. <sharding:binding-table-rules>
  58. <sharding:binding-table-rule logic-tables="t_order, t_order_item" />
  59. </sharding:binding-table-rules>
  60. <sharding:broadcast-table-rules>
  61. <sharding:broadcast-table-rule table="t_config" />
  62. </sharding:broadcast-table-rules>
  63. </sharding:sharding-rule>
  64. </sharding:data-source>
  65. </beans>

读写分离

  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.apache.org/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.apache.org/schema/shardingsphere/masterslave
  15. http://shardingsphere.apache.org/schema/shardingsphere/masterslave/master-slave.xsd">
  16. <context:annotation-config />
  17. <context:component-scan base-package="org.apache.shardingsphere.example.core.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="org.apache.shardingsphere.example.core.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. <!-- 4.0.0-RC1 版本 负载均衡策略配置方式 -->
  53. <!-- <bean id="randomStrategy" class="org.apache.shardingsphere.example.spring.namespace.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm" /> -->
  54. <!-- 4.0.0-RC2 之后版本 负载均衡策略配置方式 -->
  55. <master-slave:load-balance-algorithm id="randomStrategy" type="RANDOM" />
  56. <master-slave:data-source id="masterSlaveDataSource" master-data-source-name="ds_master" slave-data-source-names="ds_slave0, ds_slave1" strategy-ref="randomStrategy">
  57. <master-slave:props>
  58. <prop key="sql.show">true</prop>
  59. <prop key="executor.size">10</prop>
  60. <prop key="foo">bar</prop>
  61. </master-slave:props>
  62. </master-slave:data-source>
  63. </beans>

数据脱敏

  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:encrypt="http://shardingsphere.apache.org/schema/shardingsphere/encrypt"
  5. xmlns:bean="http://www.springframework.org/schema/util"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://shardingsphere.apache.org/schema/shardingsphere/encrypt
  9. http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd
  10. http://www.springframework.org/schema/util
  11. http://www.springframework.org/schema/util/spring-util.xsd">
  12. <bean id="ds" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  14. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo_ds?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
  15. <property name="username" value="root"/>
  16. <property name="password" value=""/>
  17. </bean>
  18. <bean:properties id="props">
  19. <prop key="aes.key.value">123456</prop>
  20. </bean:properties>
  21. <encrypt:data-source id="encryptDataSource" data-source-name="ds" >
  22. <encrypt:encrypt-rule>
  23. <encrypt:tables>
  24. <encrypt:table name="t_order">
  25. <encrypt:column logic-column="user_id" plain-column="user_decrypt" cipher-column="user_encrypt" assisted-query-column="user_assisted" encryptor-ref="encryptor_aes" />
  26. <encrypt:column logic-column="order_id" plain-column="order_decrypt" cipher-column="order_encrypt" assisted-query-column="order_assisted" encryptor-ref="encryptor_md5"/>
  27. </encrypt:table>
  28. </encrypt:tables>
  29. <encrypt:encryptors>
  30. <encrypt:encryptor id="encryptor_aes" type="AES" props-ref="props"/>
  31. <encrypt:encryptor id="encryptor_md5" type="MD5" />
  32. </encrypt:encryptors>
  33. </encrypt:encrypt-rule>
  34. <encrypt:props>
  35. <prop key="sql.show">true</prop>
  36. <prop key="query.with.cipher.column">true</prop>
  37. </encrypt:props>
  38. </encrypt:data-source>
  39. </beans>

数据分片 + 读写分离

  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:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
  8. xmlns:master-slave="http://shardingsphere.apache.org/schema/shardingsphere/masterslave"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://www.springframework.org/schema/tx
  14. http://www.springframework.org/schema/tx/spring-tx.xsd
  15. http://shardingsphere.apache.org/schema/shardingsphere/sharding
  16. http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
  17. http://shardingsphere.apache.org/schema/shardingsphere/masterslave
  18. http://shardingsphere.apache.org/schema/shardingsphere/masterslave/master-slave.xsd">
  19. <context:annotation-config />
  20. <context:component-scan base-package="org.apache.shardingsphere.example.core.jpa" />
  21. <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  22. <property name="dataSource" ref="shardingDataSource" />
  23. <property name="jpaVendorAdapter">
  24. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="MYSQL" />
  25. </property>
  26. <property name="packagesToScan" value="org.apache.shardingsphere.example.core.jpa.entity" />
  27. <property name="jpaProperties">
  28. <props>
  29. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  30. <prop key="hibernate.hbm2ddl.auto">create</prop>
  31. <prop key="hibernate.show_sql">true</prop>
  32. </props>
  33. </property>
  34. </bean>
  35. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
  36. <tx:annotation-driven />
  37. <bean id="ds_master0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  38. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  39. <property name="url" value="jdbc:mysql://localhost:3306/ds_master0" />
  40. <property name="username" value="root" />
  41. <property name="password" value="" />
  42. </bean>
  43. <bean id="ds_master0_slave0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  44. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  45. <property name="url" value="jdbc:mysql://localhost:3306/ds_master0_slave0" />
  46. <property name="username" value="root" />
  47. <property name="password" value="" />
  48. </bean>
  49. <bean id="ds_master0_slave1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  50. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  51. <property name="url" value="jdbc:mysql://localhost:3306/ds_master0_slave1" />
  52. <property name="username" value="root" />
  53. <property name="password" value="" />
  54. </bean>
  55. <bean id="ds_master1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  56. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  57. <property name="url" value="jdbc:mysql://localhost:3306/ds_master1" />
  58. <property name="username" value="root" />
  59. <property name="password" value="" />
  60. </bean>
  61. <bean id="ds_master1_slave0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  62. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  63. <property name="url" value="jdbc:mysql://localhost:3306/ds_master1_slave0" />
  64. <property name="username" value="root" />
  65. <property name="password" value="" />
  66. </bean>
  67. <bean id="ds_master1_slave1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  68. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  69. <property name="url" value="jdbc:mysql://localhost:3306/ds_master1_slave1" />
  70. <property name="username" value="root" />
  71. <property name="password" value="" />
  72. </bean>
  73. <!-- 4.0.0-RC1 版本 负载均衡策略配置方式 -->
  74. <!-- <bean id="randomStrategy" class="org.apache.shardingsphere.example.spring.namespace.algorithm.masterslave.RandomMasterSlaveLoadBalanceAlgorithm" /> -->
  75. <!-- 4.0.0-RC2 之后版本 负载均衡策略配置方式 -->
  76. <master-slave:load-balance-algorithm id="randomStrategy" type="RANDOM" />
  77. <sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="ds_ms$->{user_id % 2}" />
  78. <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order$->{order_id % 2}" />
  79. <sharding:inline-strategy id="orderItemTableStrategy" sharding-column="order_id" algorithm-expression="t_order_item$->{order_id % 2}" />
  80. <sharding:key-generator id="orderKeyGenerator" type="SNOWFLAKE" column="order_id" />
  81. <sharding:key-generator id="itemKeyGenerator" type="SNOWFLAKE" column="order_item_id" />
  82. <sharding:data-source id="shardingDataSource">
  83. <sharding:sharding-rule data-source-names="ds_master0,ds_master0_slave0,ds_master0_slave1,ds_master1,ds_master1_slave0,ds_master1_slave1">
  84. <sharding:master-slave-rules>
  85. <sharding:master-slave-rule id="ds_ms0" master-data-source-name="ds_master0" slave-data-source-names="ds_master0_slave0, ds_master0_slave1" strategy-ref="randomStrategy" />
  86. <sharding:master-slave-rule id="ds_ms1" master-data-source-name="ds_master1" slave-data-source-names="ds_master1_slave0, ds_master1_slave1" strategy-ref="randomStrategy" />
  87. </sharding:master-slave-rules>
  88. <sharding:table-rules>
  89. <sharding:table-rule logic-table="t_order" actual-data-nodes="ds_ms$->{0..1}.t_order$->{0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" key-generator-ref="orderKeyGenerator" />
  90. <sharding:table-rule logic-table="t_order_item" actual-data-nodes="ds_ms$->{0..1}.t_order_item$->{0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderItemTableStrategy" key-generator-ref="itemKeyGenerator" />
  91. </sharding:table-rules>
  92. <sharding:binding-table-rules>
  93. <sharding:binding-table-rule logic-tables="t_order, t_order_item" />
  94. </sharding:binding-table-rules>
  95. <sharding:broadcast-table-rules>
  96. <sharding:broadcast-table-rule table="t_config" />
  97. </sharding:broadcast-table-rules>
  98. </sharding:sharding-rule>
  99. </sharding:data-source>
  100. </beans>

数据分片 + 数据脱敏

  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:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:encrypt="http://shardingsphere.apache.org/schema/shardingsphere/encrypt"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:bean="http://www.springframework.org/schema/util"
  9. xmlns:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
  10. xsi:schemaLocation="http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans.xsd
  12. http://shardingsphere.apache.org/schema/shardingsphere/sharding
  13. http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context.xsd
  16. http://shardingsphere.apache.org/schema/shardingsphere/encrypt
  17. http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd
  18. http://www.springframework.org/schema/tx
  19. http://www.springframework.org/schema/tx/spring-tx.xsd
  20. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
  21. <import resource="classpath:META-INF/shardingTransaction.xml"/>
  22. <context:annotation-config />
  23. <context:component-scan base-package="org.apache.shardingsphere.example.core.jpa"/>
  24. <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  25. <property name="dataSource" ref="shardingDataSource" />
  26. <property name="jpaVendorAdapter">
  27. <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:database="MYSQL" />
  28. </property>
  29. <property name="packagesToScan" value="org.apache.shardingsphere.example.core.jpa.entity" />
  30. <property name="jpaProperties">
  31. <props>
  32. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  33. <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
  34. <prop key="hibernate.show_sql">true</prop>
  35. </props>
  36. </property>
  37. </bean>
  38. <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" p:entityManagerFactory-ref="entityManagerFactory" />
  39. <tx:annotation-driven />
  40. <bean id="demo_ds_0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  41. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  42. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo_ds_0"/>
  43. <property name="username" value="root"/>
  44. <property name="password" value=""/>
  45. <property name="maximumPoolSize" value="16"/>
  46. </bean>
  47. <bean id="demo_ds_1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  48. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  49. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo_ds_1"/>
  50. <property name="username" value="root"/>
  51. <property name="password" value=""/>
  52. <property name="maximumPoolSize" value="16"/>
  53. </bean>
  54. <sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="demo_ds_${user_id % 2}" />
  55. <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_${order_id % 2}" />
  56. <sharding:inline-strategy id="orderItemTableStrategy" sharding-column="order_id" algorithm-expression="t_order_item_${order_id % 2}" />
  57. <sharding:inline-strategy id="orderEncryptTableStrategy" sharding-column="order_id" algorithm-expression="t_order_encrypt_${order_id % 2}" />
  58. <sharding:key-generator id="orderKeyGenerator" type="SNOWFLAKE" column="order_id" />
  59. <sharding:key-generator id="itemKeyGenerator" type="SNOWFLAKE" column="order_item_id" />
  60. <bean:properties id="dataProtectorProps">
  61. <prop key="appToken">business</prop>
  62. </bean:properties>
  63. <sharding:data-source id="shardingDataSource">
  64. <sharding:sharding-rule data-source-names="demo_ds_0, demo_ds_1">
  65. <sharding:table-rules>
  66. <sharding:table-rule logic-table="t_order" actual-data-nodes="demo_ds_${0..1}.t_order_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" key-generator-ref="orderKeyGenerator" />
  67. <sharding:table-rule logic-table="t_order_item" actual-data-nodes="demo_ds_${0..1}.t_order_item_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderItemTableStrategy" key-generator-ref="itemKeyGenerator" />
  68. </sharding:table-rules>
  69. <sharding:encrypt-rule>
  70. <encrypt:tables>
  71. <encrypt:table name="t_order">
  72. <encrypt:column logic-column="user_id" plain-column="user_decrypt" cipher-column="user_encrypt" assisted-query-column="user_assisted" encryptor-ref="encryptor_aes" />
  73. <encrypt:column logic-column="order_id" plain-column="order_decrypt" cipher-column="order_encrypt" assisted-query-column="order_assisted" encryptor-ref="encryptor_md5"/>
  74. </encrypt:table>
  75. </encrypt:tables>
  76. <encrypt:encryptors>
  77. <encrypt:encryptor id="encryptor_aes" type="AES" props-ref="props"/>
  78. <encrypt:encryptor id="encryptor_md5" type="MD5" />
  79. </encrypt:encryptors>
  80. </sharding:encrypt-rule>
  81. </sharding:sharding-rule>
  82. <sharding:props>
  83. <prop key="sql.show">true</prop>
  84. </sharding:props>
  85. </sharding:data-source>
  86. <bean:properties id="props">
  87. <prop key="aes.key.value">123456</prop>
  88. </bean:properties>
  89. </beans>

治理

  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:orchestration="http://shardingsphere.apache.org/schema/shardingsphere/orchestration"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://shardingsphere.apache.org/schema/shardingsphere/orchestration
  8. http://shardingsphere.apache.org/schema/shardingsphere/orchestration/orchestration.xsd">
  9. <util:properties id="instance-props">
  10. <prop key="max-retries">3</prop>
  11. <prop key="operation-timeout-milliseconds">3000</prop>
  12. </util:properties>
  13. <orchestraion:instance id="regCenter" orchestration-type="registry_center,config_center" instance-type="zookeeper" server-lists="localhost:2181" namespace="orchestration-spring-namespace-demo"
  14. props-ref="instance-props" />
  15. <orchestraion:sharding-data-source id="shardingDatabasesTablesDataSource" data-source-ref="realShardingDatabasesTablesDataSource" instance-ref="regCenter" overwrite="true" />
  16. <orchestraion:master-slave-data-source id="masterSlaveDataSource" data-source-ref="realMasterSlaveDataSource" instance-ref="regCenter" overwrite="true" />
  17. <orchestraion:encrypt-data-source id="encryptDataSource" data-source-ref="realEncryptDataSource" instance-ref="regCenter" overwrite="true" />
  18. </beans>

配置项说明

分库分表

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd

<sharding:data-source />

名称类型说明
id属性Spring Bean Id
sharding-rule标签数据分片配置规则
props (?)标签属性配置

<sharding:sharding-rule />

名称类型说明
data-source-names属性数据源Bean列表,多个Bean以逗号分隔
table-rules标签表分片规则配置对象
binding-table-rules (?)标签绑定表规则列表
broadcast-table-rules (?)标签广播表规则列表
default-data-source-name (?)属性未配置分片规则的表将通过默认数据源定位
default-database-strategy-ref (?)属性默认数据库分片策略,对应<sharding:xxx-strategy>中的策略Id,缺省表示不分库
default-table-strategy-ref (?)属性默认表分片策略,对应<sharding:xxx-strategy>中的策略Id,缺省表示不分表
default-key-generator-ref (?)属性默认自增列值生成器引用,缺省使用org.apache.shardingsphere.core.keygen.generator.impl.SnowflakeKeyGenerator
encrypt-rule (?)标签脱敏规则

<sharding:table-rules />

名称类型说明
table-rule (+)标签表分片规则配置对象

<sharding:table-rule />

名称类型说明
logic-table属性逻辑表名称
actual-data-nodes (?)属性由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持inline表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
database-strategy-ref (?)属性数据库分片策略,对应<sharding:xxx-strategy>中的策略Id,缺省表示使用<sharding:sharding-rule />配置的默认数据库分片策略
table-strategy-ref (?)属性表分片策略,对应<sharding:xxx-strategy>中的策略Id,缺省表示使用<sharding:sharding-rule />配置的默认表分片策略
key-generator-ref (?)属性自增列值生成器引用,缺省表示使用默认自增列值生成器

<sharding:binding-table-rules />

名称类型说明
binding-table-rule (+)标签绑定表规则

<sharding:binding-table-rule />

名称类型说明
logic-tables属性绑定规则的逻辑表名,多表以逗号分隔

<sharding:broadcast-table-rules />

名称类型说明
broadcast-table-rule (+)标签广播表规则

<sharding:broadcast-table-rule />

名称类型*说明
table属性广播规则的表名

<sharding:standard-strategy />

名称类型说明
id属性Spring Bean Id
sharding-column属性分片列名称
precise-algorithm-ref属性精确分片算法引用,用于=和IN。该类需实现PreciseShardingAlgorithm接口
range-algorithm-ref (?)属性范围分片算法引用,用于BETWEEN。该类需实现RangeShardingAlgorithm接口

<sharding:complex-strategy />

名称类型说明
id属性Spring Bean Id
sharding-columns属性分片列名称,多个列以逗号分隔
algorithm-ref属性复合分片算法引用。该类需实现ComplexKeysShardingAlgorithm接口

<sharding:inline-strategy />

名称类型说明
id属性Spring Bean Id
sharding-column属性分片列名称
algorithm-expression属性分片算法行表达式,需符合groovy语法

<sharding:hint-database-strategy />

名称类型说明
id属性Spring Bean Id
algorithm-ref属性Hint分片算法。该类需实现HintShardingAlgorithm接口

<sharding:none-strategy />

名称类型说明
id属性Spring Bean Id

<sharding:key-generator />

名称类型说明
column属性自增列名称
type属性自增列值生成器类型,可自定义或选择内置类型:SNOWFLAKE/UUID
props-ref属性自增列值生成器的属性配置引用

Properties

属性配置项,可以为以下自增列值生成器的属性。

SNOWFLAKE
名称数据类型说明
worker.id (?)long工作机器唯一id,默认为0
max.tolerate.time.difference.milliseconds (?)long最大容忍时钟回退时间,单位:毫秒。默认为10毫秒
max.vibration.offset (?)int最大抖动上限值,范围[0, 4096),默认为1。注:若使用此算法生成值作分片值,建议配置此属性。此算法在不同毫秒内所生成的key取模2^n (2^n一般为分库或分表数) 之后结果总为0或1。为防止上述分片问题,建议将此属性值配置为(2^n)-1

<sharding:encrypt-rule />

名称类型说明
encrypt:encrypt-rule(?)标签加解密规则

<sharding:props />

名称类型说明
sql.show (?)属性是否开启SQL显示,默认值: false
executor.size (?)属性工作线程数量,默认值: CPU核数
max.connections.size.per.query (?)属性每个物理数据库为每次查询分配的最大连接数量。默认值: 1
check.table.metadata.enabled (?)属性是否在启动时检查分表元数据一致性,默认值: false
query.with.cipher.column (?)属性当存在明文列时,是否使用密文列查询,默认值: true

读写分离

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/masterslave/master-slave.xsd

<master-slave:data-source />

名称类型说明
id属性Spring Bean Id
master-data-source-name属性主库数据源Bean Id
slave-data-source-names属性从库数据源Bean Id列表,多个Bean以逗号分隔
strategy-ref (?)属性从库负载均衡算法引用。该类需实现MasterSlaveLoadBalanceAlgorithm接口
strategy-type (?)属性从库负载均衡算法类型,可选值:ROUND_ROBIN,RANDOM。若strategy-ref存在则忽略该配置
props (?)标签属性配置

<master-slave:props />

名称类型说明
sql.show (?)属性是否开启SQL显示,默认值: false
executor.size (?)属性工作线程数量,默认值: CPU核数
max.connections.size.per.query (?)属性每个物理数据库为每次查询分配的最大连接数量。默认值: 1
check.table.metadata.enabled (?)属性是否在启动时检查分表元数据一致性,默认值: false

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

4.0.0-RC2 版本 添加

名称类型说明
id属性Spring Bean Id
type属性负载均衡算法类型,‘RANDOM’或’ROUND_ROBIN’,支持自定义拓展
props-ref (?)属性负载均衡算法配置参数

数据脱敏

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd

<encrypt:data-source />

名称类型说明
id属性Spring Bean Id
data-source-name属性加密数据源Bean Id
props (?)标签属性配置

<encrypt:encryptors />

名称类型说明
encryptor(+)标签加密器配置

<encrypt:encryptor />

名称类型说明
id属性加密器的名称
type属性加解密器类型,可自定义或选择内置类型:MD5/AES
props-ref属性属性配置, 注意:使用AES加密器,需要配置AES加密器的KEY属性:aes.key.value

<encrypt:tables />

名称类型说明
table(+)标签加密表配置

<encrypt:table />

名称类型说明
column(+)标签加密列配置

<encrypt:column />

名称类型说明
logic-column属性逻辑列名
plain-column属性存储明文的字段
cipher-column属性存储密文的字段
assisted-query-columns属性辅助查询字段,针对ShardingQueryAssistedEncryptor类型的加解密器进行辅助查询

<encrypt:props />

名称类型说明
sql.show (?)属性是否开启SQL显示,默认值: false
query.with.cipher.column (?)属性当存在明文列时,是否使用密文列查询,默认值: true

数据分片 + 治理

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/orchestration/orchestration.xsd

<orchestration:sharding-data-source />

名称类型说明
id属性ID
data-source-ref (?)属性被治理的数据库id
instance-ref属性配置/注册中心id
overwrite属性本地配置是否覆盖配置中心配置。如果可覆盖,每次启动都以本地配置为准。缺省为不覆盖

读写分离 + 治理

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/orchestration/orchestration.xsd

<orchestration:master-slave-data-source />

名称类型说明
id属性ID
data-source-ref (?)属性被治理的数据库id
instance-ref属性配置/注册中心id
overwrite属性本地配置是否覆盖配置中心配置。如果可覆盖,每次启动都以本地配置为准。缺省为不覆盖

数据脱敏 + 治理

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/orchestration/orchestration.xsd

<orchestration:encrypt-data-source />

名称类型说明
id属性ID
data-source-ref (?)属性被治理的数据库id
instance-ref属性配置/注册中心id
overwrite属性本地配置是否覆盖配置中心配置。如果可覆盖,每次启动都以本地配置为准。缺省为不覆盖

治理配置/注册中心

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/orchestration/orchestration.xsd

<orchestration:instance />

名称类型说明
id属性配置/注册中心的Spring Bean Id
instance-type属性配置/注册中心类型。如:zookeeper
orchestration-type属性配置/注册中心的类型,例如config_center/registry_center
server-lists属性连接配置/注册中心服务器的列表,包括IP地址和端口号,多个地址用逗号分隔。如: host1:2181,host2:2181
namespace (?)属性配置/注册中心的命名空间
props-ref (?)属性配置中心其它属性