Mixed Rules

Configuration Item Explanation

  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. <!-- ...Omit specific configuration. -->
  29. </bean>
  30. <bean id="read_ds0_1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  31. <!-- ...Omit specific configuration. -->
  32. </bean>
  33. <bean id="write_ds1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  34. <!-- ...Omit specific configuration. -->
  35. </bean>
  36. <bean id="read_ds1_0" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  37. <!-- ...Omit specific configuration. -->
  38. </bean>
  39. <bean id="read_ds1_1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  40. <!-- ...Omit specific configuration. -->
  41. </bean>
  42. <!-- load balance algorithm configuration for readwrite-splitting -->
  43. <readwrite-splitting:load-balance-algorithm id="randomStrategy" type="RANDOM" />
  44. <!-- readwrite-splitting rule configuration -->
  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. <!-- sharding strategy configuration -->
  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. <!-- the expression enumeration is the logical data source name of the readwrite-splitting configuration -->
  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. <!-- sharding rule configuration -->
  70. <sharding:rule id="shardingRule">
  71. <sharding:table-rules>
  72. <!-- the expression 'ds_${0..1}' enumeration is the logical data source name of the readwrite-splitting configuration -->
  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. <!-- data encrypt configuration -->
  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. <!-- datasource configuration -->
  97. <!-- the element data-source-names's value is all of the datasource name -->
  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>