混合规则

混合配置的规则项之间的叠加使用是通过数据源名称和表名称关联的。

如果前一个规则是面向数据源聚合的,下一个规则在配置数据源时,则需要使用前一个规则配置的聚合后的逻辑数据源名称; 同理,如果前一个规则是面向表聚合的,下一个规则在配置表时,则需要使用前一个规则配置的聚合后的逻辑表名称。

配置项说明

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
  6. xmlns:readwrite-splitting="http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting"
  7. xmlns:encrypt="http://shardingsphere.apache.org/schema/shardingsphere/encrypt"
  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/datasource
  15. http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
  16. http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting
  17. http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting/readwrite-splitting.xsd
  18. http://shardingsphere.apache.org/schema/shardingsphere/encrypt
  19. http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd
  20. ">
  21. <bean id="write_ds0" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  22. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  23. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/write_ds?useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
  24. <property name="username" value="root" />
  25. <property name="password" value="" />
  26. </bean>
  27. <bean id="read_ds0_0" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  28. <!-- 省略详细数据源配置详情 -->
  29. </bean>
  30. <bean id="read_ds0_1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  31. <!-- 省略详细数据源配置详情 -->
  32. </bean>
  33. <bean id="write_ds1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  34. <!-- 省略详细数据源配置详情 -->
  35. </bean>
  36. <bean id="read_ds1_0" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  37. <!-- 省略详细数据源配置详情 -->
  38. </bean>
  39. <bean id="read_ds1_1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  40. <!-- 省略详细数据源配置详情 -->
  41. </bean>
  42. <!-- 主从配置负载均衡策略 -->
  43. <readwrite-splitting:load-balance-algorithm id="randomStrategy" type="RANDOM" />
  44. <!-- 主从规则配置 -->
  45. <readwrite-splitting:rule id="readWriteSplittingRule">
  46. <readwrite-splitting:data-source-rule id="ds_0" write-data-source-name="write_ds0" read-data-source-names="read_ds0_0, read_ds0_1" load-balance-algorithm-ref="randomStrategy" />
  47. <readwrite-splitting:data-source-rule id="ds_1" write-data-source-name="write_ds1" read-data-source-names="read_ds1_0, read_ds1_1" load-balance-algorithm-ref="randomStrategy" />
  48. </readwrite-splitting:rule>
  49. <!-- 分片策略配置 -->
  50. <sharding:standard-strategy id="databaseStrategy" sharding-column="user_id" algorithm-ref="inlineDatabaseStrategyAlgorithm" />
  51. <sharding:standard-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-ref="inlineOrderTableStrategyAlgorithm" />
  52. <sharding:standard-strategy id="orderItemTableStrategy" sharding-column="order_item_id" algorithm-ref="inlineOrderItemTableStrategyAlgorithm" />
  53. <sharding:sharding-algorithm id="inlineDatabaseStrategyAlgorithm" type="INLINE">
  54. <props>
  55. <!-- 表达式枚举的数据源名称为主从配置的逻辑数据源名称 -->
  56. <prop key="algorithm-expression">ds_${user_id % 2}</prop>
  57. </props>
  58. </sharding:sharding-algorithm>
  59. <sharding:sharding-algorithm id="inlineOrderTableStrategyAlgorithm" type="INLINE">
  60. <props>
  61. <prop key="algorithm-expression">t_order_${order_id % 2}</prop>
  62. </props>
  63. </sharding:sharding-algorithm>
  64. <sharding:sharding-algorithm id="inlineOrderItemTableStrategyAlgorithm" type="INLINE">
  65. <props>
  66. <prop key="algorithm-expression">t_order_item_${order_item_id % 2}</prop>
  67. </props>
  68. </sharding:sharding-algorithm>
  69. <!-- 分片规则配置 -->
  70. <sharding:rule id="shardingRule">
  71. <sharding:table-rules>
  72. <!-- 表达式 ds_${0..1} 枚举的数据源名称为主从配置的逻辑数据源名称 -->
  73. <sharding:table-rule logic-table="t_order" actual-data-nodes="ds_${0..1}.t_order_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" key-generate-strategy-ref="orderKeyGenerator"/>
  74. <sharding:table-rule logic-table="t_order_item" actual-data-nodes="ds_${0..1}.t_order_item_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderItemTableStrategy" key-generate-strategy-ref="itemKeyGenerator"/>
  75. </sharding:table-rules>
  76. <sharding:binding-table-rules>
  77. <sharding:binding-table-rule logic-tables="t_order, t_order_item"/>
  78. </sharding:binding-table-rules>
  79. <sharding:broadcast-table-rules>
  80. <sharding:broadcast-table-rule table="t_address"/>
  81. </sharding:broadcast-table-rules>
  82. </sharding:rule>
  83. <!-- 数据加密规则配置 -->
  84. <encrypt:encrypt-algorithm id="name_encryptor" type="AES">
  85. <props>
  86. <prop key="aes-key-value">123456</prop>
  87. </props>
  88. </encrypt:encrypt-algorithm>
  89. <encrypt:encrypt-algorithm id="pwd_encryptor" type="assistedTest" />
  90. <encrypt:rule id="encryptRule">
  91. <encrypt:table name="t_user">
  92. <encrypt:column logic-column="user_name" cipher-column="user_name" plain-column="user_name_plain" encrypt-algorithm-ref="name_encryptor" />
  93. <encrypt:column logic-column="pwd" cipher-column="pwd" assisted-query-column="assisted_query_pwd" encrypt-algorithm-ref="pwd_encryptor" />
  94. </encrypt:table>
  95. </encrypt:rule>
  96. <!-- 数据源配置 -->
  97. <!-- data-source-names 数据源名称为所有的数据源节点名称 -->
  98. <shardingsphere:data-source id="readQueryDataSource" data-source-names="write_ds0, read_ds0_0, read_ds0_1, write_ds1, read_ds1_0, read_ds1_1"
  99. rule-refs="readWriteSplittingRule, shardingRule, encryptRule" >
  100. <props>
  101. <prop key="sql-show">true</prop>
  102. </props>
  103. </shardingsphere:data-source>
  104. </beans>