ShardingSphere-Proxy 主从读写 入门使用


基于ShardingSphere5.0.0Alpha

环境准备

MySQL搭建

    准备MySQL

  • 准备三个mysql,一主二从,搭建教程链接:Mysql docker 主从配置

    • 3306:主库
    • 3309、3310:从库

配置ShardingSphere-Proxy

    这里使用5.0.0版本的

  • 1.下载ShardingSphere-Proxy

  • 2.下载完成后解压放到自己的目录下,比如我这里是放到:D:/temp/ssp

  • 3.下载MySQL-connect.jar

  • 4.将MySQL-connect.jar,放到ShardingSphere-Proxy根目录的lib下,比如我这里是:D:/temp/ssp/lib/

  • 5.写入配置文件,里面本身就有配置文件,这里需要写入的有两份,如果之前存在的就直接替换:server.xml和config-master_slave.yaml

    server.xml 这份感觉不用改,如果和下面的一样就不用改

######################################################################################################
#
# If you want to configure orchestration, authorization and proxy properties, please refer to this file.
#
####################################################################################################### orchestration:
#  name: orchestration_ds
#  overwrite: true
#  registry:
#    type: zookeeper
#    serverLists: localhost:2181
#    namespace: orchestrationauthentication:users:root:password: rootsharding:password: shardingauthorizedSchemas: testprops:max.connections.size.per.query: 1acceptor.size: 16  # The default value is available processors count * 2.executor.size: 16  # Infinite by default.proxy.frontend.flush.threshold: 128  # The default value is 128.# LOCAL: Proxy will run with LOCAL transaction.# XA: Proxy will run with XA transaction.# BASE: Proxy will run with B.A.S.E transaction.proxy.transaction.type: LOCALproxy.opentracing.enabled: falsequery.with.cipher.column: truesql.show: false

    config-master_slave.yaml,这个文件的配置关键字一定不要安装官方example的那个来,完全不行,要按照官方文档来,比如JDBCURL需要改成url,大致的文件内容如下:

######################################################################################################
#
# Here you can configure the rules for the proxy.
# This example is configuration of master-slave rule.
#
# If you want to use master-slave, please refer to this file;
# if you want to use sharding, please refer to the config-sharding.yaml.
#
######################################################################################################schemaName: testdataSources:master_ds:url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: rootconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50slave_ds_0:url: jdbc:mysql://localhost:3309/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: rootconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50slave_ds_1:url: jdbc:mysql://localhost:3310/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: rootconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50rules:
- !REPLICA_QUERYdataSources:master_ds:primaryDataSourceName: master_dsreplicaDataSourceNames:- slave_ds_0- slave_ds_1

运行

    经过前面的步骤基本配置差不多了,现在只需要运行Sharding-Proxy根目录下的 bin/start.bat就行了,运行命令大致如下:

# 进入相应的目录下,指定在13306端口运行,不指定默认运行在3307端口
.\start.bat 13306

    这里需要注意的是,Java11运行好像有点问题,需要用1.8来运行,而不确定Java运行版本的,可以直接写个脚本:start.bat(start.sh类似),整个脚本修改大致如下:

