二 ShardingSphere入門水平切分

2021-10-06 23:15:54 字數 3404 閱讀 9896

之前已經搭建好了基本的專案結構,下面開始資料庫水平的切分,shardingspherejdbc是乙個分表分庫整合工具,他只幫你完成在分表分庫之後的資料整理彙總等工作,不能幫你進行分表分庫,比如你已經有資料庫了想通過shardingspherejdbc進行分庫是不行的。不過最新的4.1.0版本的shardingsphere提出了shardingsphere-scaling用於做資料庫分片遷移,目前處於alpha開發階段,未來應該會有更方便的資料庫切分解決方案。

#  單庫水平切分

spring:

shardingsphere:

# 所有資料庫的別名

datasource:

names: ss1

# 具體資料庫的配置資訊

ss1:

type: com.alibaba.druid.pool.druiddatasource

url: jdbc:mysql:

//localhost:

3306

/ss1?servertimezone=asia/shanghai

driver-

class

-name: com.mysql.cj.jdbc.driver

username: root

password: admin

# 分表分庫策略

sharding:

# 需要分表的策略

tables:

# 表名字首 採坑這裡必須是user 不能是user_ user_會報找不到表切分失敗

user:

# 這裡寫實際的表明 比如 表 user_0 和 user_1 則user_$-

>

或者user_$-

>

actual-data-nodes: ss1.user_$-

>

# 主鍵生成規則 預設使用雪花演算法

key-generator:

column: id

type: snowflake

# 分表策略

table-strategy:

inline:

# 資料表示式計算 這裡是為 id %2==

0 就是user_0 如果 id %2==

1 則user_id

algorithm-expression: user_$-

>

sharding-column: id

props:

sql:

# 列印實際生成sql

show:

true

main:

allow-bean-definition-overriding:

true

@runwith

(springrunner.

class

)@springboottest

public

class

singledatabasehorizontaltest);

}@test

public

void

testfind()

@test

public

void

testfindbycondition()

}

user_0表中所有的id都是偶數,反之user_1的id都是奇數,此時已經實現了對單資料庫的水平切分

#  單庫水平切分

spring:

shardingsphere:

# 所有資料庫的別名

datasource:

# 配置多個資料來源

names: ss1,ss2

# 具體資料庫的配置資訊

ss1:

type: com.alibaba.druid.pool.druiddatasource

url: jdbc:mysql://localhost:3306/ss1?servertimezone=asia/shanghai

driver-class-name: com.mysql.cj.jdbc.driver

username: root

password: admin

ss2:

type: com.alibaba.druid.pool.druiddatasource

url: jdbc:mysql://localhost:3306/ss2?servertimezone=asia/shanghai

driver-class-name: com.mysql.cj.jdbc.driver

username: root

password: admin

# 分表分庫策略

sharding:

# 需要分表的策略

tables:

# 表名字首

user:

actual-data-nodes: ss$->.user_$->

# 主鍵生成規則 預設使用雪花演算法

key-generator:

column: id

type: snowflake

# 分庫策略 與分表策略配置類似

database-strategy:

inline:

shardingcolumn: id

algorithmexpression: ss$

# 分表策略

table-strategy:

inline:

# 資料表示式計算 這裡是為 id % 2 ==0 就是user_0 如果 id % 2==1 則user_1

algorithm-expression: user_$->

sharding-column: id

props:

# 實際生成sql

sql:

show: true

main:

allow-bean-definition-overriding: true

@runwith

(springrunner.

class

)@activeprofiles

("multipledatasourcehorizaontal"

)@springboottest

public

class

multipledatabasehorizontaltest);

}@test

public

void

testfind()

@test

public

void

testfindbycondition()

}

執行測試發現所有偶數id的資料都會加在ss1資料庫中的user_0表中

所有奇數id的資料會儲存在ss2表中的user_1表中

ShardingSphere的預設表分割槽策略的配置

今天專案裡用shardingsphere進行表分割槽,但發現預設的分表策略配置一直不生效,經仔細除錯發現,必須要配置乙個 actual data nodes 才能生效。具體配置如下 spring profiles active cache,global route shardingsphere da...

如何關閉shardingSphere的日誌輸出?

springboot logback1.2.3 公司專案是微服務架構,全部專案工程都是自動化部署的,所有的配置都是統一的。所以,如果某個服務要單獨控制shardingsphere的日誌輸出就很麻煩。根據sharding rule的配置檔案,要不全部關閉,要不都不關閉。所以如果要達成目標,就需要另想辦...

ShardingSphere分庫分表中介軟體

簡介 shardingsphere是一套開源的分布式資料庫中介軟體解決方案組成的生態圈,它由sharding jdbc sharding proxy和sharding sidecar 計畫中 這3款相互獨立的產品組成。他們均提供標準化的資料分片 分布式事務和資料庫治理功能,可適用於如j a同構 異構...