ShardingSphere-Proxy 快速入门
作为透明化的数据库网关,ShardingSphere-Proxy 提供与原生数据库完全兼容的协议,支持多语言应用接入。以下是企业级部署的核心流程:
一、环境准备
-
下载安装包
wget https://archive.apache.org/dist/shardingsphere/5.3.2/apache-shardingsphere-5.3.2-shardingsphere-proxy-bin.tar.gz tar -zxvf apache-shardingsphere-5.3.2-shardingsphere-proxy-bin.tar.gz cd apache-shardingsphere-5.3.2-shardingsphere-proxy-bin
-
依赖检查
- Java 8+ (
java -version
) - MySQL 5.7+ 或 PostgreSQL 12+(后端数据库)
- Java 8+ (
二、核心配置文件
- 全局配置 (
conf/server.yaml
)
rules:
- !AUTHORITY
users:
- root@%:root # 用户名: root, 密码: root
- sharding@:sharding123
provider:
type: NATIVE
props:
sql-show: true # 开启SQL日志
proxy-frontend-database-protocol-type: MySQL # 协议类型
- 数据分片配置 (
conf/config-sharding.yaml
)
databaseName: sharding_db # 逻辑数据库名
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: root
rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
databaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database_inline
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: table_inline
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
shardingAlgorithms:
database_inline:
type: INLINE
props:
algorithm-expression: ds_${user_id % 2}
table_inline:
type: INLINE
props:
algorithm-expression: t_order_${order_id % 2}
keyGenerators:
snowflake:
type: SNOWFLAKE
三、启动与连接
- 启动Proxy服务
bin/start.sh # Linux
bin/start.bat # Windows
# 默认监听3307端口
- MySQL客户端连接
mysql -h127.0.0.1 -P3307 -uroot -proot
- 执行操作
-- 创建逻辑表(自动路由到物理库表)
CREATE TABLE t_order (
order_id BIGINT NOT NULL,
user_id INT NOT NULL,
status VARCHAR(50),
PRIMARY KEY (order_id)
);
-- 插入数据
INSERT INTO t_order (user_id, status) VALUES (101, 'PAID');
-- 路由到 ds_1.t_order_1 (101%2=1)
-- 查询数据(自动聚合)
SELECT * FROM t_order WHERE user_id=101;
四、核心机制详解
-
流量转发原理
-
SQL执行流程
- 协议解析 → SQL解析 → 路由优化 → SQL改写 → 执行引擎 → 结果归并
-
分布式主键获取
SHOW SHARDING TABLE RULES t_order; -- 查看雪花算法生成器状态
五、运维管理
-
状态监控
# 查看运行日志 tail -f logs/stdout.log # 监控关键指标 [INFO ] - [ShardingSphere-Proxy] - Database: sharding_db - Active connections: 12 - Total executions: 342
-
动态配置更新
-- 使用DistSQL动态添加数据源 ADD RESOURCE ds_2 ( URL="jdbc:mysql://127.0.0.1:3306/demo_ds_2", USER=root, PASSWORD=root );
-
优雅启停
bin/stop.sh # 安全关闭
六、生产最佳实践
-
高可用部署
-
配置中心集成
# conf/server.yaml mode: type: Cluster repository: type: ZooKeeper props: namespace: governance server-lists: localhost:2181
-
性能调优参数
props: proxy-backend-query-fetch-size: 1000 proxy-frontend-executor-size: 16 # IO线程数 proxy-backend-executor-suitable: OLAP
七、常见问题排查
-
连接失败
- 检查防火墙:
sudo ufw allow 3307
- 验证协议兼容性:
SHOW VARIABLES LIKE 'version'
- 检查防火墙:
-
路由异常
- 确认分片键存在:
EXPLAIN SELECT * FROM t_order WHERE user_id=101
- 检查算法表达式:
SHOW SHARDING ALGORITHMS
- 确认分片键存在:
-
性能瓶颈
- 监控连接池:
SHOW TRANSACTION RULES
- 开启慢查询日志:
props: sql-slow-threshold-milliseconds: 500
- 监控连接池:
迁移建议: