技术博客 技术博客
  • JAVA
  • 仓颉
  • 设计模式
  • 人工智能
  • Spring
  • Mybatis
  • Maven
  • Git
  • Kafka
  • RabbitMQ
  • RocketMQ
  • Redis
  • Zookeeper
  • Nginx
  • 数据库套件
  • MySQL
  • Elasticsearch
  • MongoDB
  • Hadoop
  • ClickHouse
  • Hbase
  • Hive
  • Flink
  • Flume
  • SQLite
  • linux
  • Docker
  • Jenkins
  • Kubernetes
  • 工具
  • 前端
  • AI
GitHub (opens new window)
  • JAVA
  • 仓颉
  • 设计模式
  • 人工智能
  • Spring
  • Mybatis
  • Maven
  • Git
  • Kafka
  • RabbitMQ
  • RocketMQ
  • Redis
  • Zookeeper
  • Nginx
  • 数据库套件
  • MySQL
  • Elasticsearch
  • MongoDB
  • Hadoop
  • ClickHouse
  • Hbase
  • Hive
  • Flink
  • Flume
  • SQLite
  • linux
  • Docker
  • Jenkins
  • Kubernetes
  • 工具
  • 前端
  • AI
GitHub (opens new window)
  • kafka

    • kafka-2.7.0 基本概念
    • Kafka-2.7.0 搭建及参数解析
    • kafka-2.7.0 spring boot 集成 kafka
    • kafka-2.7.0 kafka Connect
    • kafka-2.7.0 Kafka Streams 流处理
  • RabbitMQ

    • rabbitmq 简介
  • RocketMQ

    • RocketMQ 基础概念
    • RocketMQ 搭建
    • RocketMQ 整合spring boot
  • redis

    • Redis 介绍及安装
    • Redis 命令介绍
    • Redis 分布式锁介绍
    • Redis 事务介绍
    • Redis 的key失效通知介绍
    • Redis 配置文件解读
    • Redis 记一次宕机排查
    • Redis 高可用(一) 主从理论
    • Redis 高可用(二) 哨兵理论
    • Redis 高可用(三) 搭建
    • Redis 集群搭建
  • zookeeper

    • Zookeeper 介绍及安装
    • Zookeeper 做为锁使用
  • nginx

    • nginx-1.18.0 安装
    • nginx 常见问题总结
    • nginx 高可用
  • 数据库套件

    • MyCat 1.6.7(一)MySQL高可用及分库分表
    • MyCat 1.6.7(二)高可用及权限
    • shardingsphere 4.x(一)Sharding-JDBC使用
    • shardingsphere 4.x(二)Sharding-Proxy使用
      • 分表
        • server.yaml 基本信息配置
        • config-sharding.yaml 分表配置
        • 连接
      • 分库分表
      • 读写分离
目录

shardingsphere 4.x(二)Sharding-Proxy使用

定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供 MySQL/PostgreSQL 版本,它可以使用任何兼容 MySQL/PostgreSQL 协议的访问客户端 (如:MySQL Command Client, MySQL Workbench, Navicat 等) 操作数据,对 DBA 更加友好。下载地址 (opens new window)

  • 向应用程序完全透明,可直接当做 MySQL/PostgreSQL 使用。
  • 适用于任何兼容 MySQL/PostgreSQL 协议的的客户端。

下载的文件 lib 下有几个文件不是.jar 结尾,需要改成.jar 结尾,否则会报找不到类文件
下载的 Sharding-Proxy 默认是没有 mysql 驱动的,需要自己找到版本往 lib 目录下丢

# 分表

# server.yaml 基本信息配置

authentication:
  users:
    # 账号 root
    root:
      # 密码 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# config-sharding.yaml 分表配置

# 逻辑库
schemaName: sharding_db

#数据源
dataSources:
  ds_0:
    url: jdbc:mysql://192.168.81.104:3306/test_jdbc?serverTimezone=GMT%2B8
    username: dev_fqr
    password: Dev@fqr2021
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50

#分片规则
shardingRule:
  tables:
    # 逻辑表名
    test_standard:
      actualDataNodes: ds_0.test_standard_${1..2}
      tableStrategy:
        # 规则
        inline:
          # 注定分片的列
          shardingColumn: id
          # 注定分片的算法
          algorithmExpression: test_standard_${id % 2 + 1}
      # 主键生成策略
      keyGenerator:
        type: SNOWFLAKE
        column: id
  bindingTables:
    - test_standard
  defaultTableStrategy:
    none:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# 连接

启动后默认端口 3307,然后可以使用 mysql 连接工具进行连接

对逻辑库创建表等操作,都会映射到物理库中。

# 分库分表

修改 config-sharding.yaml

# 逻辑库
schemaName: sharding_db

#数据源
dataSources:
  ds_0:
    url: jdbc:mysql://192.168.81.104:3306/test_jdbc1?serverTimezone=GMT%2B8
    username: dev_fqr
    password: Dev@fqr2021
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    
  ds_1:
    url: jdbc:mysql://192.168.81.104:3306/test_jdbc2?serverTimezone=GMT%2B8
    username: dev_fqr
    password: Dev@fqr2021
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50

#分片规则
shardingRule:
  tables:
    # 逻辑表名
    test_standard:
      actualDataNodes: ds_${0..1}.test_standard_${1..2}
      tableStrategy:
        # 规则
        inline:
          # 注定分片的列
          shardingColumn: id
          # 注定分片的算法
          algorithmExpression: test_standard_${id % 2 + 1}
      # 主键生成策略
      keyGenerator:
        type: SNOWFLAKE
        column: id
  bindingTables:
    - test_standard
  defaultDatabaseStrategy:
    # 分库策略
    inline:
      shardingColumn: user_id
      algorithmExpression: ds_${user_id % 2}
  defaultTableStrategy:
    none:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

创建表只能通过在配置中,把表提前预定义好,才可以创建。否则报错 Cannot find table rule and default data source with logic table

# 读写分离

修改 config-master_slave.yaml

schemaName: master_slave_db

dataSources:
  master_ds:
    url: jdbc:mysql://192.168.81.104:3306/user_db?serverTimezone=UTC&useSSL=false
    username: dev_fqr
    password: Dev@fqr2021
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  slave_ds_0:
    url: jdbc:mysql://192.168.81.104:3306/user_db?serverTimezone=UTC&useSSL=false
    username: dev_fqr
    password: Dev@fqr2021
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50

masterSlaveRule:
  name: ms_ds
  masterDataSourceName: master_ds
  slaveDataSourceNames:
    - slave_ds_0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上次更新: 6/11/2025, 4:10:30 PM
shardingsphere 4.x(一)Sharding-JDBC使用

← shardingsphere 4.x(一)Sharding-JDBC使用

Theme by Vdoing | Copyright © 2023-2025
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式