Sharding-Proxy實(shí)戰(zhàn)(一)

Sharding-Proxy是單獨(dú)的一個(gè)應(yīng)用,需要獨(dú)立部署。

1. 下載安裝包
[root@sit_mysql1 ~]# cd /usr/local/src/
[root@sit_mysql1 src]# wget https://github.com/sharding-sphere/sharding-sphere-doc/raw/master/dist/sharding-proxy-3.0.0.tar.gz
2. 解壓縮
[root@sit_mysql1 src]# tar zxvf sharding-proxy-3.0.0.tar.gz -C /usr/local/
3.分庫(kù)分表和讀寫分離配置:

解壓后,進(jìn)入conf目錄,里面有對(duì)應(yīng)的yaml配置文件,Sharding-Proxy支持多邏輯數(shù)據(jù)源,每個(gè)以config-前綴命名的yaml配置文件,即為一個(gè)邏輯數(shù)據(jù)源。這里配置了2個(gè)例子,一個(gè)是分庫(kù)分表的,一個(gè)是讀寫分離的。
分庫(kù)分表,文件名為config-sharding.yaml,具體內(nèi)容如下

schemaName: sharding_db

dataSources:
  ds0: 
    url: jdbc:mysql://10.0.0.1:3306/ds0
    username: dba_01
    password: 123456
    autoCommit: true
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    maximumPoolSize: 65
  ds1:
    url: jdbc:mysql://10.0.0.2:3306/ds1
    username: dba_01
    password: 123456
    autoCommit: true
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    maximumPoolSize: 65

shardingRule:  
  tables:
    t_order: 
      actualDataNodes: ds${0..1}.t_order${0..1}
      tableStrategy: 
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order${order_id % 2}
      keyGeneratorColumnName: order_id
    t_order_item:
      actualDataNodes: ds${0..1}.t_order_item${0..1}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_item${order_id % 2}  
  bindingTables:
    - t_order,t_order_item
  defaultDatabaseStrategy:
    inline:
      shardingColumn: user_id
      algorithmExpression: ds${user_id % 2}
  defaultTableStrategy:
    none:
  defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator

讀寫分離,文件名為config-master_slave.yaml,具體內(nèi)容如下

schemaName: master_slave_db

dataSources:
  ds_master:
    url: jdbc:mysql://10.0.0.1:3888/shardingdb
    username: dba_01
    password: 123456
    autoCommit: true
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    maximumPoolSize: 65
  ds_slave0:
    url: jdbc:mysql://10.0.0.2:3888/shardingdb
    username: dba_01
    password: 123456
    autoCommit: true
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    maximumPoolSize: 65 

masterSlaveRule:
  name: ds_ms
  masterDataSourceName: ds_master
  slaveDataSourceNames: 
    - ds_slave0
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容