欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

怎么用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql數(shù)據(jù)分表分片

本篇內(nèi)容主要講解“怎么用ShardingSphere5.0.0-alpha實(shí)現(xiàn)MySQL數(shù)據(jù)分表分片”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql數(shù)據(jù)分表分片”吧!

創(chuàng)新互聯(lián)公司于2013年開(kāi)始,先為甕安等服務(wù)建站,甕安等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為甕安企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

聲明

  • 本文會(huì)基于 Springboot + mybatis + shardingsphere + mysql5.6 + druid 進(jìn)行實(shí)戰(zhàn)講解

  • 本文的實(shí)戰(zhàn)內(nèi)容為分表、以及數(shù)據(jù)分片, 不涉及分庫(kù), 讀寫(xiě)分離之類(lèi)的

  • 本文不會(huì)介紹 shardingsphere 的歷史、概念以及分庫(kù)分表的相關(guān)理論

  • 本文采用的 shardingsphere 版本是 5.0.0-alpha, 具體見(jiàn) pom 文件

  • 本文涉及的源碼請(qǐng)參考 碼云地址

  • 如果看 官方文檔 時(shí), 請(qǐng)選對(duì)版本 !!!

正文

實(shí)現(xiàn)目標(biāo)

我們有一張邏輯用戶(hù)表 user_info, 我們把它水平拆分成 user_info0user_info1 兩張物理表 當(dāng)我們往用戶(hù)表插數(shù)據(jù)時(shí), 數(shù)據(jù)會(huì)按照一定的規(guī)則(根據(jù)id取模), 寫(xiě)入到其中一張 user_info 表中.

準(zhǔn)備工作

1. 數(shù)據(jù)庫(kù)表
create database miaosha;

DROP TABLE IF EXISTS `user_info0`;
CREATE TABLE `user_info0` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_label` varchar(32) COLLATE utf8_bin DEFAULT NULL,
  `username` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `email` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `phone` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(128) COLLATE utf8_bin NOT NULL,
  `active` tinyint(4) NOT NULL DEFAULT '1',
   PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


DROP TABLE IF EXISTS `user_info1`;
CREATE TABLE `user_info1` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_label` varchar(32) COLLATE utf8_bin DEFAULT NULL,
  `username` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `email` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `phone` varchar(64) COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(128) COLLATE utf8_bin NOT NULL,
  `active` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
2. pom 依賴(lài)
<?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.3.2.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.nimo</groupId>
    <artifactId>shardingsphere-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shardingsphere-demo</name>

    <properties>
        <java.version>1.8</java.version>
    </properties>
  
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
      
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
      
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- shardingsphere -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
            <version>5.0.0-alpha</version>
        </dependency>

        <!-- 阿里數(shù)據(jù)源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.3</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
3. application.yml

再次強(qiáng)調(diào)下, 本文采用的 shardingsphere 版本是 5.0.0-alpha. 不同版本配置會(huì)有差異.

填寫(xiě)配置文件時(shí),一定要小心, 坑死了; 不信你不配置 com.alibaba.druid.pool.DruidDataSource試試,

或者你用默認(rèn)的數(shù)據(jù)源替換試試 om.zaxxer.hikari.HikariDataSource

server:
  port: 8777

spring:
  shardingsphere:
 
    props:
      sql-show: true
      
    datasource:
      names: ds0
      # 注意這里的數(shù)據(jù)源配置用的是 druid
      common:
        type: com.alibaba.druid.pool.DruidDataSource
      ds0:
        url: jdbc:mysql://127.0.0.1:3306/miaosha?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
        username: root
        password: '123456'
        driver-class-name: com.mysql.cj.jdbc.Driver
    rules:
      sharding:
        key-generators:
          # 此處必須要配置,否則會(huì)導(dǎo)致報(bào)錯(cuò),因?yàn)閟hardingsphere-jdbc-core-spring-boot-starter需要加載此項(xiàng)配置,官網(wǎng)的demo例子有錯(cuò)
          snowflake:
            type: SNOWFLAKE
            props:
              worker-id: 123
        sharding-algorithms:
          table-inline:
            type: INLINE
            props:
              # 不要漏掉 $ 或 ->
              algorithm-expression: user_info$->{id % 2}
        tables:
          user_info:
            # 配置 user_info 的分表的規(guī)則
            actual-data-nodes: ds0.user_info$->{0..1}
            table-strategy:
              standard:
                sharding-column: id
                sharding-algorithm-name: table-inline
    enabled: true

