SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫分表的方案
SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫分表
介紹
實(shí)現(xiàn)億級數(shù)據(jù)量分庫分表的項(xiàng)目是一個挑戰(zhàn)性很高的任務(wù),下面是一個基于Spring Boot的簡單實(shí)現(xiàn)方案:
- 數(shù)據(jù)庫選擇:使用MySQL數(shù)據(jù)庫,因?yàn)镸ySQL在分庫分表方面有較成熟的解決方案。
- 分庫分表策略:可以采用水平分庫分表的策略,根據(jù)一定的規(guī)則將數(shù)據(jù)分散存儲在不同的數(shù)據(jù)庫和表中,例如可以根據(jù)用戶ID、訂單ID等進(jìn)行分片。
- 數(shù)據(jù)分片策略:可以采用基于雪花算法的分布式ID生成器來生成全局唯一的ID,確保數(shù)據(jù)在不同數(shù)據(jù)庫和表中的唯一性。
- 數(shù)據(jù)同步:考慮到數(shù)據(jù)分散存儲在不同的數(shù)據(jù)庫和表中,需要實(shí)現(xiàn)數(shù)據(jù)同步機(jī)制來保證數(shù)據(jù)的一致性,可以使用Canal等開源工具來實(shí)現(xiàn)MySQL數(shù)據(jù)的實(shí)時同步。
- 連接池優(yōu)化:在處理大量數(shù)據(jù)時,連接池的配置尤為重要,可以使用Druid等高性能的連接池來提升數(shù)據(jù)庫連接的效率。
- 緩存機(jī)制:考慮使用Redis等緩存工具來緩存熱點(diǎn)數(shù)據(jù),減輕數(shù)據(jù)庫的壓力,提升系統(tǒng)性能。
- 分布式事務(wù):在分庫分表的場景下,涉及到跨庫事務(wù),可以考慮使用分布式事務(wù)框架,如Seata等來保證事務(wù)的一致性。
監(jiān)控與調(diào)優(yōu):實(shí)時監(jiān)控?cái)?shù)據(jù)庫的性能指標(biāo),及時調(diào)整分片策略和數(shù)據(jù)庫配置,保證系統(tǒng)的穩(wěn)定性和性能。
引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--分庫分表-->
<!-- Sharding-JDBC -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>yaml配置
server:
port: 10086
spring:
shardingsphere:
# 數(shù)據(jù)源配置
datasource:
# 數(shù)據(jù)源名稱,多數(shù)據(jù)源以逗號分隔
names: db0,db1
db0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/ds0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: root
db1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/ds1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: root
# 分片規(guī)則配置
rules:
sharding:
# 分片算法配置
sharding-algorithms:
database-inline:
# 分片算法類型
type: INLINE
props:
# 分片算法的行表達(dá)式(算法自行定義,此處為方便演示效果)
algorithm-expression: db$->{age % 2}
table-inline:
# 分片算法類型
type: INLINE
props:
# 分片算法的行表達(dá)式
algorithm-expression: user_$->{age % 3}
tables:
# 邏輯表名稱
user:
# 行表達(dá)式標(biāo)識符可以使用 ${...} 或 $->{...},但前者與 Spring 本身的屬性文件占位符沖突,因此在 Spring 環(huán)境中使用行表達(dá)式標(biāo)識符建議使用 $->{...}
actual-data-nodes: db${0..1}.user_${0..2}
# 分庫策略
database-strategy:
standard:
# 分片列名稱
sharding-column: age
# 分片算法名稱
sharding-algorithm-name: database-inline
# 分表策略
table-strategy:
standard:
# 分片列名稱
sharding-column: age
# 分片算法名稱
sharding-algorithm-name: table-inline
# 屬性配置
props:
# 展示修改以后的sql語句
sql-show: trueDDL準(zhǔn)備
數(shù)據(jù)庫ds0
-- ds0.user_0 definition CREATE TABLE `user_0` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(32) NOT NULL COMMENT '姓名', `age` int NOT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='用戶表';
-- ds0.user_1 definition CREATE TABLE `user_1` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(32) NOT NULL COMMENT '姓名', `age` int NOT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='用戶表';
-- ds0.user_2 definition CREATE TABLE `user_2` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(32) NOT NULL COMMENT '姓名', `age` int NOT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='用戶表';
數(shù)據(jù)庫ds1
-- ds1.user_0 definition CREATE TABLE `user_0` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(32) NOT NULL COMMENT '姓名', `age` int NOT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='用戶表';
-- ds1.user_1 definition CREATE TABLE `user_1` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(32) NOT NULL COMMENT '姓名', `age` int NOT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='用戶表';
-- ds1.user_2 definition CREATE TABLE `user_2` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主鍵', `name` varchar(32) NOT NULL COMMENT '姓名', `age` int NOT NULL COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='用戶表';
entity
package com.kang.sharding.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("user")
public class User {
@TableId(value = "id",type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
// getter, setter, toString...
}cotroller
package com.kang.sharding.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.kang.sharding.entity.User;
import com.kang.sharding.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping ("add")
public boolean createUser(@RequestBody User user) {
return userService.save(user);
}
@PostMapping ("update")
public boolean updateByAge(@RequestBody User user){
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
// 分片數(shù)據(jù)不允許更新,否則會報(bào)錯
updateWrapper.eq(User::getAge,user.getAge()).set(User::getName,user.getName());
return userService.update(updateWrapper);
}
@GetMapping ("delete")
public boolean deleteUserByAge(@RequestParam("age") Integer age) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getAge,age);
return userService.remove(queryWrapper);
}
@GetMapping("/{age}")
public List<User> getUserByAge(@PathVariable Integer age) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getAge,age);
return userService.list(queryWrapper);
}
// 其他方法...
}service
package com.kang.sharding.service;
import com.kang.sharding.entity.User;
import com.kang.sharding.mapper.UserMapper;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@Service
public class UserService extends ServiceImpl<UserMapper, User> {
}Mapper
package com.kang.sharding.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kang.sharding.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}啟動類
package com.kang.sharding;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ShardingJdbcProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingJdbcProjectApplication.class, args);
}
}測試
添加


