混合规则

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

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

配置项说明

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