mybatis:
  typeAliasesPackage: com.nimo.shardingspheredemo.entity
  mapperLocations: classpath:mapper/*.xml
4. 主要代碼
// sql 
<insert id="addUser" parameterType="com.nimo.shardingdemo.entity.UserInfo">
   insert into user_info(id, username, password) values (#{id}, #{username}, #{password})
</insert>
 
 // 新增一個(gè)用戶(hù)信息
@PostMapping("userinfo")
public Object addUserInfo(@RequestBody UserInfo userInfo) {
   return userInfoMapper.addUser(userInfo);
}
5. 測(cè)試命令
curl -X POST --location "http://localhost:8777/userinfo" \
    -H "Content-Type: application/json" \
    -d "{
          \"id\": 18,
          \"username\": \"wangbadan\",
          \"password\": \"123456\"
        }"
問(wèn)題

我們創(chuàng)建表時(shí)設(shè)置的 主鍵id是自增的, 理論上是不用傳的, 但是我們往表中插數(shù)據(jù), 是根據(jù) 主鍵id 取模來(lái)決定具體往哪張表中插的. 所以這個(gè)主鍵 id 此時(shí)必須得有.

另外, 如果我們的服務(wù)有多個(gè), 那么這個(gè) id 如何生成?

如何解決

首先把 sql 改寫(xiě)為如下方式:

// sql 
<insert id="addUser" parameterType="com.nimo.shardingdemo.entity.UserInfo">
   insert into user_info(username, password)
    values ( #{username}, #{password})
</insert>

然后在原有配置的基礎(chǔ)上, 追加如下配置信息(通過(guò)雪花算法生成id)

spring:
  shardingsphere:
    rules:
      sharding: 
        tables:
          user_info:
            key-generate-strategy:
              key-generator-name: snowflake
              column: id

怎么用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql數(shù)據(jù)分表分片

測(cè)試

curl -X POST --location "http://localhost:8777/userinfo" \
    -H "Content-Type: application/json" \
    -d "{
          \"username\": \"wangbadan\",
          \"password\": \"123456\"
        }"

總結(jié)

本文強(qiáng)調(diào)的是 分表, 分片的實(shí)戰(zhàn) demo 配置, 后面會(huì)逐漸更新 分庫(kù)、讀寫(xiě)分離的實(shí)戰(zhàn) demo, 跟之前的 Spring Secutiry 的實(shí)戰(zhàn)教程一樣, 講究循序漸進(jìn).

到此,相信大家對(duì)“怎么用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql數(shù)據(jù)分表分片”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

新聞名稱(chēng):怎么用ShardingSphere5.0.0-alpha實(shí)現(xiàn)mysql數(shù)據(jù)分表分片
URL地址:http://aaarwkj.com/article40/pjcdho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、動(dòng)態(tài)網(wǎng)站軟件開(kāi)發(fā)、域名注冊(cè)網(wǎng)站維護(hù)、ChatGPT

廣告

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

成都定制網(wǎng)站建設(shè)
av黄色资源在线观看| 蜜桃av网站在线播放| 黑人巨大亚洲一区二区久| 涩久久悠悠一区二区三区| 成人福利网站午夜一区| 日本不卡一区二区视频| 不卡的视频在线观看| 色吊最新在线视频免费观看| 一区二区在线视频国产| 中文字幕在线五月婷婷| 久久精品国产av极品| 国产一区二区精品性浆| 日本av免费观看一区二区| 少妇38p高潮在线| 18岁未成年禁止观看视频| 日韩精品国产一区二区在线| 久久国产精品必看狼人| 国产a级一区二区三区| 91在线视频国产网站| 欧美日韩精品免费在线观看| 99热久久精品免费精品| 蜜臀久久精品国产综合| 日日添夜夜添天天操| 91内射视频在线播放| 强暴美女视频大全久久久| 日韩在线视频观看一区二区三区| 久久精品亚洲精品国产| 欧美特黄高清在线观看| 九九热精品在线观看视频| 麻豆人妻少妇精品毛片| 亚洲av成人av天堂| 长腿丝袜美女亚洲一区二区| 国产av剧情精品亚洲| 精品蜜臀国产av一区二区| 成人中文字幕av电影| 国产亚洲一区二区三区成人| 亚洲精品一二三区免费| 亚洲男人的天堂久久精品| 欧美日韩亚洲国产三级| 亚洲国产精品欧美激情| 国产区二区三区在线视频|