HA

Background

The Spring namespace configuration method, applicable to traditional Spring projects, configures highly availability rules by means of namespace XML configuration files, and Spring completes the creation and management of ShardingSphereDataSource objects.

Parameters Explained

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/database-discovery/database-discovery-5.1.1.xsd

<database-discovery:rule />

NameTypeDescription
idPropertySpring Bean Id
data-source-rule (+)TagConfiguration of data source rules
discovery-heartbeat (+)TagConfiguration of heartbeat rules detection

<database-discovery:data-source-rule />

NameTypeDescription
idPropertyData source rules name
data-source-namesPropertyData source name,multiple datasources are divided by comma,such as:ds_0, ds_1
discovery-heartbeat-namePropertyDetect heartbeat name
discovery-type-namePropertytype name of database discovery

<database-discovery:discovery-heartbeat />

NameTypeDescription
idPropertyheartbeat listen name
props标签property configuration of heartbeat listen,cron expression of keep-alive-cron property configuration,such as:‘0/5 ?’

<database-discovery:discovery-type />

NameTypeDescription
idPropertyType name of database discovery
typePropertyDatabase discovery type,such as:MySQL.MGR
props (?)TagConfiguration of database discovery type,such as group-name property configuration of MGR

Operating Procedures

1. Introduce Maven dependency

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId>
  4. <version>${latest.release.version}</version>
  5. </dependency>

Configuration Example

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:cluster="http://shardingsphere.apache.org/schema/shardingsphere/mode-repository/cluster"
  4. xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
  5. xmlns:database-discovery="http://shardingsphere.apache.org/schema/shardingsphere/database-discovery"
  6. xmlns:readwrite-splitting="http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://shardingsphere.apache.org/schema/shardingsphere/database-discovery
  10. http://shardingsphere.apache.org/schema/shardingsphere/database-discovery/database-discovery.xsd
  11. http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting
  12. http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting/readwrite-splitting.xsd
  13. http://shardingsphere.apache.org/schema/shardingsphere/mode-repository/cluster
  14. http://shardingsphere.apache.org/schema/shardingsphere/mode-repository/cluster/repository.xsd
  15. http://shardingsphere.apache.org/schema/shardingsphere/datasource
  16. http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
  17. ">
  18. <bean id="ds_0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  19. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  20. <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:33306/primary_demo_ds?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
  21. <property name="username" value="root" />
  22. <property name="password" value="" />
  23. </bean>
  24. <bean id="ds_1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  25. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  26. <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:33307/primary_demo_ds?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8" />
  27. <property name="username" value="root" />
  28. <property name="password" value="" />
  29. </bean>
  30. <bean id="ds_2" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  31. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  32. <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:33308/primary_demo_ds?useSSL=false"/>
  33. <property name="username" value="root"/>
  34. <property name="password" value=""/>
  35. </bean>
  36. <cluster:repository id="clusterRepository" type="ZooKeeper" namespace="governance" server-lists="localhost:2181">
  37. <props>
  38. <prop key="max-retries">3</prop>
  39. <prop key="operation-timeout-milliseconds">3000</prop>
  40. </props>
  41. </cluster:repository>
  42. <readwrite-splitting:rule id="readWriteSplittingRule">
  43. <readwrite-splitting:data-source-rule id="replica_ds">
  44. <readwrite-splitting:dynamic-strategy id="dynamicStrategy" auto-aware-data-source-name="readwrite_ds" />
  45. </readwrite-splitting:data-source-rule>
  46. </readwrite-splitting:rule>
  47. <database-discovery:rule id="mgrDatabaseDiscoveryRule">
  48. <database-discovery:data-source-rule id="readwrite_ds" data-source-names="ds_0,ds_1,ds_2" discovery-heartbeat-name="mgr-heartbeat" discovery-type-name="mgr" />
  49. <database-discovery:discovery-heartbeat id="mgr-heartbeat">
  50. <props>
  51. <prop key="keep-alive-cron" >0/5 * * * * ?</prop>
  52. </props>
  53. </database-discovery:discovery-heartbeat>
  54. </database-discovery:rule>
  55. <database-discovery:discovery-type id="mgr" type="MySQL.MGR">
  56. <props>
  57. <prop key="group-name">558edd3c-02ec-11ea-9bb3-080027e39bd2</prop>
  58. </props>
  59. </database-discovery:discovery-type>
  60. <shardingsphere:data-source id="databaseDiscoveryDataSource" schema-name="database-discovery-db" data-source-names="ds_0, ds_1, ds_2" rule-refs="readWriteSplittingRule, mgrDatabaseDiscoveryRule">
  61. <shardingsphere:mode repository-ref="clusterRepository" type="Cluster" />
  62. </shardingsphere:data-source>
  63. </beans>