欢迎来到Introzo百科
Introzo百科
当前位置:网站首页 > 技术 > ShardingSphere-Proxy 子库和表

ShardingSphere-Proxy 子库和表

日期:2023-09-29 06:52

安装ShardingSphere-Proxy

中间件封装

定位为透明的数据库代理,提供封装数据库二进制协议的服务器版本,支持异构语言。目前 MySQL 和 PostgreSQL 版本可用。可以使用任何兼容MySQL/PostgreSQL协议的访问客户端(如MySQL Command Client、MySQL Workbench、Navicat等)来操作数据,对DBA更加友好。


5.1.1 安装:

https://www.introzo.com/document/5.1.1/cn/user-manual/shardingsphere-proxy/startup/bin/)https://www.introzo.com/dist/shardingsphere/

下载jar包后,上传到/usr/local文件并解压

tar -zxvf apache-shardingsphere-5.1.1-shardingsphere-proxy-bin.tar.gz

新建ext-lib目录

cd /usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-binmkdir ext-lib

然后将MySQL驱动mysql-connector-java-8.0.22.jar放入ext-lib目录

修改配置文件

cd /usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/confvim server.yaml

左侧如下配置表示任何服务器都可以root用户登录,并拥有所有权限

规则:- !AUTHORITY用户:- root@%:rootprovider:type: ALL_PRIVILEGES_PERMITTED
# 打印sql
道具:sql-show:true

开始:

/usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/bin/www.introzo.com 3306 也可以指定端口号和配置文件目录:`bin/start.bat ${proxy_port} ${ proxy_conf_directory}`,如果不指定,则为3307端口

停止:

/usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/bin/www.introzo.com

检查启动状态

 ps -ef|grep 分片球|grep -v grep

开放端口:

firewall-cmd --zone=public --add-port=3306/tcp --permanent

重新启动防火墙:

firewall-cmd --reload #restart
防火墙 systemctl stop firewalld.service #Stopfirewall systemctl disablefirewalld.service #禁用防火墙启动

远程连接

mysql -h192.168.158.166 -p3306 -uroot -p

连接成功


也可用于视觉工具连接和正常使用mysql没有什么区别

配置读写分离

/conf目录下

有以下配置文件


修改配置文件config-read write -splitting.yaml

cd /usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/confvim config-readwrite-splitting.yaml
schemaName: readwrite_splitting_db数据源:db1: url:jdbc:mysql://192.168.158.134:3306/distributed_server?serverTimezone=UTC &useUnicode=true&characterEncoding=UTF-8&useSSL=false用户名密码: psh120370连接超时毫秒: 30000空闲超时毫秒: 60000maxLifetime毫秒: 1800000maxPoolSize: 50minPoolSize: 1slave1: url:jdbc:mysql://192.168.158.146:3306/distributed_server?serverTimezone= UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false用户名 : root密码: psh120370连接超时毫秒: 30000空闲超时毫秒: 60000maxLifetime毫秒: 1800000maxPoolSize: 50minPoolSize: 1db0:网址jdbc:mysql://192.168.158.165:3306/分布式_server?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF- 8&useSSL=false用户名:密码: psh120370连接超时毫秒: 30000空闲超时毫秒: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize:1规则- !READWRITE_SPLITTING数据源:readwrite_ds:类型:静态props写入数据源-名称: db1读取数据源名称:slave1

重新启动:

/usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/bin/www.introzo.com 3306

对 readwrite_splitting_db 的查询就会象征slave1上,写就会象征db1上,slave1 是db1的从库,readwrite_splitting_db就是一个代理库

通过springboot整合ShardingSphere-Proxy,不需要添加任何额外的依赖,在配置数据源的时候需要连接上面的逻辑库readwrite_splitting_db

spring:应用程序:名称:shardingProxy云 :nacos:发现:服务器地址: http://192.168.158.135:8848命名空间: d3a69e71-1c55-411c-940a-1f275c8c7bea 用户名: nacos密码: nacos 数据源:druid:驱动程序类名称: com.mysql.cj.jdbc.Driver网址:jdbc:mysql://192.168.158.166:3306 /readwrite_splitting_db?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF -8&useSSL=false用户名:密码:

水平分片

表结构和查询逻辑见:

https://www.introzo.com/persistence_PSH/article/details/131367613

修改配置config-sharding.yaml

cd /usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/confvim config-sharding.yaml

这里的schemaName需要和上面的读写分离逻辑库的名称不一致。其实spring boot中原来的配置是写入ShardingSphere-Proxy

schemaName: sharding_db数据源:db1:url :jdbc:mysql://192.168.158.134:3306/distributed_server?serverTimezone=UTC &useUnicode=true&characterEncoding=UTF-8&useSSL=false用户名密码: psh120370连接超时毫秒: 30000空闲超时毫秒: 60000maxLifetime毫秒: 1800000maxPoolSize: 50minPoolSize: 1slave1: url:jdbc:mysql://192.168.158.146:3306/distributed_server?serverTimezone= UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false用户名 : root密码: psh120370连接超时毫秒: 30000空闲超时毫秒: 60000maxLifetime毫秒: 1800000maxPoolSize: 50minPoolSize: 1db0:url:jdbc:mysql://192.168.158.165:3306/distributed_server? serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false用户名:密码: psh120370连接超时毫秒: 30000idleTimeout毫秒: 60000maxLifetime毫秒 1800000maxPoolSize 50minPoolSize 1规则- !分片:测试顺序:实际数据节点: db$->{0..1}.test_order$->{0..1}数据库策略 :标准:分片列:账户分片算法名称: inline_accounttableStrategy:标准:shardingColumn: order_noshardingAlgorithmName: hash_modkeyGenerateStrategy :: idkeyGeneratorName :雪花test_order_item:实际数据节点:db$->{0..1}.test_order_item$->{0..1}数据库策略 :标准分片列:账户分片算法名称:inline_account表策略:标准:shardingColumn: order_noshardingAlgorithmName : hash_modkeyGenerateStrategy:: idkeyGeneratorName:雪花绑定表:- test_order ,test_order_item分片算法:inline_account:类型: HASH_MODprops:分片计数: 2hash_mod类型: HASH_MOD道具:分片计数:2keyGenerators :雪花类型 :雪花

springboot配置文件

spring:应用程序:名称:shardingProxy云 :nacos:发现:服务器地址: http://192.168.158.135:8848命名空间: d3a69e71-1c55-411c-940a-1f275c8c7bea 用户名: nacos密码: nacos 数据源:druid:驱动程序类名称: com.mysql.cj.jdbc.Driver网址:jdbc:mysql://192.168.158.166:3306 /sharding_db?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF -8&useSSL=false用户名:密码:

查询成功:

关灯