ShardingSphere-Proxy 分库分表

安装ShardingSphere-Proxy

中间件封装

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

请添加图片描述
5.1.1安装:

https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-proxy/startup/bin/)

https://archive.apache.org/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-bin

mkdir  ext-lib

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

修改配置文件

cd /usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/conf

vim server.yaml

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

rules:
  - !AUTHORITY
    users:
      - root@%:root
    provider:
      type: ALL_PRIVILEGES_PERMITTED
# 打印sql
props:
  sql-show: true
  
  

请添加图片描述

启动:
/usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/bin/start.sh 3306

也可以指定端口号和配置文件目录:`bin/start.bat ${proxy_port} ${proxy_conf_directory}` ,不指定就是3307端口

停止:

/usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/bin/stop.sh

查看启动状态

 ps -ef|grep shardingsphere|grep -v grep

开启端口:

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

重启防火墙:

firewall-cmd --reload #重启
firewall systemctl stop firewalld.service #停止
firewall systemctl disable firewalld.service #禁止firewall开机启动

远程连接

mysql -h192.168.158.166 -p3306 -uroot -p

连接成功

请添加图片描述
也可以使用可视化工具连接,和正常使用mysql没有区别

请添加图片描述

配置读写分离

/conf 目录下有以下几个配置文件

请添加图片描述
修改配置文件 config-readwrite-splitting.yaml

cd  /usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/conf

vim  config-readwrite-splitting.yaml
schemaName: readwrite_splitting_db

dataSources:
  db1:
    url: jdbc:mysql://192.168.158.134:3306/distributed_server?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: psh120370
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  slave1:
    url: jdbc:mysql://192.168.158.146:3306/distributed_server?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: psh120370
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  db0:
    url: jdbc:mysql://192.168.158.165:3306/distributed_server?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: psh120370
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !READWRITE_SPLITTING
  dataSources:
    readwrite_ds:
      type: Static
      props:
        write-data-source-name: db1
        read-data-source-names: slave1

重新启动:

/usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/bin/start.sh 3306

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

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

spring:
  application:
    name: shardingProxy
  cloud:
    nacos:
      discovery:
        server-addr: http://192.168.158.135:8848
        namespace: d3a69e71-1c55-411c-940a-1f275c8c7bea
      username: nacos
      password: nacos
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.158.166:3306/readwrite_splitting_db?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
      username: root
      password: root

水平分片

表结构和查询逻辑见:

https://blog.csdn.net/persistence_PSH/article/details/131367613

修改配置config-sharding.yaml

cd  /usr/local/apache-shardingsphere-5.1.1-shardingsphere-proxy-bin/conf

vim  config-sharding.yaml

这里的 schemaName 需要和上面的读写分离逻辑库的名称不一致,其实就是把原本在spring boot中的配置写到了ShardingSphere-Proxy

schemaName: sharding_db

dataSources:
  db1:
    url: jdbc:mysql://192.168.158.134:3306/distributed_server?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: psh120370
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  slave1:
    url: jdbc:mysql://192.168.158.146:3306/distributed_server?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: psh120370
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  db0:
    url: jdbc:mysql://192.168.158.165:3306/distributed_server?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: psh120370
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
- !SHARDING
  tables:
    test_order:
      actualDataNodes: db$->{0..1}.test_order$->{0..1}
      databaseStrategy:
        standard:
          shardingColumn: account
          shardingAlgorithmName: inline_account
      tableStrategy:
        standard:
          shardingColumn: order_no
          shardingAlgorithmName: hash_mod
      keyGenerateStrategy:
        column: id
        keyGeneratorName: snowflake
    test_order_item:
      actualDataNodes: db$->{0..1}.test_order_item$->{0..1}
      databaseStrategy:
        standard:
          shardingColumn: account
          shardingAlgorithmName: inline_account
      tableStrategy:
        standard:
          shardingColumn: order_no
          shardingAlgorithmName: hash_mod
      keyGenerateStrategy:
        column: id
        keyGeneratorName: snowflake

  bindingTables:
    - test_order,test_order_item


  shardingAlgorithms:
    inline_account:
      type: HASH_MOD
      props:
        sharding-count: 2
    hash_mod:
      type: HASH_MOD
      props:
        sharding-count: 2
  
  keyGenerators:
    snowflake:
      type: SNOWFLAKE

springboot配置文件

spring:
  application:
    name: shardingProxy
  cloud:
    nacos:
      discovery:
        server-addr: http://192.168.158.135:8848
        namespace: d3a69e71-1c55-411c-940a-1f275c8c7bea
      username: nacos
      password: nacos
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.158.166:3306/sharding_db?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
      username: root
      password: root

查询成功:请添加图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sharding-Proxy 是一个开源的分库分表中间件,它可以帮助应用程序实现无感知的分库分表操作。下面是一个简单的步骤来利用 Sharding-Proxy 进行分库分表: 1. 安装和配置 Sharding-Proxy:首先,你需要下载 Sharding-Proxy 的安装包,并解压到你的服务器上。然后,根据你的需求修改配置文件,配置数据源和分片规则等信息。 2. 创建数据库和表:在进行分库分表之前,你需要创建相应的数据库和表结构。你可以选择手动创建,或者使用 Sharding-Proxy 提供的自动建表功能。 3. 配置分片规则:在 Sharding-Proxy 的配置文件中,你需要定义分片规则,指定如何将数据分散到不同的数据库和表中。可以使用基于范围、哈希、精确等多种分片算法。 4. 连接到 Sharding-Proxy:在应用程序中,需要修改数据库连接信息,将原来连接数据库的地址改为连接 Sharding-Proxy 的地址。这样应用程序就可以通过 Sharding-Proxy 访问分片后的数据。 5. 进行分库分表操作:现在你可以在应用程序中执行正常的数据库操作,而无需关心具体的分库分表细节。Sharding-Proxy 会根据配置的规则自动将数据路由到正确的库和表中。 需要注意的是,使用 Sharding-Proxy 进行分库分表操作需要仔细考虑数据一致性、事务处理、跨库查询等问题。在配置和使用过程中,建议参考官方文档和示例来确保正确性和性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值