Sharding-Proxy是单独的一个应用,需要独立部署。
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.分库分表和读写分离配置:
解压后,进入conf目录,里面有对应的yaml配置文件,Sharding-Proxy支持多逻辑数据源,每个以config-前缀命名的yaml配置文件,即为一个逻辑数据源。这里配置了2个例子,一个是分库分表的,一个是读写分离的。
分库分表,文件名为config-sharding.yaml,具体内容如下:
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,具体内容如下
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