ShardingSphere-Proxy 快速入门

ShardingSphere-Proxy 快速入门

作为透明化的数据库网关,ShardingSphere-Proxy 提供与原生数据库完全兼容的协议,支持多语言应用接入。以下是企业级部署的核心流程:


一、环境准备
  1. 下载安装包

    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
    
  2. 依赖检查

    • Java 8+ (java -version)
    • MySQL 5.7+ 或 PostgreSQL 12+(后端数据库)

二、核心配置文件
  1. 全局配置 (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 # 协议类型
  1. 数据分片配置 (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

三、启动与连接
  1. 启动Proxy服务
bin/start.sh  # Linux
bin/start.bat # Windows
# 默认监听3307端口
  1. MySQL客户端连接
mysql -h127.0.0.1 -P3307 -uroot -proot
  1. 执行操作
-- 创建逻辑表(自动路由到物理库表)
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;

四、核心机制详解
  1. 流量转发原理

    Application
    Proxy:3307
    ds_0:3306
    ds_1:3306
  2. SQL执行流程

    • 协议解析 → SQL解析 → 路由优化 → SQL改写 → 执行引擎 → 结果归并
  3. 分布式主键获取

    SHOW SHARDING TABLE RULES t_order; 
    -- 查看雪花算法生成器状态
    

五、运维管理
  1. 状态监控

    # 查看运行日志
    tail -f logs/stdout.log 
    
    # 监控关键指标
    [INFO ] - [ShardingSphere-Proxy] 
    - Database: sharding_db
    - Active connections: 12
    - Total executions: 342
    
  2. 动态配置更新

    -- 使用DistSQL动态添加数据源
    ADD RESOURCE ds_2 (
      URL="jdbc:mysql://127.0.0.1:3306/demo_ds_2",
      USER=root,
      PASSWORD=root
    );
    
  3. 优雅启停

    bin/stop.sh  # 安全关闭
    

六、生产最佳实践
  1. 高可用部署

    LB
    Proxy-1
    Proxy-2
    DB Cluster
  2. 配置中心集成

    # conf/server.yaml
    mode:
      type: Cluster
      repository:
        type: ZooKeeper
        props:
          namespace: governance
          server-lists: localhost:2181
    
  3. 性能调优参数

    props:
      proxy-backend-query-fetch-size: 1000
      proxy-frontend-executor-size: 16 # IO线程数
      proxy-backend-executor-suitable: OLAP
    

七、常见问题排查
  1. 连接失败

    • 检查防火墙:sudo ufw allow 3307
    • 验证协议兼容性:SHOW VARIABLES LIKE 'version'
  2. 路由异常

    • 确认分片键存在:EXPLAIN SELECT * FROM t_order WHERE user_id=101
    • 检查算法表达式:SHOW SHARDING ALGORITHMS
  3. 性能瓶颈

    • 监控连接池:SHOW TRANSACTION RULES
    • 开启慢查询日志:
      props:
        sql-slow-threshold-milliseconds: 500
      

迁移建议

  • 开发环境使用 JDBC,生产环境使用 Proxy
  • 利用 ShadowDB 实现零停机迁移
  • 通过 UI管理端 简化运维

通过此指南,您已掌握 Proxy 的完整部署流程,下一步可深入探索数据分片算法扩展分布式治理能力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值