作为轻量级java框架,sharding JDBC在Java的jdbc层提供了额外的服务,可以理解为增强版的jdbc驱动。其中,分库分表的操作是其中的重要一环,接下来就跟随我来看一看,这一操作如何进行。
环境准备
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version></parent>
<properties>
<java.version>1.8</java.version>
<sharding.version>3.1.0</sharding.version></properties>
<dependencies>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${sharding.version}</version>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency></dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins></build>
domain
// 建立domain@Setter@Getter@ToString@NoArgsConstructor@AllArgsConstructorpublic class Employee {
private Long id;
private String name;}
配置类
@SpringBootApplication@MapperScan("cn.wolfcode.sharding.mapper")public class ShardingApplication { }
分库分表
案例模型
把数据分别存放在两台服务器的两个数据库中表,通过分片算法来决定当前的数据存放在哪个数据库的哪个表中,由于一个连接池只能连接一个特定的数据库,所以这里需要创建多个连接池对象
建表
-- 分别在2台服务器中建立数据库sharding,并且建表employee_0和employee_1CREATE TABLE employee_0
(id
bigint(20) PRIMARY KEY AUTO_INCREMENT,name
varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ###################################CREATE TABLE employee_1
(id
bigint(20) PRIMARY KEY AUTO_INCREMENT,name
varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
application.properties
sharding.jdbc.datasource.names=db0,db1
sharding.jdbc.datasource.db0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db0.url=jdbc:mysql://db0Ip:port/sharing
sharding.jdbc.datasource.db0.username=xxx
sharding.jdbc.datasource.db0.password=xxx
sharding.jdbc.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db1.url=jdbc:mysql://db1Ip:port/sharing
sharding.jdbc.datasource.db1.username=xxx
sharding.jdbc.datasource.db1.password=xxx
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=db$->{id % 2}
sharding.jdbc.config.sharding.binding-tables=employee
sharding.jdbc.config.sharding.tables.employee.actual-data-nodes=db$->{0..1}.employee$->{0..1}
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.algorithm-expression=employee$->{id % 2}
sharding.jdbc.config.sharding.tables.employee.key-generator-column-name=id
sharding.jdbc.config.props.sql.show=true
mapper
/**
底层会根据分片规则,把我们写的逻辑表改写为数据库中的真实表
/@Mapperpublic interface EmployeeMapper {
@Select("select from employee")
List<Employee> selectAll();
@Insert("insert into employee (name) values (#{name})")
void inser(Employee entity);}
测试
@RunWith(SpringRunner.class)@SpringBootTest(classes=ShardingApplication.class)public class ShardingApplicationTests {
@Autowired
private EmployeeMapper employeeMapper;
@Test
public void save() {
for (int i = 0; i < 10; i++) {
Employee employee = new Employee();
employee.setName("xx"+i);
employeeMapper.inser(employee);
}
}
@Test
public void list() {
employeeMapper.selectAll().forEach(System.out::println);
}}
优缺点
拆分后单表数据量比较小,单表大数据被拆分,解决了单表大数据访问问题
分表以什么切分如果弄的不好,导致多次查询,而且有时候要跨库操作,甚至导致join无法使用,对排序分组等有性能影响
之前的原子操作被拆分成多个操作,事务处理变得复杂
多个DB维护成本增加
分库分表也仅仅是shardingJDBC中小小的一部分,还有很多内容需要大家继续研究,如果你对此感兴趣的话,不妨关注我,接下来我还会发布更多的相关教程供大家学习,如果遇到问题我也会尽力帮助你。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。