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

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

实验环境:

主数据库:userdb_master

从数据库:userdb_slave0,userdb_slave1

数据库各有一个表user

shardingsphere版本:4.0.0-RC3

实验没有实现数据库的主从同步,只是建了数据库叫从库。

第一步:建立数据库表

在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`))

第二步:springboot项目中引入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

#读写分离配置,即一主多从方式配置debug=falseserver.port=8082server.servlet.context-path=spring.jackson.serialization.INDENT_OUTPUT=truespring.profiles.active=#spring.main.allow-bean-definition-overriding=truespring.shardingsphere.datasource.names=db-master-user,db-slave-user,db-slave1-userspring.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=spring.shardingsphere.datasource.db-master-user.password=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=spring.shardingsphere.datasource.db-slave-user.password=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=spring.shardingsphere.datasource.db-slave1-user.password=spring.shardingsphere.masterslave.load-balance-algorithm-type=round_robinspring.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

到此配置就完毕了。

第四步:验证

自己实现user表的增删改查。

验证的时候,通过代码增加一条数据,发现数据插入主库中。

查询数据的时候,需要自己手动往从库查几条数据做验证,发现代码查询的时候,会轮询去查两个从库。

到此验证完毕。

 

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

你可能感兴趣的文章
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>