Sharding-jdbc整合springboot
0:数据库中的主键我设置的为: bigint类型,不是⾃增,使⽤mybatis的时候,可以不⽤⾃⼰管理其id,即: n_id,当然这个n_id可以⾃⼰⽣成,不使⽤框架提供的,但是不建议⾃增,因为不同表或库中可能会出现主键重复的问题。
1. 搭建基本的sharding-jdbc整合springboot
  1. springboot导⼊sharding-jdbc相关依赖
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile 'org.apache.shardingsphere:sharding-jdbc-spring-boot-starter:4.0.0-RC1' // 这个是
compile 'batis.spring.boot:mybatis-spring-boot-starter:1.3.2'
compile 'mysql:mysql-connector-java:8.0.15'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
compileOnly 'org.projectlombok:lombok:1.18.8'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
  2. sharding-jdbc的配spring# 相关的配置  shardingsphere:
# 是否展⽰sql
props:
sql:
show: true
# 数据源配置
datasource:
# 数据源名称
names: ds1
# 数据源名称对应的连接池,url,⽤户名和密码配置
ds1:
# 数据库连接池类型:HikariDataSource 是springboot⾃带的数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 数据库⽤户名
username: root
# 数据库密码
password: root
  3.  编写启动类,springboot单元测试,进⾏数据库的插⼊,即可
2. sharding-jdbc的相关配置,以及分库分表
  1. 同库⽔平分表
spring:
# 相关的配置
shardingsphere:
props:
sql:
show: true
datasource:
names: ds1
ds1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: sql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
username: root
password: root
sharding:
tables:
# 这个地⽅注意: sharding-jdbc会根据名称去本节点,所以写sql的时候,要写此节点的名称
t_student:
# 表达式,健康节点:根据上⼀个节点到此值, {1..2}为groovy语⾔,$会替换成{1..2}的⼀个值,数据库表是: t_student_1 , t_student_2
# 这个配置是告诉sharding有多少个表
actual-data-nodes: ds1.t_student_$->{1..2}
# 主键⽣成策略
key-generator:
# 对应的数据库表的主键
column: n_id
# ⽣成⽅式,雪花模式
type: SNOWFLAKE
# 配置其分⽚策略和分⽚算法
table-strategy:
# ⾏表达式
inline:
# 配置sharding的计算列
sharding-column: n_id
# 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
algorithm-expression: t_student_$->{n_id % 2 +1}
  2. ⽔平分库,在⽔平分库基础上进⾏⽔平分表
spring:
# 相关的配置
shardingsphere:
props:
sql:
show: true
datasource:
# 配置数据源的名称
names: ds1,ds2
# 第⼀个数据源
ds1:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true        # 数据库⽤户名
username: root
# 数据库密码
password: root
# 第⼆个数据源
ds2:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true        # 数据库⽤户名
username: root
# 数据库密码
password: root
# 分库的策略
sharding:
tables:
# 这个地⽅注意: sharding-jdbc会根据名称去本节点,所以写sql的时候,要写此节点的名称
t_student:
# 配置数据库的分库策略
database-strategy:
# ⾏表达式模式
inline:
# 选择需要分库的字段,根据那个字段进⾏区分
sharding-column: n_card
# 表达式,c_card需要和上⾯的⼀致,groovy表达式spring boot选择题
algorithm-expression: ds$->{n_card % 2 + 1}
# 表达式,健康节点:根据上⼀个节点到此值, {1..2}为groovy语⾔,$会替换成{1..2}的⼀个值,数据库表是: t_student_1 , t_student_2          # 这个配置是告诉sharding有多少个表
actual-data-nodes: ds1.t_student_$->{1..2}
# 主键⽣成策略
key-generator:
# 对应的数据库表的主键
column: n_id
# ⽣成⽅式,雪花模式
type: SNOWFLAKE
# 配置其分⽚策略和分⽚算法
table-strategy:
# ⾏表达式
inline:
# 配置sharding的计算列
sharding-column: n_id
# 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
algorithm-expression: t_student_$->{n_id % 2 +1}
  3.垂直分库
