0: 數據庫中的主鍵我設置的為 : bigint類型,不是自增,使用mybatis的時候,可以不用自己管理其id,即: n_id,當然這個n_id可以自己生成,不使用框架提供的,但是不建議自增,因為不同表或庫中可能會出現主鍵重復的問題。
1. 搭建基本的sharding-jdbc整合springboot
1. springboot導入sharding-jdbc相關依賴
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile 'org.apache.shardingsphere:sharding-jdbc-spring-boot-starter:4.0.0-RC1' // 這個是
compile 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2'
compile 'mysql:mysql-connector-java:8.0.15'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
compileOnly 'org.projectlombok:lombok:1.18.8'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
2. sharding-jdbc的配spring# 相關的配置 shardingsphere:
# 是否展示sql props: sql: show: true
# 數據源配置 datasource:
# 數據源名稱 names: ds1
# 數據源名稱對應的連接池,url,用戶名和密碼配置 ds1:
# 數據庫連接池類型:HikariDataSource 是springboot自帶的數據庫連接池 type: com.zaxxer.hikari.HikariDataSource
# 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver
# 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 數據庫用戶名 username: root
# 數據庫密碼 password: root
3. 編寫啟動類,springboot單元測試,進行數據庫的插入,即可
2. sharding-jdbc的相關配置,以及分庫分表
1. 同庫水平分表
spring: # 相關的配置 shardingsphere: props: sql: show: true datasource: names: ds1 ds1: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true username: root password: root sharding: tables: # 這個地方注意: sharding-jdbc會根據名稱去找本節點,所以寫sql的時候,要寫此節點的名稱 t_student: # 表達式, 健康節點: 根據上一個節點找到此值, {1..2}為groovy語言,$會替換成{1..2}的一個值,數據庫表是: t_student_1 , t_student_2 # 這個配置是告訴sharding有多少個表 actual-data-nodes: ds1.t_student_$->{1..2} # 主鍵生成策略 key-generator: # 對應的數據庫表的主鍵 column: n_id # 生成方式, 雪花模式 type: SNOWFLAKE # 配置其分片策略和分片算法 table-strategy: # 行表達式 inline: # 配置sharding的計算列 sharding-column: n_id # 配置sharding的表達式,對應的n_id必須和sharding-column的值對應,否則報錯 algorithm-expression: t_student_$->{n_id % 2 +1}
2. 水平分庫,在水平分庫基礎上進行水平分表
spring: # 相關的配置 shardingsphere: props: sql: show: true datasource: # 配置數據源的名稱 names: ds1,ds2 # 第一個數據源 ds1: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 第二個數據源 ds2: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 分庫的策略 sharding: tables: # 這個地方注意: sharding-jdbc會根據名稱去找本節點,所以寫sql的時候,要寫此節點的名稱 t_student: # 配置數據庫的分庫策略 database-strategy: # 行表達式模式 inline: # 選擇需要分庫的字段,根據那個字段進行區分 sharding-column: n_card # 表達式,c_card需要和上面的一致,groovy表達式 algorithm-expression: ds$->{n_card % 2 + 1} # 表達式, 健康節點: 根據上一個節點找到此值, {1..2}為groovy語言,$會替換成{1..2}的一個值,數據庫表是: t_student_1 , t_student_2 # 這個配置是告訴sharding有多少個表 actual-data-nodes: ds1.t_student_$->{1..2} # 主鍵生成策略 key-generator: # 對應的數據庫表的主鍵 column: n_id # 生成方式, 雪花模式 type: SNOWFLAKE # 配置其分片策略和分片算法 table-strategy: # 行表達式 inline: # 配置sharding的計算列 sharding-column: n_id # 配置sharding的表達式,對應的n_id必須和sharding-column的值對應,否則報錯 algorithm-expression: t_student_$->{n_id % 2 +1} mybatis.configuration.map-underscore-to-camel-case: true
3.垂直分庫
spring: # 相關的配置 shardingsphere: props: sql: show: true datasource: # 配置數據源的名稱 names: ds1,ds2,ds3 # 第一個數據源 ds1: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 第二個數據源 ds2: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 配置垂直分庫策略 ds3: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding3?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 分庫的策略 sharding: tables: # 垂直分庫、分表,其實就是寫死了 t_course: database-strategy: inline: sharding-column: n_id algorithm-expression: ds3 # 雖然垂直分庫沒有必要但是還要配置一下分表規則 # table-strategy: # inline: # sharding-column: n_id # algorithm-expression: t_course # 水平分庫、分表 # 這個地方注意: sharding-jdbc會根據名稱去找本節點,所以寫sql的時候,要寫此節點的名稱 t_student: # 配置數據庫的分庫策略 database-strategy: # 行表達式模式 inline: # 選擇需要分庫的字段,根據那個字段進行區分 sharding-column: n_card # 表達式,c_card需要和上面的一致,groovy表達式 algorithm-expression: ds$->{n_card % 2 + 1} # 表達式, 健康節點: 根據上一個節點找到此值, {1..2}為groovy語言,$會替換成{1..2}的一個值,數據庫表是: t_student_1 , t_student_2 # 這個配置是告訴sharding有多少個表 actual-data-nodes: ds1.t_student_$->{1..2} # 主鍵生成策略 key-generator: # 對應的數據庫表的主鍵 column: n_id # 生成方式, 雪花模式 type: SNOWFLAKE # 配置其分片策略和分片算法 table-strategy: # 行表達式 inline: # 配置sharding的計算列 sharding-column: n_id # 配置sharding的表達式,對應的n_id必須和sharding-column的值對應,否則報錯 algorithm-expression: t_student_$->{n_id % 2 +1} mybatis.configuration.map-underscore-to-camel-case: true
4. 讀寫分離,sharding-jdbc只負責路由,即:幫助我們把select或者insert、update、delete路由到不同的數據庫上,但是不會幫助我們同步數據庫數據
spring: # 相關的配置 shardingsphere: props: sql: show: true datasource: # 配置數據源的名稱 names: ds1,ds2,ds3 # 第一個數據源 ds1: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding1?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 第二個數據源 ds2: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding2?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 配置垂直分庫策略 ds3: # 數據庫連接池 type: com.zaxxer.hikari.HikariDataSource # 數據庫驅動 driver-class-name: com.mysql.jdbc.Driver # 數據庫鏈接 jdbcUrl: jdbc:mysql://localhost:3306/sharding3?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true # 數據庫用戶名 username: root # 數據庫密碼 password: root # 分庫的策略 sharding: tables: # 垂直分庫、分表,其實就是寫死了 t_course: database-strategy: inline: sharding-column: n_id algorithm-expression: ds3 # 雖然垂直分庫沒有必要但是還要配置一下分表規則 # table-strategy: # inline: # sharding-column: n_id # algorithm-expression: t_course # 水平分庫、分表 # 這個地方注意: sharding-jdbc會根據名稱去找本節點,所以寫sql的時候,要寫此節點的名稱 t_student: # 配置數據庫的分庫策略 database-strategy: # 行表達式模式 inline: # 選擇需要分庫的字段,根據那個字段進行區分 sharding-column: n_card # 表達式,c_card需要和上面的一致,groovy表達式 algorithm-expression: ds$->{n_card % 2 + 1} # 表達式, 健康節點: 根據上一個節點找到此值, {1..2}為groovy語言,$會替換成{1..2}的一個值,數據庫表是: t_student_1 , t_student_2 # 這個配置是告訴sharding有多少個表 actual-data-nodes: ds1.t_student_$->{1..2} # 主鍵生成策略 key-generator: # 對應的數據庫表的主鍵 column: n_id # 生成方式, 雪花模式 type: SNOWFLAKE # 配置其分片策略和分片算法 table-strategy: # 行表達式 inline: # 配置sharding的計算列 sharding-column: n_id # 配置sharding的表達式,對應的n_id必須和sharding-column的值對應,否則報錯 algorithm-expression: t_student_$->{n_id % 2 +1} # 主從規則,未做驗證 master-slave-rules: # 隨機起一個名稱,如果配置主從,那么需要修改分表策略:::公共表修改 # 分表策略改成: spring.shardingsphere.sharding.tables.t_public.actual-data-nodes=ms0.t_public # ms0 主從已經指向了 ds1、ds2數據源 ms0: # 主節點 master-data-source-name: ds1 # 從節點 slave-data-source-names: ds2 mybatis.configuration.map-underscore-to-camel-case: true