博客
关于我
shardingsphere实现数据库读写分离
阅读量:330 次
发布时间:2019-03-04

本文共 2995 字,大约阅读时间需要 9 分钟。

Apache ShardingSphere 实验配置与验证

一、实验环境

在本次实验中,我们采用以下数据库架构进行配置:

  • 主数据库:userdb_master
  • 从数据库:userdb_slave0、userdb_slave1

每个数据库均包含一张user表,字段定义如下:

CREATE TABLE `user` (    `id` bigint(255) NOT NULL AUTO_INCREMENT,    `name` varchar(255) DEFAULT NULL,    `uid` varchar(255) DEFAULT NULL,    `school` varchar(255) DEFAULT NULL,    `age` int(11) DEFAULT NULL,    PRIMARY KEY (`id`))

使用的Shardingsphere版本为4.0.0-RC3,本次实验未实现数据库的主从同步,只是创建了一个从库。


二、Spring Boot 项目引入 ShardingSphere

为了实现数据库的分片与读写分离,我们需要在Spring Boot项目中引入Shardingsphere相关组件。以下是需要配置的依赖项:

org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
4.0.0-RC3
org.apache.shardingsphere
sharding-jdbc-spring-namespace
4.0.0-RC3

三、配置 application.properties

在Spring Boot项目中,我们需要通过application.properties文件配置Shardingsphere的数据源信息。以下是完整的配置示例:

# 读写分离配置,采用一主多从的方式spring.profiles.active= # 可根据实际需求配置# 数据源配置spring.shardingsphere.datasource.names=db-master-user,db-slave-user,db-slave1-user# 主数据源配置spring.shardingsphere.datasource.db-master-user.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.db-master-user.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.db-master-user.jdbc-url=jdbc:mysql://youripaddress:3306/userdb_master?characterEncoding=utf-8&serverTimezone=UTCspring.shardingsphere.datasource.db-master-user.username=springspring.shardingsphere.datasource.db-master-user.password=spring# 从数据源配置spring.shardingsphere.datasource.db-slave-user.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.db-slave-user.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.db-slave-user.jdbc-url=jdbc:mysql://youripaddress:3306/userdb_slave0?characterEncoding=utf-8&serverTimezone=UTCspring.shardingsphere.datasource.db-slave-user.username=springspring.shardingsphere.datasource.db-slave-user.password=spring# 第二个从数据源配置spring.shardingsphere.datasource.db-slave1-user.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.db-slave1-user.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.db-slave1-user.jdbc-url=jdbc:mysql://youripaddress:3306/userdb_slave1?characterEncoding=utf-8&serverTimezone=UTCspring.shardingsphere.datasource.db-slave1-user.username=springspring.shardingsphere.datasource.db-slave1-user.password=spring# 分片配置spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robinsspring.shardingsphere.masterslave.name=ds_msspring.shardingsphere.masterslave.master-data-source-name=db-master-userspring.shardingsphere.masterslave.slave-data-source-names=db-slave-user,db-slave1-user

四、验证与测试

1. 数据插入

通过代码向user表中插入一条数据。插入操作会自动路由到主数据库进行写入。可以通过Shardingsphere提供的监控工具查看具体的写入情况。

2. 数据查询

在查询数据时,Shardingsphere会根据配置的负载均衡算法(round_robins)自动轮询从数据库,确保读写分离的高效性。可以手动查询从数据库的数据,验证读取结果是否正确。

3. 数据删除与更新

对user表进行删除或更新操作时,同样会通过主数据库执行。可以通过日志或监控工具跟踪操作结果,确保数据变化正确反映到各个从库。


通过以上配置和验证步骤,可以实现数据库的分片与读写分离,充分发挥Shardingsphere的优化能力。

转载地址:http://vcuh.baihongyu.com/

你可能感兴趣的文章
Python中xPath技术和BeautifulSoup的使用
查看>>
python中yield的用法详解——最简单,最清晰的解释
查看>>
python中‘set.intersection()‘的算法是什么?
查看>>
Python中一个图形中的多个绘图
查看>>
python中一个好的持久同步队列
查看>>
Python中不准确的对数
查看>>
python中什么是实例方法_在Python中创建实例方法对象的不同方法
查看>>
Python中使用cutecharts实现简单的手绘风格的图表
查看>>
python中使用dxfgrabber,解析dxf文件信息生成obj文件
查看>>
Python中使用ncmbot玩转网易云音乐
查看>>
Python中使用PIL快速实现二值图
查看>>
Python中使用PIL快速实现灰度图
查看>>
Python中使用pip install ncmbot时各种错误解决办法
查看>>
Python中使用requests和parsel爬取喜马拉雅电台音频
查看>>
Python中使用turtle绘制多个同心圆
查看>>
Python中使用turtle绘制棋盘详解
查看>>