springboot + sharding-jdbc + mybatis-plus 快速實現(xiàn)分庫分表

pom.xml

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

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
        </dependency>

這里采用的版本是4.0.0-RC1,經測試
4.0.0-RC2會有分表規(guī)則不生交往的情況
4.1.1會有自定義主鍵生成SPI的問題

配置:

# shardingjdbc分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名稱
spring.shardingsphere.datasource.names=m1
 
# 一個實體類對應兩張表概耻,覆蓋
spring.main.allow-bean-definition-overriding=true
 
#配置數(shù)據(jù)源具體內容母剥,包含連接池十嘿,驅動箭养,地址旁壮,用戶名和密碼
spring.shardingsphere.datasource.m1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.jdbc-url=jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
spring.shardingsphere.datasource.m1.username=uuu
spring.shardingsphere.datasource.m1.password=ppp

#指定表分布情況婿牍,配置表在哪個數(shù)據(jù)庫里面侈贷,表名稱都是什么  m1.course_1 , m1.course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{1..64}
 
# 指定表里面主鍵生成策略SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=id
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
 
# 指定分片策略
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=corp_id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course$->{corp_id % 64 + 1}
 
# 打開sql輸出日志
spring.shardingsphere.props.sql.show=true

參考: http://www.reibang.com/p/a58c3b1abb9a

druid:


spring.shardingsphere.datasource.names=m1
 
spring.main.allow-bean-definition-overriding=true
 
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false

spring.shardingsphere.datasource.m1.username=rrrr
spring.shardingsphere.datasource.m1.password=___rM11xI2Pemzr6EerD4xY1A==___
spring.shardingsphere.datasource.m1.connection-properties=password=___rM11xI2Pemzr6EerD4xY1A==___
spring.shardingsphere.datasource.m1.password-callback-class-name=cn.xxx.ddd.callback.DBPasswordCallback

spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course$->{1..64}
 
spring.shardingsphere.sharding.tables.course.key-generator.column=id
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE
 
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=corp_id
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course$->{corp_id % 64 + 1}
 
spring.shardingsphere.props.sql.show=true

自定義主鍵生成類:

spring.shardingsphere.sharding.tables.t_chat_group.key-generator.column=id
#spring.shardingsphere.sharding.tables.t_chat_group.key-generator.type=SNOWFLAKE
spring.shardingsphere.sharding.tables.t_chat_group.key-generator.type=MyShardingKey
@Data
@Slf4j
public class MyShardingKeyGenerator implements ShardingKeyGenerator {

    private Properties properties;

    @Override
    public Comparable<?> generateKey() {
        try {
            return SnowflakeIdUtils.generateId();
        } catch (Exception e) {
            log.error("生成雪花id出錯:{}", e.getMessage());
        }
        return null;
    }

    @Override
    public String getType() {
        return "MyShardingKey";
    }

}

創(chuàng)建目錄:META/services
在其下建文件:org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator
內容寫上上面自定義的MyShardingKeyGenerator的全類名

同時在pom文件里資源配置里,各個環(huán)境都需加上

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 資源根目錄排除各環(huán)境的配置等脂,使用單獨的資源目錄來指定 -->
                <includes>
                    <include>*/*</include>
                    <include>META-INF/dubbo/com.alibaba.dubbo.rpc.Filter</include>
                    <include>META-INF/app.properties</include>
                    <include>META-INF/services/*</include>
                    <include>static/**</include>
                </includes>
                <!-- 是否替換資源中的屬性 -->
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
   </build>

shardingsphere踩坑實記

環(huán)境概況:
組件 版本
springboot 2.1.1.RELEASE
mybatis-plus-boot-starter 3.0.7.1
sharding-jdbc-spring-boot-starter 4.0.0-RC1
踩坑記錄:
報錯datasource不能完成注冊(The bean ‘dataSource’, defined in class path resource [io/shardingjdbc/spring/boot/SpringBootConfiguration.class], could not be registered.)
解決方案:根據(jù)提示加入配置spring.main.allow-bean-definition-overriding=true

實際執(zhí)行的sql與邏輯sql相同俏蛮,沒有起到分表作用
解決方案:將sharding-jdbc-spring-boot-starter的版本號改為4.0.0-RC1就正常了(RC2和RC3均異常,很無奈上遥,不知道是不是我這邊環(huán)境有問題)

sql中含有now()函數(shù)調用搏屑,程序報錯
解決方案:service生成時間戳傳給dao,sql中直接參數(shù)替換粉楚,剔除now函數(shù)(看來是組件還不支持這個函數(shù)解析)

報sql表達式錯誤辣恋,表中含有字段“desc”,因為desc同樣為mysql關鍵字,導致insert時模软,sql解析出問題伟骨,影響了最終參數(shù)設置,程序報錯燃异。(update/select時都不會影響携狭,唯有insert時出錯)
解決方案:將參數(shù)改為description,避開mysql關鍵字

報錯缺少antlr4-runtime
解決方案:加入依賴

        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-runtime</artifactId>
            <version>4.7.1</version>
        </dependency>

報錯沒有收到mysql server回應回俐,感覺就是沒連上mysql
解決方案:忘了…(我記得我把maven依賴整體了一下逛腿,無用的依賴剔除掉稀并,貌似就可以了)

寫了幾個demo,如有需要可以自行獲鹊ツ:https://github.com/naget/sharding

原文鏈接:https://blog.csdn.net/qq_33240946/article/details/103506983

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末碘举,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子雕凹,更是在濱河造成了極大的恐慌殴俱,老刑警劉巖政冻,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件枚抵,死亡現(xiàn)場離奇詭異,居然都是意外死亡明场,警方通過查閱死者的電腦和手機汽摹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來苦锨,“玉大人逼泣,你說我怎么就攤上這事≈凼妫” “怎么了拉庶?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長秃励。 經常有香客問我氏仗,道長,這世上最難降的妖魔是什么夺鲜? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任皆尔,我火速辦了婚禮,結果婚禮上币励,老公的妹妹穿的比我還像新娘慷蠕。我一直安慰自己,他們只是感情好食呻,可當我...
    茶點故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布流炕。 她就那樣靜靜地躺著,像睡著了一般仅胞。 火紅的嫁衣襯著肌膚如雪浪感。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天饼问,我揣著相機與錄音影兽,去河邊找鬼。 笑死莱革,一個胖子當著我的面吹牛峻堰,可吹牛的內容都是我干的讹开。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼捐名,長吁一口氣:“原來是場噩夢啊……” “哼旦万!你這毒婦竟也來了?” 一聲冷哼從身側響起镶蹋,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤成艘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后贺归,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體淆两,經...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年拂酣,在試婚紗的時候發(fā)現(xiàn)自己被綠了秋冰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,488評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡婶熬,死狀恐怖剑勾,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情赵颅,我是刑警寧澤虽另,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站饺谬,受9級特大地震影響捂刺,放射性物質發(fā)生泄漏。R本人自食惡果不足惜商蕴,卻給世界環(huán)境...
    茶點故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一叠萍、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧绪商,春花似錦苛谷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至例书,卻和暖如春锣尉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背决采。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工自沧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓拇厢,卻偏偏與公主長得像爱谁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子孝偎,可洞房花燭夜當晚...
    茶點故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內容