当前位置:网站首页>Shardingsphere read write separation
Shardingsphere read write separation
2022-04-23 01:50:00 【Charge8】
Read write separation is to ensure the stability of database products , Many databases have dual hot standby function .
principle : Let the main database (master) Transactional increase 、 Change 、 Delete operation , And from the database (slave) Handle select Query operation .
Be careful :
- Sharding-JDBC adopt sql Sentence semantic analysis , Realize the separation of reading and writing , No data synchronization .
- Data synchronization requires the database to build a master-slave cluster architecture , To do master-slave synchronization between data .
The master-slave cluster architecture of the database is built by itself , The following from Sharding-JDBC Application layer to separate data reading and writing .
One 、 Read / write separation
1、application.properties The configuration file
stay application.properties This is done in the configuration file
# Configure real data sources
spring.shardingsphere.datasource.names=db1,db2
# Configure the 1 Data sources
spring.shardingsphere.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.db1.url=jdbc:mysql://localhost:3306/sharding_db1?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
spring.shardingsphere.datasource.db1.username=root
spring.shardingsphere.datasource.db1.password=123456
# Configure the 2 Data sources
spring.shardingsphere.datasource.db2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.db2.url=jdbc:mysql://localhost:3306/sharding_db2?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
spring.shardingsphere.datasource.db2.username=root
spring.shardingsphere.datasource.db2.password=123456
## Configure master-slave logical data sources , Read write separation rules , db1 Main library ,db2 Slave Library
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=db1
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names[0]=db2
# Table fragmentation based on read-write separation
spring.shardingsphere.sharding.tables.course.actual-data-nodes=ds0.course_$->{
1..2}
# Specifies the primary key generation policy for the table
spring.shardingsphere.sharding.tables.course.key-generator.column=id
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.props.worker.id=1
# Table strategy
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{id%2+1}
# open shardingsphere Of sql Log output .
spring.shardingsphere.props.sql.show=true
# An entity class corresponds to two tables , Cover
spring.main.allow-bean-definition-overriding=true
2、 test
1) preservation
@Test
public void testSave() throws InterruptedException {
for (int i = 0; i < 4; i++) {
Course course = new Course();
course.setGmtCreate(new Date());
course.setName("java");
course.setUserId(1001L + i);
course.setStatus("1");
TimeUnit.MILLISECONDS.sleep(50);
int count = courseMapper.insert(course);
System.out.println("id ->" + course.getId());
}
}
From the log , The databases for saving operations are all primary databases (db1).
2) Inquire about
@Test
public void testIN() {
QueryWrapper<Course> wrapper = new QueryWrapper<Course>();
wrapper.in("id",1515577381737865217L, 1515492164109004802L, 1515576508269936641L);
List<Course> courses = courseMapper.selectList(wrapper);
System.out.println(courses.size());
courses.forEach(course -> System.out.println(course));
}
From the log , The database of query operation is from the database (db2).
because id=1515577381737865217 Record in main library (db1) in . The data has not been synchronized to the slave Library (db2), So we can't find it .
– Stay Hungry , stay foolish .
版权声明
本文为[Charge8]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220911326953.html
边栏推荐
- C语言实现Base64编码/解码
- 2022.4.22-----leetcode. three hundred and ninety-six
- DO447管理用户和团队的访问
- After disk D is distributed to Disk C, what should I do if the database recovery hangs? Please answer
- 腾讯云接口进行人脸检测 和签名出错问题
- J-link v9 使用技巧之虚拟串口功能
- Uncover floating-point operations hidden by the ARM compiler
- NR polar code VII - SCL (successful cancellation list coding)
- K zeros after leetcode factorial function
- Performance introduction of the first new version of cdr2022
猜你喜欢
[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?
Redis implements distributed locks
一些使用代理IP的小技巧。
ESP32蓝牙Bluetooth Controller API介绍
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
Leetcode 112 Total path (2022.04.22)
Performance introduction of the first new version of cdr2022
紫光国微财报一枝独秀 2021年净利润三位数增长靠什么
2022第六季完美童模 IPA国民赛领跑元宇宙赛道
CC2541的仿真器CC Debugger使用教程
随机推荐
d盘分给C盘后,数据库恢复挂起怎么办,求各位解答
How to "gracefully" measure system performance
2022.4.22-----leetcode.396
使用代理IP是需要注意什么?
Performance introduction of the first new version of cdr2022
Learning methods and career development guide (2022 Edition)
EBS:PO_EMPLOYEE_HIERARCHIES_ALL
After disk D is distributed to Disk C, what should I do if the database recovery hangs? Please answer
Uncover floating-point operations hidden by the ARM compiler
Use of j-link RTT
Virtual serial port function of j-link V9 using skills
BGP服务器在什么业务场景会被用到?
Makefile文件是什麼?
Jerry's CPU performance test [chapter]
Summary of LSF usage
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
LSF的使用方法总结
J-Link RTT使用
Keil MDK Chinese garbled code, two solutions, the font is no longer ugly
Under the pressure of sales, domestic mobile phones began to reduce prices, but they haven't put down their final face