sharding-proxy定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供MySQL/PostgreSQL版本,它可以使用任何兼容MySQL/PostgreSQL协议的访问客户端(如:MySQL Command Client, MySQL Workbench, Navicat等)操作数据,对DBA更加友好。
Sharding-JDBC | Sharding-Proxy | |
---|---|---|
数据库 | 任意 | MySQL |
连接消耗数 | 高 | 低 |
异构语言 | JAVA | 任意 |
性能 | 损耗低 | 损耗略高 |
无中心化 | 高是 | 否 |
静态入口 | 无 | 有 |
——摘自官方文档
服务器 | 名称 | 数据库 | 表 | 角色 |
---|---|---|---|---|
192.168.30.130 | server1 | orders | orders | 独立 |
192.168.30.131 | server2 | orders | orders,user | 独立 |
192.168.30.132 | server3 | test | t_user | 主 |
192.168.30.133 | server4 | test | t_user | 从(只读) |
目标:
到官方网站下载sharding-jdbc的二进制安装包,本例使用4.1.1版本。
在/opt目录解压。
修改conf文件夹下面的配置文件:
server.yaml:
authentication:
users:
root:
password: 123456
sharding:
password: sharding
authorizedSchemas: sharding_db
config-sharding.yaml:
######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################
schemaName: sharding_db
#
dataSources:
# server1
ds_0:
url: jdbc:mysql://192.168.30.130:3306/orders?serverTimezone=GMT%2B8&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
#server2
ds_1:
url: jdbc:mysql://192.168.30.131:3306/orders?serverTimezone=GMT%2B8&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
# server3 主
ds_m_0:
url: jdbc:mysql://192.168.30.132:3306/test?serverTimezone=GMT%2B8&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
# server4 从
ds_s_0:
url: jdbc:mysql://192.168.30.133:3306/test?serverTimezone=GMT%2B8&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
# orders表,根据customer_id判断分表的策略
orders:
actualDataNodes: ds_${0..1}.orders
databaseStrategy:
inline:
shardingColumn: customer_id
algorithmExpression: ds_${customer_id % 2}
# user表,存放在ds_1即server2的user表中
user:
actualDataNodes: ds_$->{1}.user
# t_user,使用读写分离进行读和写操作,规则命名为m_s_0(见下面读写分离规则配置)
t_user:
actualDataNodes: m_s_$->{0}.t_user
#读写分离规则
masterSlaveRules:
m_s_0:
masterDataSourceName: ds_m_0
slaveDataSourceNames: [ds_s_0]
在bin目录下,启动sharding-proxy:
[root@server3 bin]# ./start.sh
Starting the Sharding-Proxy ...
The classpath is .:..:/opt/sharding-proxy-4.1.1/lib/*:/opt/sharding-proxy-4.1.1/lib/*:/opt/sharding-proxy-4.1.1/ext-lib/*
Please check the STDOUT file: /opt/sharding-proxy-4.1.1/logs/stdout.log
[root@server3 bin]# pwd
/opt/sharding-proxy-4.1.1/bin
默认端口为3307
insert into user (id, name) values (1,'a');
insert into user (id, name) values (2,'b');
全部存储在server2的orders库的user表中。
insert into orders (id, order_type,customer_id,amount) values (1,100,100,1000);
insert into orders (id, order_type,customer_id,amount) values (2,101,101,2000);
insert into orders (id, order_type,customer_id,amount) values (3,100,102,3000);
insert into orders (id, order_type,customer_id,amount) values (4,101,103,4000);
customer_id为100和102的存储到server1中,customer_id为101和103的存储到server2中
insert into t_user (uid,uname) values (1,@@hostname);
server3中的数据为1-server3;server4中的数据为1-server4,写操作通过server3实现(server4只读模式);
查询:
select * from t_user;
获取到server4中的数据,读操作通过server4实现。