栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

shardingjdbc 4.x参考配置

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

shardingjdbc 4.x参考配置

采用properties文件进行配置水平分库,分表。

# 配置数据源
spring.shardingsphere.datasource.names=ds0,ds1

spring.shardingsphere.datasource.ds0.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=

spring.shardingsphere.datasource.ds1.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=

# 数据节点
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}

# 主键生成策略
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE

# 分库策略
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds$->{order_id % 2}

# 分表策略
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}

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

对不参与分库和分表的表可以只配置其数据源

# 配置数据源
spring.shardingsphere.datasource.names=ds0,ds1

spring.shardingsphere.datasource.ds0.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=

spring.shardingsphere.datasource.ds1.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=

# 不分库也不分表的数据节点配置
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds0.t_order

限定了数据节点以后,就可以指定的数据节点进行操作了。 

广播表配置(通常的字典表):

spring.shardingsphere.sharding.broadcast-tables=t_dict

每个库中都应该建立对应的表。

读写分离配置:

# 配置数据源
spring.shardingsphere.datasource.names=mds0,sds1

spring.shardingsphere.datasource.mds0.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.mds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.mds0.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.mds0.username=root
spring.shardingsphere.datasource.mds0.password=

spring.shardingsphere.datasource.sds1.type=org.apache.commons.dbcp2.BasicDataSource
spring.shardingsphere.datasource.sds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.sds1.url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.sds1.username=root
spring.shardingsphere.datasource.sds1.password=

# 读写数据源配置
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=mds0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-name=sds1

# 此时的ds0就有了读写的两个数据源
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds0.t_order

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/677795.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号