ShardingSphere-Proxy 分库分表 简单示例


简要说明

    对一张简单的订单表数据表进行水平分库分表,拆分2个库,每个库16张表并在新结构在演示常见的增删改查操作

环境配置

设置MySQL

    使用docker设置mysql

# 启动两个mysql
docker run --name mysql11 -p 3311:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql:latest
docker run --name mysql12 -p 3312:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -d mysql:latest# 在11上创建数据库demo_ds_0
docker exec -ti mysql11 mysql -u root -p
create database demo_ds_0;# 在12上创建数据库demo_ds_1
docker exec -ti mysql11 mysql -u root -p
create database demo_ds_1;

ShardingSphere-Proxy 5.0.0 alpha 设置

    docker一直不能设置成功,有点奇怪,这里就直接下载使用了

  • 1.下载ShardingSphere-Proxy,下载完成后放到自己相应的目录下
  • 2.下载MySQL-connect.jar,下载完成后将jar文件放到Sharding根目录的lib目录下

    下面需要配置两个文件:server.yaml、config-sharding.yaml,示例如下(配置都有默认示例说明的,相应进行修改即可)

    server.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.
#######################################################################################################
#
# If you want to configure governance, authorization and proxy properties, please refer to this file.
#
######################################################################################################
#
#governance:
#  name: governance_ds
#  registryCenter:
#    type: ZooKeeper
#    serverLists: localhost:2181
#    props:
#      retryIntervalMilliseconds: 500
#      timeToLiveSeconds: 60
#      maxRetries: 3
#      operationTimeoutMilliseconds: 500
#  overwrite: falseauthentication:users:root:password: rootsharding:password: shardingauthorizedSchemas: sharding_dbprops: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: falseproxy-hint-enabled: falsequery-with-cipher-column: truesql-show: truecheck-table-metadata-enabled: false

    config-sharding.yaml

######################################################################################################
#
# If you want to connect to MySQL, you should manually copy MySQL driver to lib directory.
#
######################################################################################################schemaName: sharding_dbdataSourceCommon:username: rootpassword: rootconnectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50minPoolSize: 1maintenanceIntervalMilliseconds: 30000dataSources:ds_0:url: jdbc:mysql://127.0.0.1:3311/demo_ds_0?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=trueds_1:url: jdbc:mysql://127.0.0.1:3312/demo_ds_1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=truerules:
- !SHARDINGtables:t_order:actualDataNodes: ds_${0..1}.t_order_${0..15}tableStrategy:standard:shardingColumn: order_idshardingAlgorithmName: 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_itemdefaultDatabaseStrategy:standard:shardingColumn: user_idshardingAlgorithmName: database_inline
#  defaultTableStrategy:
#    none:
#  shardingAlgorithms:database_inline:type: INLINEprops:algorithm-expression: ds_${user_id % 2}t_order_inline:type: INLINEprops:algorithm-expression: t_order_${order_id % 16}
#    t_order_item_inline:
#      type: INLINE
#      props:
#        algorithm-expression: t_order_item_${order_id % 2}
#
#  keyGenerators:
#    snowflake:
#      type: SNOWFLAKE
#      props:
#        worker-id: 123

    OK,一切准备就绪,直接进入sharding的根目录下的bin目录中运行:start.bat即可(也可以在命令行中运行)

# 使用命令行运行可以指定运行端口
./start.bat 13306

    成功以后刷刷刷的一排日志打出,没有错误就说明可以运行了

    使用mysql命令或者mysqlworkbench连接上sharding,运行下面的SQL语句生成测试的表,运行成功看到日志中一大批SQL语句,

