ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

一、简要说明

以下配置实现了:

1、分库分表

2、每一个分库的读写分离

3、读库负载均衡算法

4、雪花算法,生成唯一id

5、字段取模

6、解决笛卡尔积问题

7、设置默认所有表不进行分表

二、配置项

#
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.

The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at
#
    http://www.apache.org/licenses/LICENSE-2.0
#
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and
limitations under the License.

#

######################################################################################################
#
Here you can configure the rules for the proxy.

This example is configuration of sharding rule.

#
######################################################################################################
#
#schemaName: sharding_db
#
#dataSources:
 ds_0:
   url: jdbc:postgresql://127.0.0.1:5432/demo_ds_0
   username: postgres
   password: postgres
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 50
   minPoolSize: 1
 ds_1:
   url: jdbc:postgresql://127.0.0.1:5432/demo_ds_1
   username: postgres
   password: postgres
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 50
   minPoolSize: 1
#
#rules:
#- !SHARDING
 tables:
   t_order:
     actualDataNodes: ds_${0..1}.t_order_${0..1}
     tableStrategy:
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_inline
     keyGenerateStrategy:
         column: order_id
         keyGeneratorName: snowflake
   t_order_item:
     actualDataNodes: ds_${0..1}.t_order_item_${0..1}
     tableStrategy:
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_item_inline
     keyGenerateStrategy:
       column: order_item_id
       keyGeneratorName: snowflake
 bindingTables:
   - t_order,t_order_item
 defaultDatabaseStrategy:
   standard:
     shardingColumn: user_id
     shardingAlgorithmName: database_inline
 defaultTableStrategy:
   none:
#
 shardingAlgorithms:
   database_inline:
     type: INLINE
     props:
       algorithm-expression: ds_${user_id % 2}
   t_order_inline:
     type: INLINE
     props:
       algorithm-expression: t_order_${order_id % 2}
   t_order_item_inline:
     type: INLINE
     props:
       algorithm-expression: t_order_item_${order_id % 2}
#
 keyGenerators:
   snowflake:
     type: SNOWFLAKE
     props:
       worker-id: 123

######################################################################################################
#
If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.

#
######################################################################################################

连接mysql所使用的数据库名
 schemaName: MyDb

 dataSources:
  dsdatasources_0:
    url: jdbc:mysql://127.0.0.1:3306/MyDb_0?serverTimezone=UTC&useSSL=false
    username: root # 数据库用户名
    password: mysql123  # 登录密码
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  dsdatasources_0_read0:
    url: jdbc:mysql://192.168.140.132:3306/MyDb_0?serverTimezone=UTC&useSSL=false
    username: root # 数据库用户名
    password: Xiaohemiao_123  # 登录密码
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  dsdatasources_1:
    url: jdbc:mysql://127.0.0.1:3306/MyDb_1?serverTimezone=UTC&useSSL=false
    username: root # 数据库用户名
    password: mysql123  # 登录密码
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  dsdatasources_1_read1:
    url: jdbc:mysql://192.168.140.132:3306/MyDb_1?serverTimezone=UTC&useSSL=false
    username: root # 数据库用户名
    password: Xiaohemiao_123  # 登录密码
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
 ds_1:
   url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
   username: root
   password:
   connectionTimeoutMilliseconds: 30000
   idleTimeoutMilliseconds: 60000
   maxLifetimeMilliseconds: 1800000
   maxPoolSize: 50
   minPoolSize: 1
