springboot + sharding-jdbc + mybatis-plus 快速實(shí)現(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,經(jīng)測試
4.0.0-RC2會有分表規(guī)則不生交往的情況
4.1.1會有自定義主鍵生成SPI的問題

配置:

# shardingjdbc分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名稱
spring.shardingsphere.datasource.names=m1
 
# 一個(gè)實(shí)體類對應(yīng)兩張表,覆蓋
spring.main.allow-bean-definition-overriding=true
 
#配置數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動,地址,用戶名和密碼
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

#指定表分布情況,配置表在哪個(gè)數(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.lxweimin.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出錯(cuò):{}", e.getMessage());
        }
        return null;
    }

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

}

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

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

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 資源根目錄排除各環(huán)境的配置,使用單獨(dú)的資源目錄來指定 -->
                <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踩坑實(shí)記

環(huán)境概況:
組件 版本
springboot 2.1.1.RELEASE
mybatis-plus-boot-starter 3.0.7.1
sharding-jdbc-spring-boot-starter 4.0.0-RC1
踩坑記錄:
報(bào)錯(cuò)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

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

sql中含有now()函數(shù)調(diào)用,程序報(bào)錯(cuò)
解決方案:service生成時(shí)間戳傳給dao,sql中直接參數(shù)替換,剔除now函數(shù)(看來是組件還不支持這個(gè)函數(shù)解析)

報(bào)sql表達(dá)式錯(cuò)誤,表中含有字段“desc”,因?yàn)閐esc同樣為mysql關(guān)鍵字,導(dǎo)致insert時(shí),sql解析出問題,影響了最終參數(shù)設(shè)置,程序報(bào)錯(cuò)。(update/select時(shí)都不會影響,唯有insert時(shí)出錯(cuò))
解決方案:將參數(shù)改為description,避開mysql關(guān)鍵字

報(bào)錯(cuò)缺少antlr4-runtime
解決方案:加入依賴

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

報(bào)錯(cuò)沒有收到mysql server回應(yīng),感覺就是沒連上mysql
解決方案:忘了…(我記得我把maven依賴整體了一下,無用的依賴剔除掉,貌似就可以了)

寫了幾個(gè)demo,如有需要可以自行獲取:https://github.com/naget/sharding

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,748評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,165評論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,595評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,633評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,435評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,943評論 1 321
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,035評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,175評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,713評論 1 333
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,599評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,788評論 1 369
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,303評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,034評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,412評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,664評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,408評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,747評論 2 370

推薦閱讀更多精彩內(nèi)容