@rem
@rem Licensed to the Apache Software Foundation (ASF) under one or more
@rem contributor license agreements.  See the NOTICE file distributed with
@rem this work for additional information regarding copyright ownership.
@rem The ASF licenses this file to You under the Apache License, Version 2.0
@rem (the "License"); you may not use this file except in compliance with
@rem the License.  You may obtain a copy of the License at
@rem
@rem     http://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem@echo off & setlocal enabledelayedexpansioncd %~dp0set SERVER_NAME=ShardingSphere-Proxyset CLASS_PATH="..;..\lib\*;..\ext-lib\*"set PORT=%1set CONFIG=%2if "%PORT%"=="-h" (goto print_usage
)
if "%PORT%"=="--help" (goto print_usage
)if "%PORT%"=="" (
set MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap
) else ( if "%CONFIG%"=="" (set MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap %PORT%echo The port is configured as %PORT%set CLASS_PATH=../conf;%CLASS_PATH%) else (set MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap %PORT% %CONFIG%echo The port is configured as %PORT%echo The configuration path is %CONFIG%set CLASS_PATH=../%CONFIG%;%CLASS_PATH%)echo The classpath is %CLASS_PATH%
)echo Starting the %SERVER_NAME% ...# 在这里就直接设置死了Java的运行版本,如果不能正常运行,请去掉这行
F:\Software\Java\jdk1.8.0_261\bin\java.exe -server -Xmx2g -Xms2g -Xmn1g -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 -Dfile.encoding=UTF-8 -classpath %CLASS_PATH% %MAIN_CLASS%goto exit:print_usageecho "usage: start.bat [port] [config_dir]"echo "  port: proxy listen port, default is 3307"echo "  config_dir: proxy config directory, default is conf"pause:exitpause

    运行正常的话,就可以看到刷刷刷的一排正常日志打出,这样就OK了

ShardingSphere Proxy连接测试

    使用docker mysql命令有问题,成功连接了但是有bug,但使用MySQL workbench连接和程序连接是没有问题的

MySQL Workbench链接

    使用图形化界面MySQL Workbench连接:本地ip、13306端口、root、root,连接成功,查询和删除,相应数据变化正确

Java JDBC连接测试

    下面是整个测试代码,可直接运行,运行下来也没有问题:

package com.example.demo.shardingsphere.proxy.jdbc;import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.annotation.Transactional;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;/*** ShardingSphere Proxy Raw JDBC测试*/
public class ShardingSphereProxyRawJdbcTest {@Test @Transactionalpublic void test() throws SQLException {System.out.println("Start test");DataSource SSDataSource = createShardingSphereProxyDataSource();DataSource slaveDataSource = createSlaveDataSource();// 测试通过SSP是否数据修改和查询正常Connection ssCon = SSDataSource.getConnection();Statement ssState = ssCon.createStatement();ssState.execute("UPDATE `users` SET `money` = '200' WHERE (`id` = '1');");ResultSet ret = ssState.executeQuery("select * from test.users where id=1;");while (ret.next()) {System.out.println(ret.getLong("money"));assert ret.getLong("money") == 200;}// 测试从库是否数据同步更改Connection slaveCon = slaveDataSource.getConnection();Statement slaveState = slaveCon.createStatement();ResultSet ret2 = slaveState.executeQuery("select * from test.users where id=1;");while (ret2.next()) {System.out.println(ret2.getLong("money"));assert ret2.getLong("money") == 200;}ret2.close();ret.close();ssState.close();slaveState.close();ssCon.close();slaveCon.close();}private DataSource createSlaveDataSource() {HikariDataSource dataSource = new HikariDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3309/test?serverTimezone=UTC&useUnicode=true&characterEncoding" +"=utf-8&useSSL=false&allowPublicKeyRetrieval=true");dataSource.setUsername("root");dataSource.setPassword("root");return dataSource;}private DataSource createShardingSphereProxyDataSource() {HikariDataSource dataSource = new HikariDataSource();dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:13306/test?serverTimezone=UTC&useUnicode=true&characterEncoding" +"=utf-8&useSSL=false&allowPublicKeyRetrieval=true");dataSource.setUsername("root");dataSource.setPassword("root");return dataSource;}
}

参考链接

  • Sharding-Proxy的基本功能使用
  • The sharding-proxy read-write separation study notes of shardingsphere

ShardingSphere-Proxy 主从读写 入门使用相关推荐

  1. MySQL主从(MySQL proxy Lua读写分离设置,一主多从同步配置,分库分表方案)

    Mysql Proxy Lua读写分离设置 一.读写分离说明 读写分离(Read/Write Splitting),基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELE ...

  2. shardingsphere之sharding-proxy读写分离学习笔记

    shardingsphere之sharding-proxy读写分离学习笔记 引言 重要提示 演示环境 版本信息 代码示例和参考 读写分离简介 主从复制 主从复制的原理和流程 sharding-prox ...

  3. Amoeba实现mysql主从读写分离