#
规则
 rules:
 - !READWRITE_SPLITTING
   dataSources:
     pr_ds1:
       writeDataSourceName: dsdatasources_0 #主库
       readDataSourceNames:
         - dsdatasources_0_read0 # 从库,如果有多个从库,就在下面写多个
       loadBalancerName: loadBalancer_ROUND_ROBIN
     pr_ds2:
       writeDataSourceName: dsdatasources_1 #主库
       readDataSourceNames:
         - dsdatasources_1_read1 # 从库,如果有多个从库,就在下面写多个
       loadBalancerName: loadBalancer_ROUND_ROBIN
   loadBalancers: # 负载均衡算法配置
     loadBalancer_ROUND_ROBIN: # 负载均衡算法名称,自定义
       type: ROUND_ROBIN   # 负载均衡算法,默认为轮询算法,还有加权算法和随机算法,可参考官网
 - !SHARDING
   tables:
     t_product: #需要进行分表的表名
       actualDataNodes: dsdatasources_${0..1}.t_product_${0..1} # 表达式,将表分为t_product_0 , t_product_1
       tableStrategy: #分表策略
        standard:
           shardingColumn: product_id # 字段名
           shardingAlgorithmName: t_product_MOD
       databaseStrategy: # 分库策略
           standard:
             shardingColumn: product_id
             shardingAlgorithmName: t_product_MOD
       keyGenerateStrategy:
         column: id
         keyGeneratorName: snowflake #雪花算法
   t_order_item:
     actualDataNodes: ds_${0..1}.t_order_item_${0..1}
     tableStrategy:
       standard:
         shardingColumn: order_id
         shardingAlgorithmName: t_order_item_inline
     keyGenerateStrategy:
       column: order_item_id
       keyGeneratorName: snowflake
 bindingTables: # 解决笛卡尔积问题(主从集群,当存在有关联关系表时候,联合查询数据会有重复的问题)
   - t_order,t_order_item
 defaultDatabaseStrategy:
   standard:
     shardingColumn: user_id
     shardingAlgorithmName: database_inline
   defaultTableStrategy: # 设置所有的表默认不进行分表,如果要进行分表,则进行如上单独的配置即可
     none:
#
   shardingAlgorithms:
     t_product_MOD: # 取模名称,可自定义
       type: MOD # 取模算法
       props:
         sharding-count: 2 #分片数量,因为分了两个表,所以这里是2
   t_order_inline:
     type: INLINE
     props:
       algorithm-expression: t_order_${order_id % 2}
   t_order_item_inline:
     type: INLINE
     props:
       algorithm-expression: t_order_item_${order_id % 2}
#
   keyGenerators:
     snowflake: # 雪花算法名称,自定义名称
       type: SNOWFLAKE
       props:
         worker-id: 123

三、数据准备

