ShardingSphere jdbc集成多数据源的实现步骤
作者:读钓 时间:2023-11-25 07:54:56
最近有个项目的几张表,数量级在千万以上,技术栈是SpringBoot+Mybatis-plus+MySQL。如果使用单表,在进行查询操作,非常耗时,经过一番调研,决定使用分表中间件:ShardingSphere。
ShardingSphere今年4月份成为了 Apache 软件基金会的顶级项目,目前支持数据分片、读写分离、多数据副本、数据加密、影子库压测等功能,同时兼容多种数据库,通过可插拔架构,理想情况下,可以做到对业务代码无感知。
ShardingSphere下有两款成熟的产品:sharding jdbc和sharding proxy
sharding jdbc:可理解为增强版的 JDBC 驱动;
sharding proxy:透明化的数据库代理端,可以看做是一个虚拟的数据库服务。
集成sharding jdbc
仅是集成sharding jdbc还是很简单的,为了更好的理解,这里以订单表为例。
1. 引入依赖
<properties>
<sharding-sphere.version>4.1.0</sharding-sphere.version>
</properties>
<!-- 分库分表:https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-boot-starter -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
2. 配置分表规则
spring:
shardingsphere:
datasource:
names: sharding-order-system
sharding-order-system:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/order_system?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: root
props:
# 日志显示SQL
sql.show: true
sharding:
tables:
# 订单表 分表:20
order:
# 真实表 order_0
actualDataNodes: sharding-order-system.order_$->{0..19}
# 分库策略
databaseStrategy:
none:
# 分表策略
tableStrategy:
inline:
shardingColumn: order_key
# 分片算法行表达式,需符合groovy语法 '& Integer.MAX_VALUE' 位运算使hash值为正数
algorithmExpression: order_$->{(order_key.hashCode() & Integer.MAX_VALUE) % 20}
问题
上面虽然完成了对订单表(order)的分表,但是sharding jdbc对一些语法不支持,官方的文档里说的比较笼统,如下图:
像insert into ... select
这些语法是不支持的,**而且对于没有涉及到分表的语句,也有同样的限制。**例如,项目里有个SQL:insert into user_temp select * from user;
在集成了sharding jdbc后,即使user表没有配置分表,执行该SQL也会报错。
官方的问答中提到,使用多数据源分别处理分片和不分片的情况,对分表的SQL使用sharding jdbc数据源,对不涉及到分表的SQL,使用普通数据源。
集成多数据源
我们项目中使用到了baomidou团队开源的mybatis-plus,其团队还开源了一个多数据源的组件:dynamic-datasource-spring-boot-starter,集成后,使用@DS
注解就可以切换数据源,非常方便。
1. 引入依赖
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
2. 多数据源配置
核心思路是将sharding jdbc数据源,加入到多数据源中。
/**
* 动态数据源配置:
*
* 使用{@link com.baomidou.dynamic.datasource.annotation.DS}注解,切换数据源
*
* <code>@DS(DataSourceConfiguration.SHARDING_DATA_SOURCE_NAME)</code>
*
* @author songyinyin
* @date 2020/7/27 15:19
*/
@Configuration
@AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class,
SpringBootConfiguration.class})
public class DataSourceConfiguration {
/**
* 分表数据源名称
*/
private static final String SHARDING_DATA_SOURCE_NAME = "gits_sharding";
/**
* 动态数据源配置项
*/
@Autowired
private DynamicDataSourceProperties properties;
/**
* shardingjdbc有四种数据源,需要根据业务注入不同的数据源
*
* <p>1. 未使用分片, 脱敏的名称(默认): shardingDataSource;
* <p>2. 主从数据源: masterSlaveDataSource;
* <p>3. 脱敏数据源:encryptDataSource;
* <p>4. 影子数据源:shadowDataSource
*
*/
@Lazy
@Resource(name = "shardingDataSource")
AbstractDataSourceAdapter shardingDataSource;
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
return new AbstractDataSourceProvider() {
@Override
public Map<String, DataSource> loadDataSources() {
Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
// 将 shardingjdbc 管理的数据源也交给动态数据源管理
dataSourceMap.put(SHARDING_DATA_SOURCE_NAME, shardingDataSource);
return dataSourceMap;
}
};
}
/**
* 将动态数据源设置为首选的
* 当spring存在多个数据源时, 自动注入的是首选的对象
* 设置为主要的数据源之后,就可以支持shardingjdbc原生的配置方式了
*
* @return
*/
@Primary
@Bean
public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setProvider(dynamicDataSourceProvider);
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
}
sharding jdbc有四种数据源:
未使用分片, 脱敏的名称(默认):shardingDataSource;
主从数据源: masterSlaveDataSource;
脱敏数据源:encryptDataSource;
影子数据源:shadowDataSource
需要需要根据不同的场景,注入不同的数据源,本文以分表举例,所以将shardingDataSource放到了多数据源(dataSourceMap)中。
3. 增加多数据源配置
在第2步,我们指定了shardingsphere数据源的名称为:gits_sharding
spring:
datasource:
# 动态数据源配置
dynamic:
datasource:
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/gits?useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true
username: root
password: root
# 指定默认数据源名称
primary: master
# 分表配置
shardingsphere:
datasource:
names: sharding-order-system
sharding-order-system:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://172.20.20.19:3306/order_system?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: root
props:
# 日志显示SQL
sql.show: true
sharding:
tables:
# 订单表 分表:20
order:
# 真实表 order_0
actualDataNodes: sharding-order-system.order_$->{0..19}
# 分库策略
databaseStrategy:
none:
# 分表策略
tableStrategy:
inline:
shardingColumn: order_key
# 分片算法行表达式,需符合groovy语法 '& Integer.MAX_VALUE' 位运算使hash值为正数
algorithmExpression: order_$->{(order_key.hashCode() & Integer.MAX_VALUE) % 20}
这里将默认数据源指定为了普通数据源。
4. 使用
在需要分表的service方法上加上@DS("gits_sharding")
,即可切换为sharding jdbc数据源。
@Service
@Slf4j
public class OrderServiceImpl extends OrderService {
@Override
@DS("gits_sharding")
public List<Order> getOrderByUser(OrderQueryDTO dto) throws Exception {
// 省略若干业务代码
...
}
}
来源:https://blog.csdn.net/youthsong/article/details/107743936
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android自定义弹出框dialog效果
Java 中很好用的数据结构EnumSet
![](https://img.aspxhome.com/file/2023/5/64395_0s.webp)
PowerManagerService之手动灭屏流程示例分析
SpringBoot实现二维码扫码登录的原理及项目实践
![](https://img.aspxhome.com/file/2023/5/77995_0s.png)
Android 获得屏幕宽高的三种方式
![](https://img.aspxhome.com/file/2023/4/113304_0s.gif)
java多线程CyclicBarrier的使用案例,让线程起步走
Java实现学生管理系统(控制台版本)
Android图表库HelloChart绘制多折线图
![](https://img.aspxhome.com/file/2023/8/122238_0s.jpg)
SpringBoot项目没有把依赖的jar包一起打包的问题解决
Android界面刷新的方法分享
android应用开发之spinner控件的简单使用
![](https://img.aspxhome.com/file/2023/7/139127_0s.png)
Spring Cloud 配置中心内容加密的配置方法
浅谈如何在项目中使用Spring Cloud Alibaba Sentinel组件
![](https://img.aspxhome.com/file/2023/8/96088_0s.png)
Spring集成MyBatis 及Aop分页的实现代码
![](https://img.aspxhome.com/file/2023/5/64785_0s.png)
详解maven的setting配置文件中mirror和repository的区别
![](https://img.aspxhome.com/file/2023/2/83112_0s.jpg)
SpringBoot详解如何进行整合Druid数据源
![](https://img.aspxhome.com/file/2023/0/106080_0s.png)
Go Java算法最大单词长度乘积示例详解
实现qq中按返回键返回桌面不退出程序的实例
带你一文了解C#中的Expression
![](https://img.aspxhome.com/file/2023/4/129634_0s.png)
java多线程抓取铃声多多官网的铃声数据
![](https://img.aspxhome.com/file/2023/8/85868_0s.jpg)