怎么实现Sharding Sphere读写分离

发布时间:2021-12-20 09:21:20 作者:柒染
来源:亿速云 阅读:289

这篇文章将为大家详细讲解有关怎么实现Sharding Sphere读写分离,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

一、读写分离

为了确保数据库产品的稳定性,很多数据库拥有双机热备功能。也就是,第一台数据库服务器,是对外提供增删改查业务的生产服务器;第二台数据库服务器,仅仅接收来自第一台服务器的备份数据(注意,不同数据库产品,第一台数据库服务器,向第二台数据库服务器发送备份数据的方式不同)。当第一台数据库崩溃后,第二台数据库服务器,可以立即上线来代替第一台数据库服务器。

怎么实现Sharding Sphere读写分离

sharding-jdbc分析sql语义,实现路由读写操作到不同的数据库服务器。

由数据库进行数据同步。

二、Mysql配置读写分离

启动两个mysql实例

一个是我本机、一个是我的云服务器

Master

Master为服务器上节点

vim /etc/my.cnf

怎么实现Sharding Sphere读写分离

重启

service mysql restart

创建主从复制账号

172.16.15.25 为windows的slave机器

GRANT REPLICATION SLAVE ON *.* to 'myslave'@'%' identified by 'xxxxx';

刷新权限

flush privileges;
SHOW MASTER STATUS

怎么实现Sharding Sphere读写分离

从服务器

怎么实现Sharding Sphere读写分离

STOP SLAVE
CHANGE MASTER TOMASTER_HOST = '49.232.61.212',
MASTER_USER =  'myslave',
MASTER_PASSWORD = 'xxxxx',
MASTER_LOG_FILE = 'mysql-bin.000002',
MASTER_LOG_POS = 154;
start slave;

设置成功

怎么实现Sharding Sphere读写分离

这样就实现了mysql的主从配置,master库负责增删改操作,slave专注于查询,实现了性能与数据安全的提升。

三、shardingJDBC读写分离

shardingJDBC不提供数据同步,我们做的是通过shardingJDBC配置,解析sql语义,实现读写分离。

注意两个数据库需要结构数据一样。(先同步)

配置

# 读写分离# 一个实体类可对应两张表spring.main.allow-bean-definition-overriding=true# sharding 多数据源配置spring.shardingsphere.datasource.names=ds1,ds2
# ds1 212Masterspring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.ds1.url=jdbc:mysql://49.232.61.212:3306/sharding_1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghaispring.shardingsphere.datasource.ds1.username=rootspring.shardingsphere.datasource.ds1.password=xxxxxxxxx
# ds2 本地slavespring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.ds2.url=jdbc:mysql://127.0.0.1:3306/sharding_1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghaispring.shardingsphere.datasource.ds2.username=rootspring.shardingsphere.datasource.ds2.password=gwssi
# 主从逻辑  ds为统一的spring.shardingsphere.sharding.master-slave-rules.ds.master-data-source-name=ds1spring.shardingsphere.sharding.master-slave-rules.ds.slave-data-source-names=ds2
# 表配置spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds.user_1

#  控制台显示sqlspring.shardingsphere.props.sql.show=true

怎么实现Sharding Sphere读写分离


怎么实现Sharding Sphere读写分离

关于怎么实现Sharding Sphere读写分离就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. 如何实现Spring Boot中整合Sharding-JDBC读写分离
  2. 怎么在Spring Boot中使用Sharding-JDBC实现读写分离

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

shardingsphere

上一篇:如何编写脚本实用工具

下一篇:bash脚本中如何使用while

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》