修改


查詢


刪除


總結(jié)
這只是個簡單的入門示例,后續(xù)深入研究
到此這篇關(guān)于SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫分表的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis-plus shardingsphere分庫分表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MybatisPlus攔截器如何實(shí)現(xiàn)數(shù)據(jù)表分表
- SpringBoot+MybatisPlus實(shí)現(xiàn)sharding-jdbc分庫分表的示例代碼
- Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶分庫分表實(shí)戰(zhàn)
- SQL數(shù)據(jù)分表Mybatis?Plus動態(tài)表名優(yōu)方案
- SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表
- springboot+mybatis-plus基于攔截器實(shí)現(xiàn)分表的示例代碼
- Mybatis-plus使用TableNameHandler分表詳解(附完整示例源碼)
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實(shí)現(xiàn)分庫分表功能
- MyBatis-Plus使用動態(tài)表名分表查詢的實(shí)現(xiàn)
相關(guān)文章
RSA加密算法java簡單實(shí)現(xiàn)方法(必看)
下面小編就為大家?guī)硪黄猂SA加密算法java簡單實(shí)現(xiàn)方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
springboot+mysql+mybatis實(shí)現(xiàn)控制臺打印sql
在Spring Boot中使用MyBatis與MySQL,并希望在控制臺打印SQL語句,可以通過配置MyBatis的日志級別來實(shí)現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01
springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶身份跳轉(zhuǎn)不同頁面
Spring?Security是一種基于Spring框架的安全技術(shù),用于實(shí)現(xiàn)身份驗(yàn)證和訪問控制,本文介紹了如何使用Spring?Security,結(jié)合session和redis來存儲用戶信息,并通過編寫特定的登錄處理類和Web配置,實(shí)現(xiàn)用戶登錄和注銷功能2024-09-09
SpringBoot最新定時任務(wù)的7種實(shí)現(xiàn)方案
在現(xiàn)代應(yīng)用中,定時任務(wù)是一個非常常見的需求,本文將通過7種方式講解如何在SpringBoot中實(shí)現(xiàn)定時任務(wù),包括使用@Scheduled注解、ScheduledExecutorService、Quartz、SpringTaskScheduler、Redis、XXL-JOB和Elastic-Job等,各有優(yōu)缺點(diǎn),選擇時應(yīng)根據(jù)實(shí)際需求進(jìn)行考慮2024-12-12
java中instanceof 關(guān)鍵字作用和實(shí)際用途詳解
這篇文章主要介紹了java中instanceof 關(guān)鍵字作用和實(shí)際用途,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
springboot不同環(huán)境使用不同配置文件打包方式
這篇文章主要介紹了springboot不同環(huán)境使用不同配置文件打包方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Java中List排序的三種實(shí)現(xiàn)方法實(shí)例
其實(shí)Java針對數(shù)組和List的排序都有實(shí)現(xiàn),對數(shù)組而言你可以直接使用Arrays.sort,對于List和Vector而言,你可以使用Collections.sort方法,下面這篇文章主要給大家介紹了關(guān)于Java中List排序的三種實(shí)現(xiàn)方法,需要的朋友可以參考下2021-12-12

