sharding-proxy代理分庫分表配置

原理看官網:https://shardingsphere.apache.org/document/current/cn/overview/

sharding proxy代理mysql實現分庫的步驟:

  1. 下載

wget https://mirrors.tuna.tsinghua.edu.cn/apache/shardingsphere/4.1.0/apache-shardingsphere-4.1.0-sharding-proxy-bin.tar.gz

  1. 解壓

tar xvf apache-shardingsphere-4.1.0-sharding-proxy-bin.tar.gz

  1. 使用mysql數據庫時需要自己導入運行工具:

(1) 下載

wget https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.47.tar.gz

(2) 解壓:

tar xvf mysql-connector-java-5.1.47

(3) 把運行 jar複製到sharding proxy lib/

cd cd mysql-connector-java-5.1.47

cp mysql-connector-java-5.1.47.jar /_mytools/sharding-proxy/v4.1.0/lib/

  1. sharding proxy基本配置server.yaml

cd apache-shardingsphere-4.1.0-sharding-proxy-bin/conf/

 

#用戶配置

authentication:

  users: #用戶列表

    root: #名爲root的用戶

      password: root  #連接密碼

    sharding: #名爲 sharding的用戶

      password: sharding #密碼

      authorizedSchemas: sharding_db #爲sharding用戶配置管理表的權限,不配置時爲全部權限

 

#sharding proxy 基本參數配置

props:

  max.connections.size.per.query: 1 #每個查詢可以打開的最大連接數量,默認爲1

  acceptor.size: 12  # 用於設置接收客戶端請求的工作線程個數,默認爲CPU核數*2.

  executor.size: 6  # 工作線程數量,默認值: CPU核數

  proxy.frontend.flush.threshold: 128  # 對於單個大查詢,每多少個網絡包返回一次 128.

#     LOCAL: Proxy will run with LOCAL transaction.

#     XA: Proxy will run with XA transaction.

#     BASE: Proxy will run with B.A.S.E transaction.

  proxy.transaction.type: LOCAL #默認爲LOCAL事務,允許LOCAL,XA,BASE三個值,XA採用Atomikos作爲事務管理器,BASE類型需要拷貝實現ShardingTransactionManager的接口的jar包至lib目錄中

  proxy.opentracing.enabled: false  #是否開啓鏈路追蹤功能,默認爲不開啓

  proxy.hint.enabled: false  #是否啓用hint算法強制路由 默認false

  query.with.cipher.column: true #是否使用密文列查詢 默認false

  sql.show: false  #是否打印sql 默認false

  allow.range.query.with.inline.sharding: false  #允許範圍查詢,默認爲 false ,要是我們分庫分表是水平切分,可以想得到範圍查詢會像廣播去查每一個表,比較耗性能能

 

 

  1. 數據源、分片配置config-sharding.yaml

(1) 邏輯庫名稱

schemaName: sharding_db

 

(2) 數據源配置


dataSources:
#數據源別名,保證唯一且最好與分片列保持規律性,
  ds_37:
#數據源地址,後面是爲了統一日期和管理亂碼,配置時物理庫必須存在
    url: jdbc:mysql://192.168.2.170:3306/ds37?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
#連接超時時間
connectionTimeoutMilliseconds: 30000
#空閒連接回收超時毫秒數
idleTimeoutMilliseconds: 60000
#連接最大存活時間毫秒數
maxLifetimeMilliseconds: 1800000
#最大連接數
maxPoolSize: 50
ds_45:
url: jdbc:mysql://192.168.2.170:3306/ds45?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50

 

(3) 分片規則


shardingRule:
tables: #數據分片規則,可配置多個
seal_data: #邏輯庫中的邏輯表名
actualDataNodes: ds_${['37','45']}.seal_data #規則節點,邏輯數據源+邏輯表名 支持inline表達式。(缺省表示自動匹配全部邏輯庫和邏輯表組合,每個庫需要一個邏輯表,若沒有自動創建一個,適用於只分庫不分表且分庫的邏輯表結構完全一致的情況)
# tableStrategy: #分表規則屬性
# inline: #分表規則算法
# shardingColumn: area_code  #分表分片列
# algorithmExpression: seal_data_${area_code.toInteger().intdiv(100)} #分表規則表達式
# keyGenerator:  #分表主鍵屬性
# type: SNOWFLAKE #主鍵生成算法
# column: seal_code #主鍵列
# bindingTables: # 綁定關係表
# - seal_data
databaseStrategy: #分庫屬性
standard:  #使用standard算法
shardingColumn: STR_AREA_CODE #分庫分片列
preciseAlgorithmClassName: com.CustomShardingTest #自定義的分庫算法(製作方式)
defaultDatabaseStrategy: #默認分庫規則
none:
# inline:
# shardingColumn: STR_AREA_CODE
# algorithmExpression: ds_${STR_AREA_CODE.toInteger().intdiv(10000)}
# preciseAlgorithmClassName: splitDB-1.0-SNAPSHOT.jar
defaultTableStrategy:   #默認分表規則
none:

 

(4) 分片策略,官網有https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-yaml/

 

 

  1. 其他:

(1) 啓動:

安裝目錄:/_mytools/sharding-proxy/v4.1.0/bin/start.sh

(2) 連接:

服務ip:3307   用戶名和密碼由上方配置獲取

(3) 使用Navicat連接查看邏輯表時,注意數據庫編碼問題,可能導致字段長度被限制顯示不完整

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章