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: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. <!-- ...Omit specific configuration. -->
  23. </bean>
  24. <bean id="read_ds0_1" class=" com.zaxxer.hikari.HikariDataSource" init-method="init" destroy-method="close">
  25. <!-- ...Omit specific configuration. -->
  26. </bean>
  27. <bean id="write_ds1" class=" com.zaxxer.hikari.HikariDataSource" init-method="init" destroy-method="close">
  28. <!-- ...Omit specific configuration. -->
  29. </bean>
  30. <bean id="read_ds1_0" class=" com.zaxxer.hikari.HikariDataSource" init-method="init" destroy-method="close">
  31. <!-- ...Omit specific configuration. -->
  32. </bean>
  33. <bean id="read_ds1_1" class=" com.zaxxer.hikari.HikariDataSource" init-method="init" destroy-method="close">
  34. <!-- ...Omit specific configuration. -->
  35. </bean>
  36. <!-- load balance algorithm configuration for readwrite-splitting -->
  37. <readwrite-splitting:load-balance-algorithm id="randomStrategy" type="RANDOM" />
  38. <!-- readwrite-splitting rule configuration -->
  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. <!-- sharding strategy configuration -->
  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. <!-- the expression enumeration is the logical data source name of the readwrite-splitting configuration -->
  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. <!-- sharding rule configuration -->
  64. <sharding:rule id="shardingRule">
  65. <sharding:table-rules>
  66. <!-- the expression 'ds_${0..1}' enumeration is the logical data source name of the readwrite-splitting configuration -->
  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. <!-- data encrypt configuration -->
  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. <!-- datasource configuration -->
  91. <!-- the element data-source-names's value is all of the datasource name -->
  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>