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)

大家都在看

  • Linux 程序后台运行 ☞ nohup

    nohup(no hang up),可以使程序在系统后台运行,即使退出终端也不受影响。 安装教程: CSDN: Linux 安装nohup 常见问题 执行jar包时: ignori…

    Linux 2023年6月14日
    0114
  • 异常—异常安全问题–内存泄漏动图演示

    异常是一种处理错误的方式,当一个函数发现自己无法处理的错误时就可以抛出异常,让函数的 直接或间接的调用者处理这个错误。 throw: 当问题出现时,程序会抛出一个异常。这是通过使用…

    Linux 2023年6月13日
    091
  • 总结:弹性伸缩的五个条件与六个教训

    前言弹性伸缩是云计算时代给我们带来的一项核心技术红利,但是 IT 的世界中,没有一个系统功能可以不假思索的应用到所有的场景中。这篇文章,我们将应用企业级分布式应用服务-EDAS 的…

    Linux 2023年6月8日
    0102
  • 子网掩码、前缀长度、IP地址数的换算

    子网掩码、前缀长度、IP地址数的换算 子网掩码 子网掩码只有一个功能,就是将IP地址划分为网络地址和主机地址两部分。 如同现实生活中的通讯地址,可以看作省市部分和具体门牌号部分。相…

    Linux 2023年6月6日
    0250
  • 嵌入式软件架构设计-程序分层

    1 前言 在嵌入式MCU软件开发过程中,程序分层设计也是重中之重,关系到整个软件开发过程中的协同开发,降低系统软件的复杂度(复杂问题分解)和依赖关系、同时有利于标准化,便于管理各层…

    Linux 2023年6月7日
    0147
  • JDK8以上提高开发效率

    1 接口的默认方法和静态方法 1.1 接口中可以定义默认方法和静态方法。 默认方法使用default修饰,静态方法和默认方法可以多个; 静态方法通过接口直接调用,默认方法通过接口实…

    Linux 2023年6月13日
    0101
  • MySQL实现备份(3)之xtrabackup 备份工具

    xtrabackup工具: 是percona公司开发的一个用于对MySQL进行备份的工具。相对于mysqldump,xtrabackup支持增量备份、差异备份等。 使用手册;htt…

    Linux 2023年6月7日
    0132
  • docker的基本使用

    一、 实验前置知识 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化。容器是…

    Linux 2023年6月13日
    083
  • Git的常见命令

    Git 一、git环境安装 1.初始化本地仓库: git init 2.将本地仓库跟远程仓库建立连接:git remote add name path ​ git clone pa…

    Linux 2023年6月7日
    082
  • Bash编程中对字符串的操作

    Bash的字符串操作 String="Hello World" #获取字符串长度,获取字符长度的变量调用应该使用${},这里大括号是必须的 #例1-1 echo…

    Linux 2023年6月13日
    0114
  • 【redis使用全解析】常见运维操作

    $ redis-server redis.conf 常见选项: ./redis-server (run the server with default conf) ./redis-…

    Linux 2023年5月28日
    076
  • python截取字符串(字符串切片)

    python中使用 []来截取字符串,语法: 字符串[起始&#…

    Linux 2023年6月6日
    0131
  • 为spring cloud config实现刷新动态掉的坑

    正常搭建配置中心,网上教程多,这里不讨论,只记坑也是为了后来者少花时间在这里,由于是当时研究了好久才写的文章,所以只能提供问题的原因,当然会给出印证的思路,闲话不多说进入正题! 版…

    Linux 2023年6月7日
    0100
  • 机器学习1

    常见的几种假设检验的实例以及对应python代码实现(包括基于图的效果展示 Z检验 t检验 χ2检验 F检验 熟悉scikit-learn及其相关应用 Numpy Numpy 优势…

    Linux 2023年6月6日
    099
  • Spring 对Controller异常的统一处理

    对于Controller的异常处理,分为两种,一种是对已知的异常处理,一种是未知的异常处理 1、定义自定义异常类 /** * @author hzc * */ public cla…

    Linux 2023年6月7日
    0114
  • docker安装redis

    首先考虑需要安装的redis版本,我这里是安装的redis 6.0.16,如果宿主机没有,那么就docker pull redis:6.0.16 一、指定redis配置文件 我的宿…

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