本篇内容主要讲解“shardingsphere的读写分离和分表设置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“shardingsphere的读写分离和分表设置”吧!
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.1.1</version>
</dependency>
spring:
shardingsphere:
datasource:
names: master,slave0
master: #主库
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://xxx.xxxx.xxxx:3432/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useLocalSessionState=true
username: db_dev
password: db_dev123
slave0: #从库(读库)
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://xxxx.xxxx.xxxx.xxxx:3433/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useLocalSessionState=true
username: db_dev
password: db_dev123
sharding: #分片设置
tables: #分表规则
t_order: #要做分表的表名(或者叫表名前辍)
actualDataNodes: ms.t_order$->{0..2} # t_order0~2子表, 这里的ms是主从名: ms
tableStrategy: #表分片设置
inline:
shardingColumn: customer_id #以此字段做为分片条件
algorithmExpression: t_order$->{customer_id % 3} #数据分配策略,这里考虑客户维度的数据分表
keyGenerator:
column: id
type: SNOWFLAKE #雪花算法
masterSlaveRules: #主从设置(读写设置)
ms: #主从名
masterDataSourceName: master #主库
slaveDataSourceNames: slave0 #从库列表
loadBalanceAlgorithmType: round_robin #主从轮询规则
props:
sql:
show: true
https://shardingsphere.apache.org/document/legacy/3.x/document/cn/manual/sharding-proxy/configuration/ https://www.cnblogs.com/qdhxhz/p/11656205.html https://www.jianshu.com/p/cb48ba2941cb
到此,相信大家对“shardingsphere的读写分离和分表设置”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/tangcoffee/blog/5049673