当前位置:网站首页>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
边栏推荐
- . net unit test Part 1: common Net unit test framework?
- 拨号服务器是什么,有什么用处?
- Redis implements distributed locks
- After disk D is distributed to Disk C, what should I do if the database recovery hangs? Please answer
- W801 / w800 WiFi socket development (I) - UDP
- 最长公共子序列(记录路径版)
- PHP & laravel & master several ways of generating token by API and some precautions (PIT)
- 2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
- d盘分给C盘后,数据库恢复挂起怎么办,求各位解答
- 2022.4.20-----leetcode.388
猜你喜欢

What business scenarios will the BGP server be used in?

揭秘被Arm编译器所隐藏的浮点运算

JSP基础知识总结

What are the common proxy IP problems?

拨号vps会遇到什么问题?

Question bank and online simulation examination for safety management personnel of hazardous chemical business units in 2022

What businesses use physical servers?

ESP32使用freeRTOS的消息队列

How to initialize "naming and surname" in C language

How to classify proxy IP?
随机推荐
什么是bgp服务器,有哪些优势?
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
C语言实现Base64编码/解码
What is a dial-up server and what is its use?
What categories do you need to know before using proxy IP?
What code needs unit testing?
W801 / w800 WiFi socket development (I) - UDP
How to choose a good dial-up server?
电子采购如何成为供应链中的增值功能?
PID refinement
What is a boolean type?
What are the test steps of dynamic proxy IP?
When should I write unit tests? What is TDD?
English abbreviation of role personal attribute
Leetcode 112 Total path (2022.04.22)
客户端项目管理经常面临的挑战
W801/W800-wifi-socket开发(一)-UDP
Jerry's AI server [chapter]
力扣(LeetCode)112. 路径总和(2022.04.22)
代理IP可用率是不是等同于代理IP的效率?