ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)

一、修改配置文件config-sharding.yaml,并重启服务

#
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:
  ds_0: # 主库
    url: jdbc:mysql://127.0.0.1:3306/MyDb?serverTimezone=UTC&useSSL=false
    username: root # 数据库用户名
    password: mysql123  # 登录密码
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
    minPoolSize: 1
  ds_0_read0: # 从库
    url: jdbc:mysql://192.168.140.132:3306/MyDb?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_ds:
       writeDataSourceName: ds_0
       readDataSourceNames:
         - ds_0_read0

 - !SHARDING
   tables:
     t_product: #需要进行分表的表名
       actualDataNodes: ds_0.t_product_${0..1} # 表达式,将表分为t_product_0 , t_product_1
       tableStrategy:
        standard:
           shardingColumn: product_id # 字段名
           shardingAlgorithmName: t_product_VOLUME_RANGE
       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_VOLUME_RANGE: # 取模名称,可自定义
       type: VOLUME_RANGE # 取模算法
       props:
         range-lower: '5' # 最小容量为5条数据,仅方便测试
         range-upper: '10' #最大容量为10条数据,仅方便测试
         sharding-volume: '5' #分片的区间的数据的间隔
   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

ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)

上述配置是同时有做容量范围分片

二、数据准备

在中间件中ShardingSphere中创建MyDb数据库,并创建相关表和插入数据

-- 创建表
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建立mysql读写分离的连接(六)

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

ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)

2、主库192.168.140.131数据

ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)

3、从库192.168.140.132数据

ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)

Original: https://www.cnblogs.com/sportsky/p/16414683.html
Author: SportSky
Title: ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)

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

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

(0)

大家都在看

  • 最小生成树-Prim算法

    最小生成树minimal-spanning-tree(概念就不具体介绍了)有两种基于不同贪心选择的算法,一个为Prim算法,一个为Kruskal算法。 Prim和Dijkstra算…

    Linux 2023年6月7日
    099
  • redis研究记录

    redis研究记录 1 概述目前多数的NoSql数据库本质上都是键值对形式,Redis也不例外。作为缓存数据库的一种,和Memcached相比,有以下几种主要的优点:(1)速度上,…

    Linux 2023年5月28日
    081
  • zabbix 报表动作日志 报错”503“

    本文来自博客园,作者:xiao智,转载请注明原文链接:https://www.cnblogs.com/yuwen01/p/16216868.html Original: https…

    Linux 2023年6月13日
    0106
  • windows-cmd-help结果集

    有关某个命令的详细信息,请键入 HELP 命令名ASSOC 显示或修改文件扩展名关联。ATTRIB 显示或更改文件属性。BREAK 设置或清除扩展式 CTRL+C 检查。BCDED…

    Linux 2023年6月7日
    083
  • 条件分支

    条件分支 if-else-fi [root@node1 test]# vim if.sh #!/bin/bash amswer=30 if [ $1 -gt $answer ];t…

    Linux 2023年6月11日
    077
  • laravel源码分析-队列Queue

    队列 (Queue) 是 laravel 中比较常用的一个功能,队列的目的是将耗时的任务延时处理,比如发送邮件,从而大幅度缩短 Web 请求和响应的时间。本文我们就来分析下队列创建…

    Linux 2023年6月7日
    078
  • zabbix快速安装(yum)

    1、先卸载系统自带数据库 [root@bogon ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 –nodeps 2、安装mys…

    Linux 2023年6月6日
    088
  • 订阅消息组件由 redis 改为 rabbitmq

    刚开始测试 dapr 时为了图省事,使用了 pubsub.redis,现在准备上生产环境,改用支持消息持久化的 pubsub.rabbitmq。 之前使用的 pubsub.redi…

    Linux 2023年5月28日
    086
  • 编写 Shell 程序,实现自动删除 50 个账号的功能,账号名为stud1 至 stud50 ?

    #!/bin/bash for((i=1;i<51;i++))< code><code>do</code><code> &am…

    Linux 2023年5月28日
    099
  • redis导致的错误错误

    ==========双预防系统启动成功========== 14:42:39.821 [http-nio-9217-exec-1] INFO o.a.c.c.C.[.[.[/] -…

    Linux 2023年5月28日
    0158
  • 内部类

    内部类:将一个类的定义放在另一个类的定义内部。内部类机制可以把逻辑相关的类组织在一起,并控制位于内部的类的可视性。 内部类与组合是完全不同的概念。 内部类不仅是一种代码隐藏机制(将…

    Linux 2023年6月8日
    0104
  • Python3中datetime不同时区转换介绍与踩坑

    最近的项目需要根据用户所属时区制定一些特定策略,学习、应用了若干python3的时区转换相关知识,这里整理一部分记录下来。 下面涉及的几个概念及知识点: GMT时间:Greenwi…

    Linux 2023年6月6日
    098
  • 使用MyBatis Generator代码生成器的简单模式

    在动态web项目的lib目录下放入mybatis-3.2.2jar、mysql-connector-java-5.1.25-bin.jar、log4j-1.2.17.jar还有生成…

    Linux 2023年6月8日
    0117
  • 面试题目汇总

    目录: 1、数字数组数字数组2、字符串字符串3、链表 链表4、二叉树二叉树 5、堆栈 堆栈 posted @2019-12-11 20:35 风御之举 阅读(63 ) 评论() 编…

    Linux 2023年6月13日
    091
  • Filter、Interceptor、Aspect 区别及实现

    Fliter 过滤器 请求在到达Controller之前进行与返回去之后 调用 入参为 reuqest,response,chian,过滤器获取不了具体调用哪一个类,哪一个方法。 …

    Linux 2023年6月7日
    0112
  • 爱快在PVE下不定时反复重启死机的解决方法

    太长不看版本: 爱快3.6.X在PVE乃至于ESXI下都存在一定的兼容问题 ! 详细情况: 如题,使用爱快版本为3.6.3 x64 Build20220407113364,底层虚拟…

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