shared-jdbc简介 Sharding-JDBC是当当应用框架ddframe中,从关系型数据库模块dd-rdb中分离出来的数据库水平分片框架,实现透明化数据库分库分表访问。Sharding-JDBC是继dubbox和elastic-job之后,ddframe系列开源的第3个项目。 Sharding-JDBC直接封装JDBC协议,可以理解为增强版的JDBC驱动,旧代码迁移成本几乎为零。 Sharding-JDBC定位为轻量级java框架,使用客户端直连数据库,以jar包形式提供服务,无proxy代理层,无需额外部署,无其他依赖,DBA也无需改变原有的运维方式。 1.导入依赖 <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.0version> <scope>testscope> dependency>
<dependency> <groupId>io.shardingjdbcgroupId> <artifactId>sharding-jdbc-coreartifactId> <version>2.0.0version> dependency>
<dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>1.0.13version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.28version> dependency> dependencies> 2.表的创建语句 CREATE TABLE `t_order_x` ( `order_id` int(11) NOT NULL, `user_id` int(11) NOT NULL, PRIMARY KEY (`order_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
3.获取数据源 package com.irisian.sharedjdbc; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSource; import io.shardingjdbc.core.api.ShardingDataSourceFactory; import io.shardingjdbc.core.api.config.ShardingRuleConfiguration; import io.shardingjdbc.core.api.config.TableRuleConfiguration; import io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration; public class DateSourceUtils { public static DataSource getDataSource() throws SQLException {
// 配置真实数据源 Map
// 配置第一个数据源 DruidDataSource dataSource1 = new DruidDataSource(); dataSource1.setDriverClassName("com.mysql.jdbc.Driver"); dataSource1.setUrl("jdbc:mysql://localhost:3306/db0"); dataSource1.setUsername("root"); dataSource1.setPassword("123456"); // 将数据库放入到数据库map集合中 dataSourceMap.put("db0", dataSource1);
// 配置第二个数据源 DruidDataSource dataSource2 = new DruidDataSource(); dataSource2.setDriverClassName("com.mysql.jdbc.Driver"); dataSource2.setUrl("jdbc:mysql://localhost:3306/db1"); dataSource2.setUsername("root"); dataSource2.setPassword("123456"); // 将数据库放入到map集合中 dataSourceMap.put("db1", dataSource2);
// 配置Order表规则 TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration(); orderTableRuleConfig.setLogicTable("t_order"); //下面这两种是等价的,表示在两个库均匀分布 /** * db0 ├── t_order_0 └── t_order_1 db1 ├── t_order_0 └── t_order_1 */ orderTableRuleConfig.setActualDataNodes("db0.t_order_0, db0.t_order_1, db1.t_order_0, db1.t_order_1"); //orderTableRuleConfig.setActualDataNodes("db${0..1}.t_order_${0..1}");
// 配置分库策略 orderTableRuleConfig.setDatabaseShardingStrategyConfig( new InlineShardingStrategyConfiguration("user_id", "db${user_id % 2}"));
// 配置分表策略 orderTableRuleConfig.setTableShardingStrategyConfig( new InlineShardingStrategyConfiguration("order_id", "t_order_${order_id % 2}"));
// 配置分片规则 ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
// 获取数据源对象 DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), new Properties()); return dataSource;
}
} 4.操作数据库 package com.irisian.sharedjdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.sql.DataSource; public class Test { public static void main(String[] args) throws SQLException { DataSource dataSource = DateSourceUtils.getDataSource(); Connection conn = dataSource.getConnection(); String sql="insert into t_order(order_id,user_id) values(?,?)"; PreparedStatement prep = conn.prepareStatement(sql); prep.setInt(1, 3); prep.setInt(2, 1); prep.execute(); } } |