当前位置:网站首页>Interviewer: how about shardingsphere
Interviewer: how about shardingsphere
2020-11-06 21:30:00 【Xiaoliu】
List of articles
[toc]
Before learning, first understand the concept of sub database and sub table :https://spiritmark.blog.csdn.net/article/details/109524713
One 、ShardingSphere brief introduction
In database design, consider vertical database and vertical table . As the amount of data in the database increases , Don't immediately think about horizontal segmentation , First consider cache processing , Read / write separation , send By indexing and so on , If these methods can't solve the problem at all , Then consider the horizontal sub database and horizontal sub table .
Problems caused by sub database and sub table :
- Cross node join query problem ( Pagination 、 Sort )
- Multiple data source management issues
Apache ShardingSphere Is a set of open source distributed database middleware solutions composed of the ecosystem , It consists of JDBC、 Proxy and Sidecar( Planning ) this 3 They are independent of each other , But it can mix deployment and use of product composition . They all provide standardized data fragmentation 、 Distributed transaction and database governance functions , It can be applied to such as Java isomorphism 、 Heterogeneous languages 、 Various application scenarios such as cloud native .
Apache ShardingSphere Positioning as a relational database middleware , Designed to be fully reasonable in distributed fields Using the computing and storage capabilities of relational databases , Instead of implementing a new relational database . It doesn't change through focus , And then grasp the essence of things . Relational databases still have a huge market today , It's the cornerstone of every company's core business , The future is hard to shake , At present, we pay more attention to the increment based on the original , Not subversion .
Two 、Sharding-JDBC
Sharding-JDBC It's lightweight java frame , It's an enhanced version of JDBC drive , Simplify the data related operations after sub database and sub table .
Create a new project and add dependencies :
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-parentartifactId>
<version>2.2.1.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.20version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.apache.shardingspheregroupId>
<artifactId>sharding-jdbc-spring-boot-starterartifactId>
<version>4.0.0-RC1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.0.5version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
2.1 Sharding-JDBC Realize the level table
① According to the way of horizontal table , Create databases and database tables
Horizontal table rules : If you add cid It's even numbers that add data course_1, If it is an odd number, add it to course_2 
CREATE TABLE `course_1` (
`cid` bigint(16) NOT NULL,
`cname` varchar(255) ,
`userId` bigint(16),
`cstatus` varchar(16) ,
PRIMARY KEY (`cid`)
)
② Write entities and Mapper class
@Data
public class Course {
private Long cid;
private String cname;
private Long userId;
private String cstatus;
}
@Repository
public interface CourseMapper extends BaseMapper<Course> {
}
③ Detailed configuration file
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: m1
m1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.182.200:3306/course_db?serverTimezone=GMT%2B8
username: root
password: 1234
sharding:
tables:
course:
actual-data-nodes: m1.course_$->{1..2}
key-generator:
column: cid
type: SNOWFLAKE
table-strategy:
inline:
shardingcolumn: cid
algorithm-expression: course_$->{cid%2+1}
props:
sql:
show: true
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
④ test
@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingSphereTestApplication {
@Autowired
CourseMapper courseMapper;
@Test
public void addCourse() {
for (int i = 1; i 10; i++) {
Course course = new Course();
course.setCname("java" + i);
course.setUserId(100L);
course.setCstatus("Normal" + i);
courseMapper.insert(course);
}
}
@Test
public void queryCourse() {
QueryWrapper<Course> wrapper = new QueryWrapper<>();
wrapper.eq("cid",493001315358605313L);
Course course = courseMapper.selectOne(wrapper);
System.out.println(course);
}
}

