- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章ShardingSphere jdbc集成多数据源的实现步骤由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
最近有个项目的几张表,数量级在千万以上,技术栈是SpringBoot+Mybatis-plus+MySQL。如果使用单表,在进行查询操作,非常耗时,经过一番调研,决定使用分表中间件:ShardingSphere.
ShardingSphere今年4月份成为了 Apache 软件基金会的顶级项目,目前支持数据分片、读写分离、多数据副本、数据加密、影子库压测等功能,同时兼容多种数据库,通过可插拔架构,理想情况下,可以做到对业务代码无感知.
ShardingSphere下有两款成熟的产品:sharding jdbc和sharding proxy 。
仅是集成sharding jdbc还是很简单的,为了更好的理解,这里以订单表为例.
1
2
3
4
5
6
7
8
9
10
11
|
<
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
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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
2
3
4
5
6
|
<!-- 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
>
|
核心思路是将sharding jdbc数据源,加入到多数据源中.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/**
* 动态数据源配置:
*
* 使用{@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放到了多数据源(dataSourceMap)中.
在第2步,我们指定了shardingsphere数据源的名称为:gits_sharding 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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}
|
这里将默认数据源指定为了普通数据源.
在需要分表的service方法上加上@DS("gits_sharding"),即可切换为sharding jdbc数据源.
1
2
3
4
5
6
7
8
9
10
11
|
@Service
@Slf4j
public
class
OrderServiceImpl
extends
OrderService {
@Override
@DS
(
"gits_sharding"
)
public
List<Order> getOrderByUser(OrderQueryDTO dto)
throws
Exception {
// 省略若干业务代码
...
}
}
|
sharding jdbc虽然是Apache的顶级项目,但也不是对有所SQL兼容,使用多数据源 + sharding jdbc则能跳过很多sharding jdbc的不足.
到此这篇关于ShardingSphere jdbc集成多数据源的实现步骤的文章就介绍到这了,更多相关ShardingSphere jdbc多数据源内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。
原文链接:https://blog.csdn.net/youthsong/article/details/107743936 。
最后此篇关于ShardingSphere jdbc集成多数据源的实现步骤的文章就讲到这里了,如果你想了解更多关于ShardingSphere jdbc集成多数据源的实现步骤的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
目录 集成sharding jdbc 1. 引入依赖 2. 配置分表规则 问题 集成多数据源
Apache ShardingSphere 是一个开源的分布式数据库,它还有一个用户和开发人员需要的生态系统,为之提供了定制和云原生的体验。 Apache ShardingSphere
谈到分库分表中间件时,我们自然而然的会想到 ShardingSphere-JDBC 。 这篇文章,我们聊聊 ShardingSphere-JDBC 相关知识点,并实战演示一番。
本文整理了Java中io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration类的一些代码示例,展示了YamlSharding
本文整理了Java中org.apache.shardingsphere.core.yaml.YamlAuthentication类的一些代码示例,展示了YamlAuthentication类的具体用法
本文整理了Java中org.apache.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration类的一些代码示例,展示了Yaml
本文整理了Java中org.apache.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration类的一些代码示例,展
本文整理了Java中org.apache.shardingsphere.orchestration.yaml.config.YamlOrchestrationConfiguration类的一些代码示例
本文整理了Java中org.apache.shardingsphere.core.yaml.sharding.YamlKeyGeneratorConfiguration类的一些代码示例,展示了Yaml
本文整理了Java中io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration.getMasterSlaveRul
本文整理了Java中io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration.getShardingRuleConfigur
本文整理了Java中org.apache.shardingsphere.core.yaml.YamlAuthentication.getUsername()方法的一些代码示例,展示了YamlAuthe
本文整理了Java中org.apache.shardingsphere.core.yaml.YamlAuthentication.getPassword()方法的一些代码示例,展示了YamlAuthe
本文整理了Java中org.apache.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration.getShardingRule
本文整理了Java中org.apache.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration.getMaster
本文整理了Java中org.apache.shardingsphere.orchestration.yaml.config.YamlOrchestrationConfiguration.getOrch
我是一名优秀的程序员,十分优秀!