一小時讀懂ShardingJDBC之分庫分表

作為輕量級java框架,sharding JDBC在Java的jdbc層提供了額外的服務(wù),可以理解為增強(qiáng)版的jdbc驅(qū)動。其中,分庫分表的操作是其中的重要一環(huán),接下來就跟隨我來看一看,這一操作如何進(jìn)行。
環(huán)境準(zhǔn)備
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version></parent>
<properties>
<java.version>1.8</java.version>
<sharding.version>3.1.0</sharding.version></properties>
<dependencies>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-core</artifactId>
<version>${sharding.version}</version>
</dependency>

為蘇州等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及蘇州網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、蘇州網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

<dependency>
    <groupId>io.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>${sharding.version}</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.4.5</version>
</dependency>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>MySQL</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.46</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency></dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins></build>
domain
// 建立domain@Setter@Getter@ToString@NoArgsConstructor@AllArgsConstructorpublic class Employee {
private Long id;
private String name;}
配置類
@SpringBootApplication@MapperScan("cn.wolfcode.sharding.mapper")public class ShardingApplication { }
分庫分表
案例模型
把數(shù)據(jù)分別存放在兩臺服務(wù)器的兩個數(shù)據(jù)庫中表,通過分片算法來決定當(dāng)前的數(shù)據(jù)存放在哪個數(shù)據(jù)庫的哪個表中,由于一個連接池只能連接一個特定的數(shù)據(jù)庫,所以這里需要創(chuàng)建多個連接池對象
建表
-- 分別在2臺服務(wù)器中建立數(shù)據(jù)庫sharding,并且建表employee_0和employee_1CREATE TABLE employee_0 (
id bigint(20) PRIMARY KEY AUTO_INCREMENT,
name varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ###################################CREATE TABLE employee_1 (
id bigint(20) PRIMARY KEY AUTO_INCREMENT,
name varchar(255) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
application.properties

定義連接池

sharding.jdbc.datasource.names=db0,db1

格式sharding.jdbc.datasource.連接池名.xxx:設(shè)置4要素信息

sharding.jdbc.datasource.db0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db0.url=jdbc:mysql://db0Ip:port/sharing
sharding.jdbc.datasource.db0.username=xxx
sharding.jdbc.datasource.db0.password=xxx

sharding.jdbc.datasource.db1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.db1.url=jdbc:mysql://db1Ip:port/sharing
sharding.jdbc.datasource.db1.username=xxx
sharding.jdbc.datasource.db1.password=xxx

設(shè)置分庫規(guī)則

sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column:分庫列

sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression:分庫算法

sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=db$->{id % 2}

綁定邏輯表

sharding.jdbc.config.sharding.binding-tables=employee

設(shè)置分表規(guī)則

sharding.jdbc.config.sharding.tables.邏輯表.actual-data-nodes:邏輯表對應(yīng)的真實(shí)表

sharding.jdbc.config.sharding.tables.邏輯表.table-strategy.inline.sharding-column:分表列

sharding.jdbc.config.sharding.tables.邏輯表.table-strategy.inline.algorithm-expression:分表算法

sharding.jdbc.config.sharding.tables.邏輯表.key-generator-column-name:主鍵列

sharding.jdbc.config.sharding.tables.employee.actual-data-nodes=db$->{0..1}.employee$->{0..1}
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.sharding-column=id
sharding.jdbc.config.sharding.tables.employee.table-strategy.inline.algorithm-expression=employee
$->{id % 2}
sharding.jdbc.config.sharding.tables.employee.key-generator-column-name=id

打印日志

sharding.jdbc.config.props.sql.show=true
mapper
/**

  • 這里寫的employee表是上面所配置的邏輯表
  • 底層會根據(jù)分片規(guī)則,把我們寫的邏輯表改寫為數(shù)據(jù)庫中的真實(shí)表
    /@Mapperpublic interface EmployeeMapper {
    @Select("select
    from employee")
    List<Employee> selectAll();

    @Insert("insert into employee (name) values (#{name})")
    void inser(Employee entity);}
    測試
    @RunWith(SpringRunner.class)@SpringBootTest(classes=ShardingApplication.class)public class ShardingApplicationTests {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Test
    public void save() {
    for (int i = 0; i < 10; i++) {
    Employee employee = new Employee();
    employee.setName("xx"+i);
    employeeMapper.inser(employee);
    }
    }

    @Test
    public void list() {
    employeeMapper.selectAll().forEach(System.out::println);
    }}
    優(yōu)缺點(diǎn)
    ?拆分后單表數(shù)據(jù)量比較小,單表大數(shù)據(jù)被拆分,解決了單表大數(shù)據(jù)訪問問題
    ?分表以什么切分如果弄的不好,導(dǎo)致多次查詢,而且有時候要跨庫操作,甚至導(dǎo)致join無法使用,對排序分組等有性能影響
    ?之前的原子操作被拆分成多個操作,事務(wù)處理變得復(fù)雜
    ?多個DB維護(hù)成本增加

分庫分表也僅僅是shardingJDBC中小小的一部分,還有很多內(nèi)容需要大家繼續(xù)研究,如果你對此感興趣的話,不妨關(guān)注我,接下來我還會發(fā)布更多的相關(guān)教程供大家學(xué)習(xí),如果遇到問題我也會盡力幫助你。

文章題目:一小時讀懂ShardingJDBC之分庫分表
本文地址:http://bm7419.com/article10/pssogo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站內(nèi)鏈ChatGPT、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司