2.2 Sharding-JDBC Realize the horizontal sub database
① Demand analysis
② Create databases and tables
③ Detailed configuration file
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: m1,m2
m1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.182.200:3306/course_db_2?serverTimezone=GMT%2B8
username: root
password: 1234
m2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.182.200:3306/course_db_3?serverTimezone=GMT%2B8
username: root
password: 1234
sharding:
tables:
course:
actual-data-nodes: m$->{1..2}.course_$->{1..2}
key-generator:
column: cid
type: SNOWFLAKE
database-strategy:
inline:
sharding-column: userId
algorithm-expression: m$->{userId%2+1}
table-strategy:
inline:
sharding-column: cid
algorithm-expression: course_$->{cid%2+1}
props:
sql:
show: true
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
④ Test code
@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingSphereTestApplication {
@Autowired
CourseMapper courseMapper;
@Test
public void addCourse() {
for (int i = 1; i 20; i++) {
Course course = new Course();
course.setCname("java" + i);
int random = (int) (Math.random() * 10);
course.setUserId(100L + random);
course.setCstatus("Normal" + i);
courseMapper.insert(course);
}
}
@Test
public void queryCourse() {
QueryWrapper<Course> wrapper = new QueryWrapper<>();
wrapper.eq("cid", 493001315358605313L);
Course course = courseMapper.selectOne(wrapper);
System.out.println(course);
}
}
Query the actual corresponding SQL: 
2.3 Sharding-JDBC Working with common tables
Public table :
- A table that stores fixed data , Table data rarely changes , Queries are often associated
- Create a common table with the same structure in each database
① Thought analysis
② Create a common table in the corresponding database t_udict,并创建对应实体和 Mapper``
CREATE TABLE `t_udict` (
`dict_id` bigint(16) NOT NULL,
`ustatus` varchar(16) ,
`uvalue` varchar(255),
PRIMARY KEY (`dict_id`)
)
③ Detailed configuration file
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: m1,m2
m1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.182.200:3306/course_db_2?serverTimezone=GMT%2B8
username: root
password: 1234
m2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.182.200:3306/course_db_3?serverTimezone=GMT%2B8
username: root
password: 1234
sharding:
tables:
course:
actual-data-nodes: m$->{1..2}.course_$->{1..2}
key-generator:
column: cid
type: SNOWFLAKE
database-strategy:
inline:
sharding-column: userId
algorithm-expression: m$->{userId%2+1}
table-strategy:
inline:
sharding-column: cid
algorithm-expression: course_$->{cid%2+1}
t_udict:
key-generator:
column: dict_id
type: SNOWFLAKE
broadcast-tables: t_udict
props:
sql:
show: true
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
④ To test
After testing : Data is inserted into each table in each library , Deleting will also delete all data .
@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingSphereTestApplication {
@Autowired
UdictMapper udictMapper;
@Test
public void addUdict() {
Udict udict = new Udict();
udict.setUstatus("a");
udict.setUvalue(" Enabled ");
udictMapper.insert(udict);
}
@Test
public void deleteUdict() {
QueryWrapper<Udict> wrapper = new QueryWrapper<>();
wrapper.eq("dict_id", 493080009351626753L);
udictMapper.delete(wrapper);
}
}
2.4 Sharding-JDBC Read and write separation
To ensure the stability of database products , Many databases have dual hot standby function . That is to say , The first database server is the production server that provides the business of adding, deleting and modifying ; The second database server is mainly used for reading operations .
Sharding-JDBC adopt sql Sentence semantic analysis , Realize the separation of reading and writing , No data synchronization , Data synchronization usually automatically synchronizes between database clusters .
Detailed configuration file :
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
names: m0,s0
m0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.182.200:3306/course_db?serverTimezone=GMT%2B8
username: root
password: 1234
s0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.182.200:3307/course_db?serverTimezone=GMT%2B8
username: root
password: 1234
masterslave:
master-data-source-name: m0
slave-data-source-names: s0
props:
sql:
show: true
mybatis-plus:
configuration:
map-underscore-to-camel-case: false
After testing : Add, delete and modify operations will pass master database , meanwhile master The database will synchronize data to slave database ; Check operations are all through slave database .
3、 ... and 、Sharding-Proxy
Sharding-Proxy It is positioned as Transparent database proxy side , Provide the server version that encapsulates the binary protocol of the database , Used to support heterogeneous languages , At present, only MySQL and PostgreSQL edition .
Sharding-Proxy It's a stand-alone application , Need to install Service , Perform sub database and sub table or read / write separation configuration , Startup and use .
<br> Sharding-proxy The use of reference :Sharding-Proxy Basic use of . Search on wechat : Xiao Liu in the whole stack
版权声明
本文为[Xiaoliu]所创,转载请带上原文链接,感谢
边栏推荐
- Metersphere developer's Manual
- 检测证书过期脚本
- 2020-08-17:详细说下数据倾斜怎么解决?
- This project allows you to quickly learn about a programming language in a few minutes
- Qt音视频开发46-视频传输UDP版
- STM32F030K6T6兼容替换灵动MM32F031K6T6
- 意外的元素..所需元素..
- 2020-08-24:什么是小文件?很多小文件会有什么问题?很多小文件怎么解决?(大数据)
- Zero basis to build a web search engine of its own
- GitHub: the foundation of the front end
猜你喜欢

行为型模式之解释器模式

2020 database technology conference helps technology upgrade

2020-08-29:进程线程的区别,除了包含关系之外的一些区别,底层详细信息?

Zero basis to build a web search engine of its own

git远程库回退指定版本

image operating system windows cannot be used on this platform

An article takes you to understand CSS gradient knowledge

Python basic variable type -- list analysis

Junit测试出现 empty test suite

Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
随机推荐
Description of phpshe SMS plug-in
Behind the record breaking Q2 revenue of Alibaba cloud, the cloud opening mode is reshaping
File download manager realized by electron
美团内部讲座|周烜:华东师范大学的数据库系统研究
How to manage the authority of database account?
迅为iMX6开发板-设备树内核-menuconfig的使用
How to make characters move
2020 database technology conference helps technology upgrade
What kind of music do you need to make for a complete game?
Flink's datasource Trilogy 2: built in connector
What is the tensor in tensorflow?
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
An article takes you to understand CSS pagination examples
An article will take you to understand CSS3 fillet knowledge
Git rebase is in trouble. What to do? Waiting line
This project allows you to quickly learn about a programming language in a few minutes
解决 WPF 绑定集合后数据变动界面却不更新的问题
Hdu3974 assign the task segment tree DFS order
递归、回溯算法常用数学基础公式
ORA-02292: 违反完整约束条件 (MIDBJDEV2.SYS_C0020757) - 已找到子记录