ShardingSphere-Proxy绑定表与广播表详解与实战

在这里插入图片描述

ShardingSphere

算法刷题专栏 | 面试必备算法 | 面试高频算法
越难的东西,越要努力坚持,因为它具有很高的价值,算法就是这样✨
作者简介:硕风和炜,CSDN-Java领域优质创作者,保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文|经验分享|好用的网站工具分享
恭喜你发现一枚宝藏博主,赶快收入囊中吧
人生如棋,我愿为卒,行动虽慢,可谁曾见我后退一步?

ShardingSphere

在这里插入图片描述
在这里插入图片描述

目录

    • 一.ShardingSphere-Proxy核心概念
    • 二.ShardingSphere-Proxy水平分片详解与实战
      • 2.1 实战环境准备
      • 2.2 shardingproxy服务器上修改配置文件config-sharding.yaml
      • 2.3 重启服务器 & 验证是否运行成功
      • 2.4 命令行远程连接简单测试
    • 三.ShardingSphere-Proxy分片实战测试
      • 3.1 命令行测试 - 查找
    • 四.总结
    • 五.共勉

一.ShardingSphere-Proxy核心概念

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

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

在这里插入图片描述

二.ShardingSphere-Proxy水平分片详解与实战

2.1 实战环境准备

注意:这篇文章的实战讲解是建立在之前的文章实操基础上的,如果你之前的环境还没有搭建好,可以先去搭建好环境,然后再来学习本篇文章的实战就会非常快,事半功倍!

  1. 192.168.10.134服务器(shardingproxy)上部署的ShardingSphere-Proxy代理192.168.10.132服务器和192.168.10.133服务器;
  2. 之前在192.168.10.132服务器(node1-shardingsphere)上创建的ljw_course_db1数据库,以及数据库下创建的t_course_1表和t_course_2表;
  3. 之前在192.168.10.133服务器(node2-shardingsphere)上创建的ljw_course_db2数据库,以及数据库下创建的t_course_1表和t_course_2表;
  4. 之前在node1和node2服务器节点上数据库下创建的user表是广播表。

2.2 shardingproxy服务器上修改配置文件config-sharding.yaml

具体的配置信息可以参考之前的文章进行配置!
广播表直接在配置文件中配置,绑定表交给对应的逻辑处理!

schemaName: sharding_db

dataSources:
  ljw_course_db1:
    url: jdbc:mysql://192.168.10.132:3306/ljw_course_db1?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ljw_course_db2:
    url: jdbc:mysql://192.168.10.133:3306/ljw_course_db2?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1

rules:
  - !SHARDING
    tables:
      t_course:
        actualDataNodes: ljw_course_db${1..2}.t_course_${1..2}
        databaseStrategy:
          standard:
            shardingColumn: user_id
            shardingAlgorithmName: table-inline
        tableStrategy:
          standard:
            shardingColumn: cid
            shardingAlgorithmName: inline-hash-mod
        keyGenerateStrategy:
          column: cid
          keyGeneratorName: snowflake
      t_course_section:
        actualDataNodes: ljw_course_db${1..2}.t_course_section_${1..2}
        databaseStrategy:
          standard:
            shardingColumn: user_id
            shardingAlgorithmName: table-mod
        tableStrategy:
          standard:
            shardingColumn: corder_no
            shardingAlgorithmName: table-hash-mod
        keyGenerateStrategy:
          column: id
          keyGeneratorName: snowflake
    broadcastTables:
      - user

    shardingAlgorithms:
      table-inline:
        type: INLINE
        props:
          algorithm-expression: t_course_${user_id % 2 + 1}
      inline-hash-mod:
        type: INLINE
        props:
          algorithm-expression: t_course_${Math.abs(cid.hashCode()) % 2 + 1}
      table-mod:
        type: INLINE
        props:
          algorithm-expression: t_course_${user_id % 2 + 1}
      table-hash-mod:
        type: INLINE
        props:
          algorithm-expression: t_course_section_${Math.abs(corder_no.hashCode()) % 2 + 1}
    keyGenerators:
      snowflake:
        type: SNOWFLAKE

ShardingSphere-Proxy绑定表与广播表详解与实战_第1张图片

2.3 重启服务器 & 验证是否运行成功

docker restart shardingproxy
docker logs shardingproxy

ShardingSphere-Proxy绑定表与广播表详解与实战_第2张图片

2.4 命令行远程连接简单测试

mysql -h192.168.10.134 -P13308 -uroot -p

逻辑库建立

ShardingSphere-Proxy绑定表与广播表详解与实战_第3张图片

三.ShardingSphere-Proxy分片实战测试

3.1 命令行测试 - 查找

mysql> use sharding_db;
Database changed
mysql> show tables;
+-----------------------+------------+
| Tables_in_sharding_db | Table_type |
+-----------------------+------------+
| t_course_section      | BASE TABLE |
| user                  | BASE TABLE |
| t_course              | BASE TABLE |
+-----------------------+------------+
3 rows in set (0.01 sec)

mysql> select * from user;
+---------------------+----------+------+----------------+
| id                  | name     | age  | email          |
+---------------------+----------+------+----------------+
| 1682581847896547330 | 硕风和炜 |   18 | [email protected] |
+---------------------+----------+------+----------------+
1 row in set (0.06 sec)

mysql> select * from t_course c  inner join t_course_section cs  on c.cid = cs.cid;
Empty set (0.11 sec)

mysql>

查找

ShardingSphere-Proxy绑定表与广播表详解与实战_第4张图片

四.总结

本篇文章主要讲解了ShardingSphere-Proxy绑定表与广播表详解与实战,实操过程非常重要,大家一定要动手亲自实践一下,必须掌握。关于ShardingSphere的内容我们就先讲解到这里,后续为大家带来更多的内容,大家敬请期待呦!!!。

五.共勉

最后,我想和大家分享一句一直激励我的座右铭,希望可以与大家共勉!

在这里插入图片描述

你可能感兴趣的:(ShardingSphere,ShardingSphere,ShardingProxy,mysql,java,绑定表,docker,广播表)