菜鸟一个,第一次尝试分库分表,参看网上的资料,写了一个简单的demo。
直接上代码
一、shardingsphere是什么?Apache ShardingSphere 是一套开源的分布式数据库解决方案组成的生态圈,它由 JDBC、Proxy 和 Sidecar(规划中)这 3 款既能够独立部署,又支持混合部署配合使用的产品组成。 它们均提供标准化的数据水平扩展、分布式事务和分布式治理等功能,可适用于如 Java 同构、异构语言、云原生等各种多样化的应用场景。
Apache ShardingSphere 旨在充分合理地在分布式的场景下利用关系型数据库的计算和存储能力,而并非实现一个全新的关系型数据库。 关系型数据库当今依然占有巨大市场份额,是企业核心系统的基石,未来也难于撼动,我们更加注重在原有基础上提供增量,而非颠覆。
Apache ShardingSphere 5.x 版本开始致力于可插拔架构,项目的功能组件能够灵活的以可插拔的方式进行扩展。 目前,数据分片、读写分离、数据加密、影子库压测等功能,以及 MySQL、PostgreSQL、SQLServer、Oracle 等 SQL 与协议的支持,均通过插件的方式织入项目。 开发者能够像使用积木一样定制属于自己的独特系统。Apache ShardingSphere 目前已提供数十个 SPI 作为系统的扩展点,仍在不断增加中。
ShardingSphere 已于2020年4月16日成为 Apache 软件基金会的顶级项目
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、使用步骤 1. 单库分表:创建一个springboot项目。
pom.xml:
2.创建数据库,表4.0.0 org.springframework.boot spring-boot-starter-parent 2.5.5 com.lhj testSharding 0.0.1-SNAPSHOT testSharding Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test com.alibaba druid 1.2.6 org.apache.shardingsphere sharding-jdbc-spring-boot-starter 4.1.1 org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
CREATE TABLE `t_user_0` ( `id` bigint(20) NOT NULL, `name` varchar(64) DEFAULT NULL COMMENT '名称', `sex` tinyint(1) DEFAULT NULL COMMENT '性别', `phone` varchar(32) DEFAULT NULL COMMENT '电话', `email` varchar(32) DEFAULT NULL COMMENT '邮箱', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATe CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `t_user_1` ( `id` bigint(20) NOT NULL, `name` varchar(64) DEFAULT NULL COMMENT '名称', `sex` tinyint(1) DEFAULT NULL COMMENT '性别', `phone` varchar(32) DEFAULT NULL COMMENT '电话', `email` varchar(32) DEFAULT NULL COMMENT '邮箱', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;3.实体类
package com.lhj.testsharding.entity;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class User implements Serializable {
private Long id;
private String name;
private String phone;
private String email;
private Date createTime;
private Integer sex;
}
4.Mapper
package com.lhj.testsharding.mapper;
import com.lhj.testsharding.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper {
public void save(User user);
public User get(Long id);
}
5.Service
package com.lhj.testsharding.service;
import com.lhj.testsharding.entity.User;
public interface UserService {
void save(User user);
User get(Long id);
}
5.ServiceImpl
package com.lhj.testsharding.service.Impl;
import com.lhj.testsharding.entity.User;
import com.lhj.testsharding.mapper.UserMapper;
import com.lhj.testsharding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public void save(User user) {
userMapper.save(user);
}
@Override
public User get(Long id) {
return userMapper.get(id);
}
}
6.Controller
package com.lhj.testsharding.controller;
import com.lhj.testsharding.entity.User;
import com.lhj.testsharding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/user/save")
public String save() {
for (int i = 0; i <10 ; i++) {
User user=new User();
user.setName("小明"+i);
user.setCreateTime(new Date());
user.setSex(i%2==0?1:2);
user.setPhone("1888888"+i);
user.setEmail("188888@"+i+".com");
userService.save(user);
}
return "success";
}
@RequestMapping("/user/get/{id}")
public User get(@PathVariable Long id) {
User user = userService.get(id);
System.out.println(user.getId());
return user;
}
}
7.mapper.xml
8.application.ymlINSERT INTO t_user(name,phone,email,sex) VALUES ( #{name},#{phone},#{email},#{sex} )
server:
port: 8080
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*Mapper.xml
#sharding 分片策略
spring:
# 配置数据源,给数据源起名是ds0,ds1...此处可配置多数据源
shardingsphere:
datasource:
names: ds0
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名
username: 用户名
password: 密码
sharding:
tables:
t_user:
# 配置表的分布,表的策略
actual-data-nodes: ds0.t_user_$->{0..1}
# 指定t_user表 主键id 生成策略为 SNOWFLAKE
key-generator:
column: id
type: SNOWFLAKE
# 指定分片策略 按照id字段添加到表
table-strategy:
inline:
sharding-column: id
#根据ID字段是否为奇数偶数然后添加到表
algorithm-expression: t_user_$->{id % 2}
# 打开sql输出日志
props:
sql:
show: true
9.项目结构
10.测试
使用postman接口测试工具,调用接口。
查看数据库数据:
t_user_0
t_user_1
到此单库分表已经完成,希望对大家有所帮助。
11. 分库分表:
只需要在创建一个数据库。更改一下application.yml
数据库图:
application.yml
server:
port: 8080
mybatis:
configuration:
map-underscore-to-camel-case: true
mapper-locations: classpath:mapper/*Mapper.xml
#sharding 分片策略
spring:
# 配置数据源,给数据源起名是ds0,ds1...此处可配置多数据源
shardingsphere:
datasource:
names: ds0,ds1
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名
username: 用户名
password: 密码
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/数据库名
username: 用户名
password: 密码
sharding:
tables:
t_user:
# 指定t_user表 主键id 生成策略为 SNOWFLAKE
key-generator:
column: id
type: SNOWFLAKE
# 配置表的分布,表的策略
actual-data-nodes: ds$->{0..1}.t_user_$->{0..1}
# 指定分片策略 按照sex字段添加到库
database-strategy: #分库策略
inline: #行表达式
sharding-column: sex
algorithm-expression: ds$->{sex % 2}
# 指定分片策略 按照id字段添加到表
table-strategy:
inline:
sharding-column: id
algorithm-expression: t_user_$->{id % 2}
# 打开sql输出日志
props:
sql:
show: true
总结
以上就是demo的全部内容,第一次尝试分库分表,若有不足之处,请指点出来,谢谢大家的支持。 


