读写分离

不使用Spring

引入Maven依赖

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>sharding-jdbc-core</artifactId>
  4. <version>${sharding-sphere.version}</version>
  5. </dependency>

基于Java编码的规则配置

  1. // 配置真实数据源
  2. Map<String, DataSource> dataSourceMap = new HashMap<>();
  3. // 配置主库
  4. BasicDataSource masterDataSource = new BasicDataSource();
  5. masterDataSource.setDriverClassName("com.mysql.jdbc.Driver");
  6. masterDataSource.setUrl("jdbc:mysql://localhost:3306/ds_master");
  7. masterDataSource.setUsername("root");
  8. masterDataSource.setPassword("");
  9. dataSourceMap.put("ds_master", masterDataSource);
  10. // 配置第一个从库
  11. BasicDataSource slaveDataSource1 = new BasicDataSource();
  12. slaveDataSource1.setDriverClassName("com.mysql.jdbc.Driver");
  13. slaveDataSource1.setUrl("jdbc:mysql://localhost:3306/ds_slave0");
  14. slaveDataSource1.setUsername("root");
  15. slaveDataSource1.setPassword("");
  16. dataSourceMap.put("ds_slave0", slaveDataSource1);
  17. // 配置第二个从库
  18. BasicDataSource slaveDataSource2 = new BasicDataSource();
  19. slaveDataSource2.setDriverClassName("com.mysql.jdbc.Driver");
  20. slaveDataSource2.setUrl("jdbc:mysql://localhost:3306/ds_slave1");
  21. slaveDataSource2.setUsername("root");
  22. slaveDataSource2.setPassword("");
  23. dataSourceMap.put("ds_slave1", slaveDataSource2);
  24. // 配置读写分离规则
  25. MasterSlaveRuleConfiguration masterSlaveRuleConfig = new MasterSlaveRuleConfiguration("ds_master_slave", "ds_master", Arrays.asList("ds_slave0", "ds_slave1"));
  26. // 获取数据源对象
  27. DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(dataSourceMap, masterSlaveRuleConfig, new Properties());

基于Yaml的规则配置

或通过Yaml方式配置,与以上配置等价:

  1. dataSources:
  2. ds_master: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_master
  5. username: root
  6. password:
  7. ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_slave0
  10. username: root
  11. password:
  12. ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
  13. driverClassName: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://localhost:3306/ds_slave1
  15. username: root
  16. password:
  17. masterSlaveRule:
  18. name: ds_ms
  19. masterDataSourceName: ds_master
  20. slaveDataSourceNames: [ds_slave0, ds_slave1]
  21. props:
  22. sql.show: true
  1. DataSource dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);

使用原生JDBC

通过YamlMasterSlaveDataSourceFactory工厂和规则配置对象获取MasterSlaveDataSource,MasterSlaveDataSource实现自JDBC的标准接口DataSource。然后可通过DataSource选择使用原生JDBC开发,或者使用JPA, MyBatis等ORM工具。 以JDBC原生实现为例:

  1. DataSource dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);
  2. String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
  3. try (
  4. Connection conn = dataSource.getConnection();
  5. PreparedStatement preparedStatement = conn.prepareStatement(sql)) {
  6. preparedStatement.setInt(1, 10);
  7. preparedStatement.setInt(2, 1001);
  8. try (ResultSet rs = preparedStatement.executeQuery()) {
  9. while(rs.next()) {
  10. System.out.println(rs.getInt(1));
  11. System.out.println(rs.getInt(2));
  12. }
  13. }
  14. }

使用Spring

引入Maven依赖

  1. <!-- for spring boot -->
  2. <dependency>
  3. <groupId>org.apache.shardingsphere</groupId>
  4. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  5. <version>${sharding-sphere.version}</version>
  6. </dependency>
  7. <!-- for spring namespace -->
  8. <dependency>
  9. <groupId>org.apache.shardingsphere</groupId>
  10. <artifactId>sharding-jdbc-spring-namespace</artifactId>
  11. <version>${sharding-sphere.version}</version>
  12. </dependency>