-- 创建表
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_product
-- ----------------------------
DROP TABLE IF EXISTS t_product;
CREATE TABLE t_product  (
  id varchar(225) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  product_id int(11) NOT NULL,
  product_name varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  PRIMARY KEY (id, product_id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

-- 插入表数据
INSERT INTO t_product(product_id,product_name) VALUES(1,'one');
INSERT INTO t_product(product_id,product_name) VALUES(2,'two');
INSERT INTO t_product(product_id,product_name) VALUES(3,'three');
INSERT INTO t_product(product_id,product_name) VALUES(4,'four');
INSERT INTO t_product(product_id,product_name) VALUES(5,'five');
INSERT INTO t_product(product_id,product_name) VALUES(6,'six');
INSERT INTO t_product(product_id,product_name) VALUES(7,'seven');

四、查看数据

ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

1、查看shardingsphere中间件t_product表数据

ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

2、主库192.168.140.131数据

ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

2、从库192.168.140.132数据

ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

Original: https://www.cnblogs.com/sportsky/p/16429011.html
Author: SportSky
Title: ShardingSphere-proxy-5.0.0企业级分库分表、读写分离、负载均衡、雪花算法、取模算法整合(八)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/609982/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 小记:音频格式转化ByPython(下)

    上文中我们已经大致明白了pydub库的使用方法,今天的目标是写个爬虫爬取歌曲信息。 关于网络爬虫,Python的标准库里是有相应的包的,可以直接打开:https://docs.py…

    Linux 2023年6月8日
    097
  • 离职,问题就解决了吗?

    刚入职场的那几年,我经常对工作有各种抱怨。回想起来,有两次冲动的不必要的离职,或者说应该干得更久一点。一旦有了离职的想法,整个人每天都纠结是去还是留,这种纠结成倍的放大焦虑,让自己…

    Linux 2023年6月6日
    099
  • Python3.9.5安装

    基础环境:yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-de…

    Linux 2023年6月6日
    085
  • js笔记之while循环

    while循环就是for循环去掉前后两个条件 var i = 0; for( ; i < 10; ){ document.write(i); document.write(&…

    Linux 2023年6月13日
    087
  • Redis 用的很溜,了解过它用的什么协议吗?

    我是风筝,公众号「古时的风筝」,一个兼具深度与广度的程序员鼓励师,一个本打算写诗却写起了代码的田园码农!文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白…

    Linux 2023年5月28日
    097
  • VirtualBox网络模式 宿主机无法访问虚拟机问题记录

    问题背景 最近在公司使用VirtualBox虚拟机,使用虚拟的ubuntu系统,家里的路由能使用桥接模式,然后能双向ping通(宿主机 现在经常要打开虚拟Linux跑一些脚本和编译…

    Linux 2023年6月6日
    096
  • 对比nushell和powershell

    2021-07-17 第一版 有些对比领域缺失,这篇文章会持续更新。 这是一篇对比powershell和nushell的文章。我是powerhsell专家,又是nushell新手,…

    Linux 2023年6月14日
    096
  • Centos8.x yum 源配置 解决 yum 不可用

    [root@iZ2ze1e3u7m7oe426pyndaa ~]# cd /etc/yum.repos.d/ [root@iZ2ze1e3u7m7oe426pyndaa yum.r…

    Linux 2023年6月7日
    0116
  • 1.1 操作系统是什么,操作系统概述

    好的开始是成功的一半。学习 Linux 的第一个问题是搞明白 Linux 是什么,了解其来龙去脉、前世今生,知道其发展趋势、应用前景,弄清楚为什么学习它,以及如何掌握它和使用它,知…

    Linux 2023年5月27日
    066
  • 007 Linux 命令三剑客之-awk

    Linux 命令三剑客,sed、grep、awk。 sed:擅长数据修改。 grep:擅长数据查找定位。 awk:擅长数据切片,数据格式化,功能最复杂。 awk 更适合格式化文本,…

    Linux 2023年5月27日
    081
  • 单片机 MCU 固件打包脚本软件

    ​ 1 前言 开发完 MCU 软件后,通常都会生成 hex 文件或者 bin 文件,用来做固件烧录或者升级,如果用来做产品开发,就涉及到固件版本的问题,初学者通常采用固件文件重命名…

    Linux 2023年6月7日
    091
  • 【5】2022年7月

    7月3日 总结3号的一天就是只有一句话,”自己经历了什么只有自己最清楚,不要辜负自己经历的”。 7月3号凌晨2点,收拾好行李和整理房间,在网上购买日常用品到…

    Linux 2023年6月13日
    076
  • TCP/IP方法安装打印机

    在计算机安装打印机驱动和添加打印机 (TCP/IP 方法 ) 一:打印机的型号和驱动。 1.1 :如何知道自己的打印机是什么品牌和型号?: 通过观察打印机表面的logo 或者其他文…

    Linux 2023年6月13日
    0112
  • Redis监控技巧(转)

    来自:http://blog.nosqlfan.com/html/4166.html Redis 监控最直接的方法当然就是使用系统提供的 info 命令来做了,你只需要执行下面一条…

    Linux 2023年5月28日
    088
  • kubeadm搭建单master k8s集群

    kubeadm搭建单master k8s集群 一、 准备环境 软件环境: 软件 版本 操作系统 CentOS7.9_x64 内核 kernel-ml-5.17.3-1.el7 Do…

    Linux 2023年6月13日
    0108
  • 【原创】Linux虚拟化KVM-Qemu分析(六)之中断虚拟化

    背景 Read the fucking source code! –By 鲁迅 A picture is worth a thousand words. –…

    Linux 2023年6月8日
    0104
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球