阅读必知
本文只介绍SpringBoot如何整合ShardingSphere的主要配置及实现
版本介绍
SpringBoot : 2.3.0.RELEASE
MyBatis-Plus : 3.3.0
ShardingSphere : 4.0.0
MySql : 5.7
pom主要依赖
<!--sharding-jdbc-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0</version>
</dependency>
<!--Mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<!--德鲁伊-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<!-- mysql的依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
配置文件(ShardingSphere)
server:
port:8002
#应用名称
application:
name:shardingSphere-demo
spring:
#日期格式化
jackson:
date-format:yyyy-MM-dd HH:mm:ss
time-zone:GMT+8
#Sharding-jdbc
shardingsphere:
datasource:
#指定数据源
names: m0,m1
m0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.101.1:3306/master?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password:
m1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.101.2:3306/slave?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password:
sharding:
tables:
#配置需要水平分表的表
user:
#指定数据库表的数据分布情况,配置数据节点
##actual-data-nodes: m$->{1..2}.t_user_$->{0..1} user_0 user_1
actual-data-nodes: ds0.user_$->{0..1}
key-generator:
#配置主键
column: id
#主键采用雪花算法
type: SNOWFLAKE
#配置分库策略
database-strategy:
inline:
sharding-column: id
#此处配置下面主从分离起的key名称
algorithm-expression: ds0
#配置表的分片策略,包括分片键和分片算法
table-strategy:
inline:
#配置分表建
sharding-column: id
#此处配置水平分表策略,user_$->{id % 2}相当于user_0,user_1
algorithm-expression: user_$->{id % 2}
#配置主从数据库 一主多从 map
master-slave-rules:
ds0:
#配置主库
master-data-source-name:m0
#配置从库
slave-data-source-names:m1
#显示Sql
props:
sql:
show:true
,
网友评论