Spring boot整合Sharding-JDBC實現分庫分表

寫在前面的話
爲什麼要分庫分表,這是基於mysql的b+樹的特性來的,當單表的數據量大了之後要維護一棵龐大的樹,這樣會使得增刪改查的性能大大降低。

本文基於一個普通Spring boot項目改造添加分庫分表依賴而來,具體可以看這一篇Spring boot整合mybatis generator實現自動生成代碼

從shardingsphere官網https://shardingsphere.apache.org/index_zh.html簡介來看好像改造分庫分表相當簡單,只需要引入maven依賴即可
在這裏插入圖片描述
可是它並沒有告訴你不同spring-boot版本要跟不同版本的sharding配合使用,比如我的demo初始化的時候spring-boot-starter-parent是2.2.6,shardingsphere是下面這個版本

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

一啓動就報如下異常

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-21 05:16:09.869 ERROR 11072 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'dataSource', defined in class path resource [io/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

Disconnected from the target VM, address: '127.0.0.1:65495', transport: 'socket'

Process finished with exit code 1

即使在yml文件添加了如下的配置也沒用

main:
  allow-bean-definition-overriding: true

踩坑完畢,正文開始

org.springframework.boot使用2.0.3,sharding-jdbc使用3.0.0

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> 
    </parent>

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

pom文件結構如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis-generator</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>io.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>

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

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>5.1.35</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--分頁插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--自動生成代碼插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

server:
  port: 8080
  
sharding:
  jdbc:
    datasource:
      names: test
      # 數據庫test
      test:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test
        username: root
        password: 888888
    config:
      sharding:
        props:
          sql.show: true
        tables:
          user:  #user表名
            key-generator-column-name: id  #主鍵
            actual-data-nodes: test.user${0..1}    #數據節點,均勻分佈
            table-strategy:  #分表策略
              inline: #行內表達式
                sharding-column: id
                algorithm-expression: user${id % 2}  #按模運算分配

mybatis:
  mapper-locations: classpath:mapping/*.xml
  type-aliases-package: com.example.mybatisgenerator.model

# 打印sql
logging:
  level:
    com.example.mybatisgenerator.dao : debug

需要注意的是,分庫分表的主鍵id要在代碼裏面生成,一般可以用雪花算法生成主鍵id,可參考舊文提及的分佈式ID生成方式之雪花算法 ,如果採用數據庫自增策略的話那麼不同的表它們的id肯定會衝突

建表語句

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `phone` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

執行測試
在這裏插入圖片描述
在這裏插入圖片描述

結果如下:
user0跟user1都有數據,user表是空的,可以理解爲user表是邏輯表,
在這裏插入圖片描述

在這裏插入圖片描述
以上這種是屬於單庫分表,當然也可以配置多個數據源,主要是yml文件上的寫法稍有差異。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章