spring:
# 相关的配置
shardingsphere:
props:
sql:
show: true
datasource:
# 配置数据源的名称
names: ds1,ds2,ds3
# 第⼀个数据源
ds1:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true        # 数据库⽤户名
username: root
# 数据库密码
password: root
# 第⼆个数据源
ds2:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true        # 数据库⽤户名
username: root
# 数据库密码
password: root
# 配置垂直分库策略
ds3:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding3?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 数据库⽤户名
username: root
# 数据库密码
password: root
# 分库的策略
sharding:
tables:
# 垂直分库、分表,其实就是写死了
t_course:
database-strategy:
inline:
sharding-column: n_id
algorithm-expression: ds3
# 虽然垂直分库没有必要但是还要配置⼀下分表规则
#          table-strategy:
#            inline:
#              sharding-column: n_id
#              algorithm-expression: t_course
# ⽔平分库、分表
# 这个地⽅注意: sharding-jdbc会根据名称去本节点,所以写sql的时候,要写此节点的名称
t_student:
# 配置数据库的分库策略
database-strategy:
# ⾏表达式模式
inline:
# 选择需要分库的字段,根据那个字段进⾏区分
sharding-column: n_card
# 表达式,c_card需要和上⾯的⼀致,groovy表达式
algorithm-expression: ds$->{n_card % 2 + 1}
# 表达式,健康节点:根据上⼀个节点到此值, {1..2}为groovy语⾔,$会替换成{1..2}的⼀个值,数据库表是: t_student_1 , t_student_2
# 这个配置是告诉sharding有多少个表
actual-data-nodes: ds1.t_student_$->{1..2}
# 主键⽣成策略
key-generator:
# 对应的数据库表的主键
column: n_id
# ⽣成⽅式,雪花模式
type: SNOWFLAKE
# 配置其分⽚策略和分⽚算法
table-strategy:
# ⾏表达式
inline:
# 配置sharding的计算列
sharding-column: n_id
# 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
algorithm-expression: t_student_$->{n_id % 2 +1}
  4. 读写分离,sharding-jdbc只负责路由,即:帮助我们把select或者insert、update、delete路由到不同的数据库上,但是不会帮助我们同步数据库数据 
spring:
# 相关的配置
shardingsphere:
props:
sql:
show: true
datasource:
# 配置数据源的名称
names: ds1,ds2,ds3
# 第⼀个数据源
ds1:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 数据库⽤户名
username: root
# 数据库密码
password: root
# 第⼆个数据源
ds2:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 数据库⽤户名
username: root
# 数据库密码
password: root
# 配置垂直分库策略
ds3:
# 数据库连接池
type: com.zaxxer.hikari.HikariDataSource
# 数据库驱动
driver-class-name: sql.jdbc.Driver
# 数据库链接
jdbcUrl: jdbc:mysql://localhost:3306/sharding3?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 数据库⽤户名
username: root
# 数据库密码
password: root
# 分库的策略
sharding:
tables:
# 垂直分库、分表,其实就是写死了
t_course:
database-strategy:
inline:
sharding-column: n_id
algorithm-expression: ds3
# 虽然垂直分库没有必要但是还要配置⼀下分表规则
#          table-strategy:
#            inline:
#              sharding-column: n_id
#              algorithm-expression: t_course
# ⽔平分库、分表
# 这个地⽅注意: sharding-jdbc会根据名称去本节点,所以写sql的时候,要写此节点的名称
t_student:
# 配置数据库的分库策略
database-strategy:
# ⾏表达式模式
inline:
# 选择需要分库的字段,根据那个字段进⾏区分
sharding-column: n_card
# 表达式,c_card需要和上⾯的⼀致,groovy表达式
algorithm-expression: ds$->{n_card % 2 + 1}
# 表达式,健康节点:根据上⼀个节点到此值, {1..2}为groovy语⾔,$会替换成{1..2}的⼀个值,数据库表是: t_student_1 , t_student_2          # 这个配置是告诉sharding有多少个表
actual-data-nodes: ds1.t_student_$->{1..2}
# 主键⽣成策略
key-generator:
# 对应的数据库表的主键
column: n_id
# ⽣成⽅式,雪花模式
type: SNOWFLAKE
# 配置其分⽚策略和分⽚算法
table-strategy:
# ⾏表达式
inline:
# 配置sharding的计算列
sharding-column: n_id
# 配置sharding的表达式,对应的n_id必须和sharding-column的值对应,否则报错
algorithm-expression: t_student_$->{n_id % 2 +1}
# 主从规则,未做验证
master-slave-rules:
# 随机起⼀个名称,如果配置主从,那么需要修改分表策略:::公共表修改
# 分表策略改成: spring.shardingsphere.sharding.tables.t_public.actual-data-nodes=ms0.t_public
# ms0 主从已经指向了 ds1、ds2数据源
ms0:
# 主节点
master-data-source-name: ds1
# 从节点
slave-data-source-names: ds2

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。