CREATE TABLE IF NOT EXISTS `t_order` (`order_id` int(11) NOT NULL,`user_id` int(11) NOT NULL,PRIMARY KEY (`order_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

SpringBoot Mybatis配置

    需要修改数据库连接配置,大致如下:

# mybatis的config文件位置配置
mybatis.config-location=classpath:mybatis/mybatis-config.xml
# 各个表的xml文件位置配置
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.neo.model# 数据库连接信息配置,自行更换数据库,用户名和密码,配置为ShardingSphereProxy
spring.datasource.url=jdbc:mysql://localhost:13306/sharding_db?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8\&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

    实体类、Mapper设置这里就不详细赘述了,看GitHub上的工程即可

    测试类进行测试,代码如下:

package week0802.week0802.mappers;import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import week0802.week0802.models.Order;import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@SpringBootTest
@ExtendWith(SpringExtension.class)
@MapperScan("week0802.week0802.mappers")
public class OrderMapperTest {@Autowiredprivate OrderMapper orderMapper;/*** 通过不同的查询条件的传入,可以体会到分库分表是需要设计的* 一个设计不好,查询难搞*/@Test @Transactionalpublic void test() throws SQLException {// 通过sharding插入数据,通过sharding自己的日志输出看出插入不同的数据库和表orderMapper.insertOne(new Order(1L, 1L));orderMapper.insertOne(new Order(2L, 2L));// 只传user_id,看到单库进行了所有表的查询Map<String, Object> condition = new HashMap<>(1);condition.put("user_id", 1L);List<Map<String, Object>> orderQuery = orderMapper.query(condition);assert orderQuery.size() == 1;for (Map item: orderQuery) {System.out.println(item.toString());}// 只传order_id,看到进行了多库单表的查询condition = new HashMap<>(1);condition.put("order_id", 1L);orderQuery = orderMapper.query(condition);assert orderQuery.size() == 1;for (Map item: orderQuery) {System.out.println(item.toString());}// 传入order_id和user_id,看到进行单库单表的查询condition = new HashMap<>(2);condition.put("order_id", 2L);condition.put("user_id", 2L);orderQuery = orderMapper.query(condition);assert orderQuery.size() == 1;for (Map item: orderQuery) {System.out.println(item.toString());}}
}

ShardingSphere-Proxy 分库分表 简单示例相关推荐

  1. Shardingsphere的分库分表+读写分离+分页条件查询

    Shardingsphere的分库分表与读写分离 导入依赖 <dependencies><dependency><groupId>org.springframewo ...

  2. ShardingSphere(八) 分库分表的多种分片策略

    在之前文章<ShardingSphere(二) 水平分表配置搭建,实现分表写入读取>中,我们介绍了数据库的水平分表配置,在文章中只介绍了最简单的行表达式分表配置方式,但往往在实际中我们的业 ...

  3. ShardingSphere JDBC 分库分表 读写分离 数据加密

    简介 在上篇文章中,在本地搭建了运行环境,本地来体验下ShardingSphere JDBC的三个功能:分库分表.读写分离.数据加密 示例运行 首先把概念先捋一捋,参考下面的文档: 数据分片 读写分离 ...

  4. mysql proxy 分库分表_OneProxy实现MySQL分库分表

    简介 Part1:写在最前 随着网站的壮大,MySQL数据库架构一般会经历一个过程: 当我们数据量比较小的时候,一台单实例数据库足矣.等我们数据量增大的时候,我们会采用一主多从的数据库架构来降低我们的 ...

  5. server sql 水平分表_springboot集成Shardingsphere进行分库分表

    当公司业务量上去之后,单表支撑不了的时候,分库分表就是一个绕不开的话题,小弟最近新入职一家公司,发现这边公司在用ShardingSphere来进行分库分表,之前没接触过这方面,所以就写了个demo学习 ...

  6. MySQL分库分表简单介绍

    分库.分表 一.前言 二.分片(类似分库) 三.Scale Out/Scale Up 和 垂直切分/水平拆分 四.分表和分区 五.分表与分库 六.分区与分片区别 一.前言 数据库的数据量达到一定程度之 ...

  7. mysql分表插件_分库分表简单?那我想问如何实现“分库分表插件”?

    随着系统数据量的日益增长,在说起数据库架构和数据库优化的时候,我们难免会常常听到分库分表这样的名词. 当然,分库分表有很多的方法论,比如垂直拆分.水平拆分:也有很多的中间件产品,比如MyCat.Sha ...

  8. ShardingSphere分库分表(SpringBoot+mybatis+mysql)配置

    一.什么是ShardingSphere 定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务. 它使用客户端直连数据库,以 jar 包形式提供服务,无需额外部署和依赖,可理解为增 ...

  9. 【分库分表】ShardingSphere分库分表实战

    一.参考资料 概览 :: ShardingSphere ShardingSphere之分库&分表_JustryDeng-CSDN博客_shardingsphere 分表 利用ShardingS ...

最新文章

  1. 吐血整理:机器学习的30个基本概念,都在这里了(手绘图解)
  2. UWP开发随笔——使用SQLite数据库
  3. c语言map作为参数传递,C++中map和vector作形参时如何给定默认参数?
  4. 《Shell脚本学习指南》第四章 文本处理工具
  5. sap 状态栏添加竖线
  6. Python 使用控制台运行带有相对路径的指令时,是以运行文件为基准,还是以控制台当前路径为基准
  7. HDU - 1540 Tunnel Warfare(线段树+区间合并)
  8. wxpython 可视化开发pdf_MicroPython for the Internet of Things.pdf
  9. 高等数学下-赵立军-北京大学出版社-题解-练习9.3
  10. 深度学习去燥学习编码_请学习编码
  11. 深入理解 ASP.NET 动态控件 (Part 5 - 编译实验)
  12. Google 搜索命令
  13. idea eclipse主题
  14. educoder-Hadoop开发环境搭建各关卡通关答案
  15. 概率论三大公式 排列组合
  16. 动手学深度学习v2 课程笔记 — 深度学习基础
  17. MacOS - 快捷键以及各种操作汇总
  18. Tcl/Tk--文件操作
  19. Office 365绝技系列:3分钟完成PPT设计排版
  20. spring mvc前端验证代码生成器

热门文章

  1. 【AC】九度OJ题目1153:括号匹配问题
  2. 科比生涯自述:忍受,然后征服——这就是曼巴精神
  3. 推荐系统走向下一阶段最重要的三个问题
  4. 图卷积神经网络入门详解
  5. NLP学习难在哪里?这份最全NLP学习路线图帮你解决难题!
  6. 【NLP保姆级教程】手把手带你RNN文本分类(附代码)
  7. 零基础轻松学python pdf 小码哥_零基础轻松学Python
  8. php背景色如何填充满,php - 按一定百分比覆盖背景颜色
  9. Leetcode每日一题:56. I. 数组中数字出现的次数
  10. 吴恩达深度学习神经网络基础编程作业Python Basics with Numpy