基于Spring boot的规则配置

  1. spring.shardingsphere.datasource.names=master,slave0,slave1
  2. spring.shardingsphere.datasource.master.type=org.apache.commons.dbcp.BasicDataSource
  3. spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
  4. spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/master
  5. spring.shardingsphere.datasource.master.username=root
  6. spring.shardingsphere.datasource.master.password=
  7. spring.shardingsphere.datasource.slave0.type=org.apache.commons.dbcp.BasicDataSource
  8. spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver
  9. spring.shardingsphere.datasource.slave0.url=jdbc:mysql://localhost:3306/slave0
  10. spring.shardingsphere.datasource.slave0.username=root
  11. spring.shardingsphere.datasource.slave0.password=
  12. spring.shardingsphere.datasource.slave1.type=org.apache.commons.dbcp.BasicDataSource
  13. spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
  14. spring.shardingsphere.datasource.slave1.url=jdbc:mysql://localhost:3306/slave1
  15. spring.shardingsphere.datasource.slave1.username=root
  16. spring.shardingsphere.datasource.slave1.password=
  17. spring.shardingsphere.masterslave.name=ms
  18. spring.shardingsphere.masterslave.master-data-source-name=master
  19. spring.shardingsphere.masterslave.slave-data-source-names=slave0,slave1
  20. spring.shardingsphere.props.sql.show=true

基于Spring boot + JNDI的规则配置

如果您计划使用Spring boot + JNDI的方式,在应用容器(如Tomcat)中使用Sharding-JDBC时,可使用spring.shardingsphere.datasource.${datasourceName}.jndiName来代替数据源的一系列配置。 如:

  1. spring.shardingsphere.datasource.names=master,slave0,slave1
  2. spring.shardingsphere.datasource.master.jndi-name=java:comp/env/jdbc/master
  3. spring.shardingsphere.datasource.slave0.jndi-name=jdbc/slave0
  4. spring.shardingsphere.datasource.slave1.jndi-name=jdbc/slave1
  5. spring.shardingsphere.masterslave.name=ms
  6. spring.shardingsphere.masterslave.master-data-source-name=master
  7. spring.shardingsphere.masterslave.slave-data-source-names=slave0,slave1
  8. spring.shardingsphere.props.sql.show=true

基于Spring命名空间的规则配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:master-slave="http://shardingsphere.apache.org/schema/shardingsphere/masterslave"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://shardingsphere.apache.org/schema/shardingsphere/masterslave
  8. http://shardingsphere.apache.org/schema/shardingsphere/masterslave/master-slave.xsd
  9. ">
  10. <bean id="ds_master" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  11. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  12. <property name="url" value="jdbc:mysql://localhost:3306/ds_master" />
  13. <property name="username" value="root" />
  14. <property name="password" value="" />
  15. </bean>
  16. <bean id="ds_slave0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  17. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  18. <property name="url" value="jdbc:mysql://localhost:3306/ds_slave0" />
  19. <property name="username" value="root" />
  20. <property name="password" value="" />
  21. </bean>
  22. <bean id="ds_slave1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  23. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  24. <property name="url" value="jdbc:mysql://localhost:3306/ds_slave1" />
  25. <property name="username" value="root" />
  26. <property name="password" value="" />
  27. </bean>
  28. <master-slave:data-source id="masterSlaveDataSource" master-data-source-name="ds_master" slave-data-source-names="ds_slave0, ds_slave1" >
  29. <master-slave:props>
  30. <prop key="sql.show">true</prop>
  31. <prop key="executor.size">10</prop>
  32. <prop key="foo">bar</prop>
  33. </master-slave:props>
  34. </master-slave:data-source>
  35. </beans>

在Spring中使用DataSource

直接通过注入的方式即可使用DataSource,或者将DataSource配置在JPA、Hibernate或MyBatis中使用。

  1. @Resource
  2. private DataSource dataSource;

更多的详细配置请参考配置手册