当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Bitcoin once exceeded 14000 US dollars and is about to face the test of the US election
- C language I blog assignment 03
- Markdown tricks
- 【学习】接口测试用例编写和测试关注点
- Filecoin has completed a major upgrade and achieved four major project progress!
- An article takes you to understand CSS3 picture border
- 【涂鸦物联网足迹】涂鸦云平台全景介绍
- Why is the LS command stuck when there are too many files?
- An article will introduce you to CSS3 background knowledge
- Can you do it with only six characters?
猜你喜欢
[elastic search engine]
What grammar is it? ]
嘉宾专访|2020 PostgreSQL亚洲大会阿里云数据库专场:王涛
JVM memory allocation - xms128m - xmx512m - XX: permsize = 128M - XX: maxpermsize = 512M
An article taught you to use HTML5 SVG tags
Will blockchain be the antidote to the global epidemic accelerating the transformation of Internet enterprises?
大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
Some operations kept in mind by the front end foundation GitHub warehouse management
美团内部讲座|周烜:华东师范大学的数据库系统研究
An article will take you to understand SVG gradient knowledge
随机推荐
细数软件工程----各阶段必不可少的那些图
Axios learning notes (2): easy to understand the use of XHR and how to package simple Axios
Zero basis to build a web search engine of its own
Markdown tricks
磁存储芯片STT-MRAM的特点
An article taught you to download cool dog music using Python web crawler
The method of realizing high SLO on large scale kubernetes cluster
实用工具类函数(持续更新)
行为型模式之解释器模式
Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
What is the purchasing supplier system? Solution of purchasing supplier management platform
Those who have worked in China for six years and a million annual salary want to share these four points with you
How to manage the authority of database account?
意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
ES6 learning notes (4): easy to understand the new grammar of ES6
Flink's datasource Trilogy: direct API
2020-08-18:介绍下MR过程?
[byte jumps, autumn recruitment Posts open] ohayoo! Don't leave after school, I want to ask you to play games!!!
非易失性MRAM存储器应用于各级高速缓存