下载安装
添加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_dbdataSources:master_ds:url: jdbc:mysql://localhost:3306/demo_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 654321@qq.comconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50slave_ds_0:url: jdbc:mysql://localhost:3308/demo_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 654321@qq.comconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50masterSlaveRule:name: ms_dsmasterDataSourceName: master_dsslaveDataSourceNames:- slave_ds_0
注意:实现读写分离的前提是配置的数据库首先要实现主从复制的功能,否则读写分离不能实现
配置数据分片
config-sharding.yaml
schemaName: sharding_dbdataSources:ds_0:url: jdbc:mysql://localhost:3306/sharding_master_1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 654321@qq.comconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50ds_1:url: jdbc:mysql://localhost:3306/sharding_master_2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 654321@qq.comconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50shardingRule:tables:t_user:actualDataNodes: ds_0.usert_student:actualDataNodes: ds_1.studentbindingTables:- t_user,t_studentdefaultTableStrategy:none:
运行
命令行运行bin里面的start.bat,默认端口号是3307,可以在start.bat后面带上其他端口号
D:\sharding-proxy\bin>start.bat 3308
运行结果如下:

