Sharding-JDBC一些配置详解4.x

快速入门

说明:单库商品表水平分片,分片规则%2

#端口号
server.port=56000
#实例名称
spring.application.name= sharding_quick
#表示后发现的bean会覆盖之前相同名称的bean
spring.main.allow-bean-definition-overriding=true
#该配置项就是指将带有下划线的表字段映射为驼峰格式的实体类属性
mybatis.configuration.map-underscore-to-camel-case=true
#以下是分片规则配置

##定义数据源
spring.shardingsphere.datasource.names=m1
#数据源1
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://118.31.18.203:3306/order_db?useUnicode=true
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root


#指定t_order表的数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.t_order.actualDataNodes=m1.t_order_$->{1..2}

#指定t_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.keyGenerator.column=order_id
spring.shardingsphere.sharding.tables.t_order.keyGenerator.type=SNOWFLAKE

#分表策略,指定t_order表的分片策略,分片策略包括分片键和分片算法
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.shardingColumn=order_id
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.algorithmExpression=t_order_$->{order_id % 2+1}


#打开sql输出日志
spring.shardingsphere.props.sql.show=true

#打开swagger
swagger.enable=true

#日志级别
logging.level.root=info
logging.level.org.springframework.web=info
logging.level.com.itheima=debug
logging.level.druid.sql=debug

水平分库分表

#端口号
server.port=56000
#实例名称
spring.application.name= sharding_quick
#表示后发现的bean会覆盖之前相同名称的bean
spring.main.allow-bean-definition-overriding=true
#该配置项就是指将带有下划线的表字段映射为驼峰格式的实体类属性
mybatis.configuration.map-underscore-to-camel-case=true
#以下是分片规则配置

##定义数据源
spring.shardingsphere.datasource.names=m1,m2
#数据源1
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://118.31.18.203:3306/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#数据源2
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://118.31.18.203:3306/order_db_2?useUnicode=true
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root

#指定t_order表的数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.t_order.actualDataNodes=m$->{1..2}.t_order_$->{1..2}

#指定t_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.keyGenerator.column=order_id
spring.shardingsphere.sharding.tables.t_order.keyGenerator.type=SNOWFLAKE

#分表策略,指定t_order表的分片策略,分片策略包括分片键和分片算法
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.shardingColumn=order_id
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.algorithmExpression=t_order_$->{order_id % 2+1}

#分库策略,以user_id为分片键,分片策略为user_id%2+1,user_id为偶数操作m1数据源,否则操作m2。
spring.shardingsphere.sharding.tables.t_order.databaseStrategy.inline.shardingColumn=user_id
spring.shardingsphere.sharding.tables.t_order.databaseStrategy.inline.algorithmExpression=m$->{user_id % 2+1 }

#打开sql输出日志
spring.shardingsphere.props.sql.show=true

#打开swagger
swagger.enable=true

#日志级别
logging.level.root=info
logging.level.org.springframework.web=info
logging.level.com.itheima=debug
logging.level.druid.sql=debug

垂直分库+读写分离

说明:垂直分库把user和order分库,读写分离以user为案例,m0为主库写操作,s0为从库读操作

#端口号
server.port=56000
#实例名称
spring.application.name= sharding_quick
#表示后发现的bean会覆盖之前相同名称的bean
spring.main.allow-bean-definition-overriding=true
#该配置项就是指将带有下划线的表字段映射为驼峰格式的实体类属性
mybatis.configuration.map-underscore-to-camel-case=true
#以下是分片规则配置

##定义数据源
spring.shardingsphere.datasource.names=m0,m1,m2,s0

#数据源s0
spring.shardingsphere.datasource.s0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.s0.url=jdbc:mysql://localhost:3307/user_db?useUnicode=true
spring.shardingsphere.datasource.s0.username=root
spring.shardingsphere.datasource.s0.password=root

#数据源0
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/user_db?useUnicode=true
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=root

#数据源1
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://118.31.18.203:3306/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root
#数据源2
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://118.31.18.203:3306/order_db_2?useUnicode=true
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root

#指定t_order表的数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.t_order.actualDataNodes=m$->{1..2}.t_order_$->{1..2}

#指定t_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.keyGenerator.column=order_id
spring.shardingsphere.sharding.tables.t_order.keyGenerator.type=SNOWFLAKE

#t_order分表策略,指定t_order表的分片策略,分片策略包括分片键和分片算法
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.shardingColumn=order_id
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.algorithmExpression=t_order_$->{order_id % 2+1}


#主库从库逻辑数据源定义ds0为user_db
spring.shardingsphere.sharding.master-slave-rules.ds0.masterDataSourceName=m0
spring.shardingsphere.sharding.master-slave-rules.ds0.slaveDataSourceNames=s0

