docker搭建mysql主从搭建及Sharding-Jdbc读写分离

在这里插入图片描述

理解读写分离

面对日益增加的系统访问量,数据库的吞吐量面临着巨大瓶颈。 对于同一时刻有大量并发读操作和较少写操作类
型的应用系统来说,将数据库拆分为主库和从库,主库负责处理事务性的增删改操作,从库负责处理查询操作,能够有效的避免由数据更新导致的行锁,使得整个系统的查询性能得到极大的改善。
在这里插入图片描述
通过一主多从的配置方式,可以将查询请求均匀的分散到多个数据副本,能够进一步的提升系统的处理能力。 使用多主多从的方式,不但能够提升系统的吞吐量,还能够提升系统的可用性,可以达到在任何一个数据库宕机,甚至磁盘物理损坏的情况下仍然不影响系统的正常运行。

在这里插入图片描述
读写分离的数据节点中的数据内容是一致的,而水平分片的每个数据节点的数据内容却并不相同。将水平分片和读写分离联合使用,能够更加有效的提升系统的性能。

**Sharding-JDBC读写分离则是根据SQL语义的分析,将读操作和写操作分别路由至主库与从库。**它提供透明化读写分离,让使用方尽量像使用一个数据库一样使用主从数据库集群。

docker环境mysql主从搭建

创建目录如下
在这里插入图片描述

具体脚本查看 https://github.com/fafeidou/fast-cloud-nacos/tree/master/dockerfile/mysql

到mysql目录下执行

docker-compose build

Master Mysql

SHOW MASTER STATUS;

在这里插入图片描述

Slave Mysql

stop slave;
change master to master_host='mysql-master', 
master_port=3306,
master_user='root',
master_password='root', 
master_log_file='replicas-mysql-bin.000003 ',
master_log_pos=0;
start slave;
show slave status;

注意这个log_pos要和上面的master的position对应,执行完后 ,只需要 关注
Slave_IO_Running
Slave_SQL_Running
这两个字段是否 都是 YES ,如果是 则配置完成,No的话 可以看Last_IO_ERROR 或 Last_SQL_ERROR 排错就可以啦。

实现sharding-jdbc读写分离

Sharding-JDBC规则

server.port=56081

spring.application.name = fast-common-sharding-rw-separation

server.servlet.context-path = /fast-common-sharding-rw-separation
spring.http.encoding.enabled = true
spring.http.encoding.charset = UTF-8
spring.http.encoding.force = true

spring.main.allow-bean-definition-overriding = true

mybatis.configuration.map-underscore-to-camel-case = true

#sharding-jdbc分片规则配置
#数据源
spring.shardingsphere.datasource.names = m0,m1,m2,s0

spring.shardingsphere.datasource.m0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url = jdbc:mysql://192.168.56.121:33065/user_db?useUnicode=true
spring.shardingsphere.datasource.m0.username = root
spring.shardingsphere.datasource.m0.password = root

spring.shardingsphere.datasource.s0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s0.url = jdbc:mysql://192.168.56.121:33066/user_db?useUnicode=true
spring.shardingsphere.datasource.s0.username = root
spring.shardingsphere.datasource.s0.password = root


spring.shardingsphere.datasource.m1.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url = jdbc:mysql://192.168.56.121:33065/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username = root
spring.shardingsphere.datasource.m1.password = root

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://192.168.56.121:33065/order_db_2?useUnicode=true
spring.shardingsphere.datasource.m2.username = root
spring.shardingsphere.datasource.m2.password = root

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

# 指定t_order表的数据分布情况,配置数据节点 m1.t_order_1,m1.t_order_2,m2.t_order_1,m2.t_order_2
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes = m$->{1..2}.t_order_$->{1..2}
#spring.shardingsphere.sharding.tables.t_user.actual-data-nodes = m$->{0}.t_user
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes = ds0.t_user

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

# 指定t_order表的分片策略,分片策略包括分片键和分片算法
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 + 1}

# 分库策略,以user_id为分片键,分片策略为user_id % 2 + 1,user_id为偶数操作m1数据源,否则操作m2。
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column= user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression = t_user

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

# 指定t_dict为公共表
spring.shardingsphere.sharding.broadcast-tables=t_dict

swagger.enable = true

logging.level.root = info
logging.level.org.springframework.web = info
fast.cloud.nacos.rw.separation.dao = debug
logging.level.druid.sql = debug


测试

执行testInsertUser单元测试:

在这里插入图片描述

通过日志可以看出,所有写操作落入m0数据源。
执行testSelectUserbyIds单元测试:
在这里插入图片描述
通过日志可以看出,所有读操作落入s0数据源,达到目标。

github地址: https://github.com/fafeidou/fast-cloud-nacos/tree/master/fast-common-examples/fast-common-sharding-rw-separation

遇到的问题

centos安装docker-compose遇到的问题

https://www.cnblogs.com/eddie1127/p/12003358.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值