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