1.简介
ShardingSphere-JDBC目前是5.0.0-alpha版本了,我这里以4.x版本演示其入门级的使用方法。此篇文章演示一个纯Java Api方式的使用Demo和整合SpringBoot的使用Demo。Demo是参考官方文档编写的。
2.基于Java Api方式的使用
ShardingSphere-JDBC的jar包版本很多,考虑到官方文档在写的时候也是基于 4.0.x版本写的文档,所以,我这里使用 4.0.1 版本(4.0版本的最后一个小升级版本)
1.依赖引入
<sharding-sphere.version>4.0.1</sharding-sphere.version>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
2.数据库与表的准备
创建2个数据库ds0,ds1。在数据库中各创建2张表,创表语句如下:
create table t_order(
user_id int(11),
order_id int(11) PRIMARY KEY
);
最后创建的结果是这样:
3.编写代码
package org.example;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws SQLException {
// 配置真实数据源
Map<String, DataSource> dataSourceMap = new HashMap<>();
// 配置第一个数据源
HikariDataSource dataSource1= new HikariDataSource();
dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/ds0");
dataSource1.setUsername("root");
dataSource1.setPassword("888888");
dataSourceMap.put("ds0", dataSource1);
// 配置第二个数据源
HikariDataSource dataSource2 = new HikariDataSource();
dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
dataSource2.setJdbcUrl("jdbc:mysql://localhost:3306/ds1");
dataSource2.setUsername("root");
dataSource2.setPassword("888888");
dataSourceMap.put("ds1", dataSource2);
// 配置Order表规则
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order","ds${0..1}.t_order${0..1}");
// 配置分库 + 分表策略
orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));
// 配置分片规则
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
// 省略配置order_item表规则...
// ...
// 获取数据源对象
DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("insert into t_order values(1,1),(1,2),(2,1),(2,2)");
int result = preparedStatement.executeUpdate();
System.out.println(result);
}
}
4.执行结果
上面的配置是:按照userId进行分库,orderId分表。userId%2 ==0
则数据进入ds0,否则进入ds1
之后,orderId%2==0则进入t_order0,否则进入t_order1
3.