#t_user分表策略
#spring.shardingsphere.sharding.tables.t_user.actualDataNodes=m0.t_user
#配置主库从库
spring.shardingsphere.sharding.tables.t_user.actualDataNodes=ds0.t_user
spring.shardingsphere.sharding.tables.t_user.tableStrategy.inline.shardingColumn=user_id
spring.shardingsphere.sharding.tables.t_user.tableStrategy.inline.algorithmExpression=t_user
spring.shardingsphere.sharding.tables.t_user.keyGenerator.column=user_id
spring.shardingsphere.sharding.tables.t_user.keyGenerator.type=SNOWFLAKE

#分库策略,以user_id为分片键,分片策略为user_id%2+1,user_id为偶数操作m1数据源,否则操作m2。
spring.shardingsphere.sharding.tables.t_order.databaseStrategy.inline.shardingColumn=user_id
spring.shardingsphere.sharding.tables.t_order.databaseStrategy.inline.algorithmExpression=m$->{user_id % 2+1 }

#打开sql输出日志
spring.shardingsphere.props.sql.show=true

#打开swagger
swagger.enable=true

#日志级别
logging.level.root=info
logging.level.org.springframework.web=info
logging.level.com.itheima=debug
logging.level.druid.sql=debug

主从+读写

#端口号
server.port=561000
#实例名称
spring.application.name= sharding_order
#表示后发现的bean会覆盖之前相同名称的bean
spring.main.allow-bean-definition-overriding=true
#该配置项就是指将带有下划线的表字段映射为驼峰格式的实体类属性
mybatis.configuration.map-underscore-to-camel-case=true

#以下是分片规则配置

##定义数据源
spring.shardingsphere.datasource.names=m1,m2,s1,s2
#------------------------------------------------------------------------
#定义order_db_1数据源主库
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root

#定义order_db_1数据源从库
spring.shardingsphere.datasource.s1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s1.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.s1.url=jdbc:mysql://localhost:3307/order_db_1?useUnicode=true
spring.shardingsphere.datasource.s1.username=root
spring.shardingsphere.datasource.s1.password=root

#读写分离,主库和从库绑定
spring.shardingsphere.sharding.master-slave-rules.ds1.masterDataSourceName=m1
spring.shardingsphere.sharding.master-slave-rules.ds1.slaveDataSourceNames=s1

#-------------------------------------------------------------------------------------

#定义order_db_2数据源主库
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/order_db_2?useUnicode=true
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root

#定义order_db_2数据源从库
spring.shardingsphere.datasource.s2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s2.driver-class-name =com.mysql.jdbc.Driver
spring.shardingsphere.datasource.s2.url=jdbc:mysql://localhost:3307/order_db_2?useUnicode=true
spring.shardingsphere.datasource.s2.username=root
spring.shardingsphere.datasource.s2.password=root

#读写分离,主库和从库绑定
spring.shardingsphere.sharding.master-slave-rules.ds2.masterDataSourceName=m2
spring.shardingsphere.sharding.master-slave-rules.ds2.slaveDataSourceNames=s2


#指定t_order表的数据分布情况,配置数据节点
#spring.shardingsphere.sharding.tables.t_order.actualDataNodes=m$->{1..2}.t_order_$->{1..2}
spring.shardingsphere.sharding.tables.t_order.actualDataNodes=ds$->{1..2}.t_order_$->{1..2}

#指定t_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.keyGenerator.column=order_id
spring.shardingsphere.sharding.tables.t_order.keyGenerator.type=SNOWFLAKE

#t_order分表策略,指定t_order表的分片策略,分片策略包括分片键和分片算法
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.shardingColumn=order_id
spring.shardingsphere.sharding.tables.t_order.tableStrategy.inline.algorithmExpression=t_order_$->{order_id % 2+1}

#分库策略,以user_id为分片键,分片策略为user_id%2+1,user_id为偶数操作m1数据源,否则操作m2。
spring.shardingsphere.sharding.tables.t_order.databaseStrategy.inline.shardingColumn=user_id
spring.shardingsphere.sharding.tables.t_order.databaseStrategy.inline.algorithmExpression=ds$->{user_id % 2+1 }

#打开sql输出日志
spring.shardingsphere.props.sql.show=true

#日志级别
logging.level.root=info
logging.level.org.springframework.web=info
logging.level.com.itheima=debug
logging.level.druid.sql=debug

Sharding-JDBC与MyCAT的区别

在这里插入图片描述
在这里插入图片描述
个人更倾向与sharding-jdbc,因为绑定了服务,服务集群便使sharding更加高可用
而mycat时独立出来的一个服务还需要集群使用keeplive等中间件来保证高可用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值