下载安装
添加MySQL的Driver
下载mysql-connector-java-5.1.48.jar,将其放到Sharding-Proxy的lib文件夹里
修改配置文件
配置账号密码
server.yaml
authentication:
users:
root:
password: root
# sharding:
# password: sharding
# authorizedSchemas: sharding_db
#
#props:
# max.connections.size.per.query: 1
# acceptor.size: 16 # The default value is available processors count * 2.
# executor.size: 16 # Infinite by default.
# proxy.frontend.flush.threshold: 128 # The default value is 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
# proxy.opentracing.enabled: false
# query.with.cipher.column: true
# sql.show: false
配置读写分离
config-master_slave.yaml
schemaName: master_slave_db
dataSources:
master_ds:
url: jdbc:mysql://localhost:3306/demo_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 654321@qq.com
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
slave_ds_0:
url: jdbc:mysql://localhost:3308/demo_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 654321@qq.com
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
masterSlaveRule:
name: ms_ds
masterDataSourceName: master_ds
slaveDataSourceNames:
- slave_ds_0
注意:实现读写分离的前提是配置的数据库首先要实现主从复制的功能,否则读写分离不能实现
配置数据分片
config-sharding.yaml
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://localhost:3306/sharding_master_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 654321@qq.com
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://localhost:3306/sharding_master_2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 654321@qq.com
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
t_user:
actualDataNodes: ds_0.user
t_student:
actualDataNodes: ds_1.student
bindingTables:
- t_user,t_student
defaultTableStrategy:
none:
运行
命令行运行bin里面的start.bat,默认端口号是3307,可以在start.bat后面带上其他端口号
D:\sharding-proxy\bin>start.bat 3308
运行结果如下: