定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务。 它使用客户端直连数据库,以 jar 包形式提供服务,无需额外部署和依赖,可理解为增强版的 JDBC 驱动,完全兼容 JDBC 和各种 ORM 框架。
Java API
yaml
properties
spring命名空间
MyCat是服务端的代理,Sharding-Jdbc是客户端代理
实际开发中如果企业有DBA建议使用MyCat,都是开发人员建议使用sharding-jdbc
MyCat不支持在一个库内进行水平分表,而sharding-jdbc支持在同一个数据库中进行水平分表
数据节点:存储数据的MySQL节点
绑定表:相当于MyCat中的子表
广播表:相当于MyCat中的全局表
两个数据库分别建两个表
CREATE TABLE `order_info_1` (
`id` int(11) NOT NULL,
`order_amount` decimal(10,2) DEFAULT NULL,
`order_status` int(255) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `order_info_2` (
`id` int(11) NOT NULL,
`order_amount` decimal(10,2) DEFAULT NULL,
`order_status` int(255) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<dependency>
<groupId>org.apache.shardingspheregroupId>
<artifactId>sharding-jdbc-spring-boot-starterartifactId>
<version>4.0.0-RC2version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.7.RELEASEversion>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.16version>
dependency>
<dependency>
<groupId>com.zaxxergroupId>
<artifactId>HikariCPartifactId>
<version>2.6.1version>
<exclusions>
<exclusion>
<artifactId>slf4j-apiartifactId>
<groupId>org.slf4jgroupId>
exclusion>
exclusions>
dependency>
# 配置数据源
spring.shardingsphere.datasource.names=ds0,ds1
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbcUrl=jdbc:mysql://192.168.56.132:3306/shard_order
spring.shardingsphere.datasource.ds0.username=guanzc
spring.shardingsphere.datasource.ds0.password=123456
# 数据源
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbcUrl=jdbc:mysql://192.168.56.134:3306/shard_order
spring.shardingsphere.datasource.ds1.username=guanzc
spring.shardingsphere.datasource.ds1.password=123456
# 具体的分片规则,基于数据节点
spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{0..1}.order_info_$->{1..2}
# 分库的规则
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 分表的规则
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{user_id % 2 + 1}
id
奇偶数分库,user_id
奇偶分表@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void contextLoads() {
String sql = "insert into order_info(id,order_amount,order_status,user_id) values(1,23.12,1,2)";
int i = jdbcTemplate.update(sql);
System.out.println("新增数据:"+i);
sql ="select * from order_info";
List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
System.out.println("条数:"+result.size());
}
按照分库分表规则, 执行插入sql 数据会保存到,ds1
的 order_info_1
表中。
字典表,在每个数据库中都需要配置
CREATE TABLE `province_info` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
spring.shardingsphere.sharding.broadcast-tables=province_info
@Test
public void testBroadTables(){
String sql = "insert into province_info(id, name) values(1, '北京')";
int i = jdbcTemplate.update(sql);
System.out.println("插入条数:"+i);
//查询
sql ="select * from province_info";
List<Map<String, Object>> result = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : result){
System.out.println(map.get("id")+"-------------- "+map.get("name"));
}
}
父子表
分别创建 order_info_1、2
的绑定表 order_item_1、2
CREATE TABLE `order_item_1` (
`id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `order_item_2` (
`id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`order_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#绑定表-分片规则
spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=ds$->{0..1}.order_item_${1..2}
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.algorithm-expression=ds$->{order_id % 2}
#绑定表-分表规则
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.algorithm-expression=order_item_$->{id % 2 + 1}
# 配置绑定表
spring.shardingsphere.sharding.binding-tables=order_info,order_item
@Test
void bangding(){
String sql = "insert into order_item(id, product_name, order_id, user_id) values(1, 'HUAWEI',2,2),(2, 'HUAWEI',3,3)";
int i = jdbcTemplate.update(sql);
System.out.println("插入条数:"+i);
}
配置,至少需要配置三个服务器,两主一从。配置按照两主两从。
# 指定主从的配置节点
spring.shardingsphere.datasource.names=master0,master0slave0,master1,master1slave0
数据源配置...
# 读写分离主从关系绑定
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave0
spring.shardingsphere.sharding.master-slave-rules.ds0.load-balance-algorithm-type=round_robin
spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave0
spring.shardingsphere.sharding.master-slave-rules.ds1.load-balance-algorithm-type=random
@Test
void contextLoads() {
String sql = "insert into order_info(id,order_amount,order_status,user_id) values(1,243.18,1,2)";
int i = jdbcTemplate.update(sql);
System.out.println("新增数据:"+i);
}