    Amoeba实现mysql主从读写分离 这段在网上看了下关于amoeba的文章,总体感觉好像要比mysql-proxy好的多,也参考了不少的资料,此文章可能与其他文章作者会有雷同的地方,请谅解,但是此 ...

  4. mysql-proxy 0.8.5_主从读写分离----mysql-proxy0.8.5安装与配置

    废话不多说,直接开干: 1.安装环境: yum -y install libevent glib2 lua gcc gcc-c++ autoconf mysql-devel libtool pkgco ...

  5. 视频教程-ShardingSphere:SpringBoot2+MybatisPlus读写分离+分表-Java

    ShardingSphere:SpringBoot2+MybatisPlus读写分离+分表 10多年互联网一线实战经验,现就职于大型知名互联网企业,架构师, 有丰富实战经验和企业面试经验:曾就职于某上 ...

  6. 视频教程-ShardingSphere:SpringBoot2+MybatisPlus+读写分离+分库分表-Java

    ShardingSphere:SpringBoot2+MybatisPlus+读写分离+分库分表 10多年互联网一线实战经验,现就职于大型知名互联网企业,架构师, 有丰富实战经验和企业面试经验:曾就职 ...

  7. mysql查询并设置高亮_Thinkphp3.2.3设置MySql主从读写分离后,简单调用主数据库查询

    图/文:迷神 Thinkphp是一款不错的国产框架,使用范围广,应用也比较多.随着网站访问增大往往需要使用mysql主从同步功能,本身Thinkphp自带了主从读写分离的功能了. 但是我们经常有一个场 ...

  8. 基于 EntityFramework 的数据库主从读写分离架构(1) - 原理概述和基本功能实现...

    回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目录: src\ NDF.Data.EntityFramework\Maste ...

  9. mysql主从读写Windows_Windows操作系统下的MySQL主从复制及读写分离

    一.主服务器(master)配置 1.修改MySQL配置文件my.ini [mysqld] log-bin=mysql-bin log-bin-index=mysql-bin.index server ...

最新文章

  1. 深度学习--说一说卷积和池化
  2. 算法 - 交换排序(C++)
  3. python中的sys的概念和作用_Python(os和sys)理解
  4. C语言实现词典编排算法(附完整源码)
  5. cxf和spring结合出现问题
  6. 2015-10-21 C#1
  7. iView学习笔记(三):表格搜索,过滤及隐藏列操作
  8. mysql normsinv_在MySQL中实现NORMSINV函数
  9. Android EditText中插入图片并响应点击事件
  10. drf 安装_drf 安装与配置
  11. Python+OpenCV:图像Harris角点检测(Harris Corner Detection)
  12. mysql约束_mysql数据库的基本建库,建表,约束,属性 操作--001
  13. 郭德纲 相声下载(续)
  14. STL CSB 11.10
  15. php英文星期中文星期,英文星期到星期天【星期一到星期天的英文用中文怎么说。发音标准的来。】...
  16. 2020亚太数学建模竞赛A题翻译
  17. 贴片天线的特征模分析及其应用
  18. 制作NGUI动态字体
  19. 7-95 深入虎穴 (树的深搜)
  20. vue中el-radio-group点击事件,双击取消

热门文章

  1. Erlang与ActionScript3采用JSON格式进行Socket通讯
  2. “饮水机”:形象比喻 深入浅出理解RAID
  3. xampp的mysql和phpnow的mysql,xampp的apache和visualsvn的apache和phpnow的apache
  4. C#中i=i++值不变的一个解释
  5. 解决python2.7.9以下版本requests访问https的问题
  6. 【vue开发问题-解决方法】(八)利用axios拦截器实现elementUI中加载动画,控制加载区域
  7. 【实践】图推荐算法在EE问题上的应用(附交流视频和PPT下载链接)
  8. original_keras_version = f.attrs[‘keras_version‘].decode(‘utf8‘)AttributeError: ‘str‘ object has no
  9. anaconda显示ImportError: cannot import name ‘secure_write‘
  10. 一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系