ShardingSphere 5.0.0-alpha 分库分表解决方案(七)——proxy分表

1. ShardingSphere-Proxy基本介绍

之前使用的ShardingSphere-JDBC的原理架构是这样的:对于每一个应用都需要配置Sharding-JDBC

在这里插入图片描述

上面这样是不是和麻烦呢,来看下ShardingSphere-Proxy的原理图:是不是一目了然

在这里插入图片描述

没错,ShardingSphere-Proxy提供透明化的数据库代理端,而不用每个应用服务都要独立的配置ShardingSphere-JDBC

向应用程序完全透明,可直接当做MySQL使用

2. ShardingSphere-Proxy安装

2.1 下载

下载ShardingSphere-Proxy最新版

https://shardingsphere.apache.org/document/current/cn/downloads/

2.2 解压

把下载之后压缩文件,解压,启动bin目录启动文件就可以了,但需要做相关配置,暂时先不要启动

2.3 配置

2.3.1 server.yaml

配置server.yaml ,把下面的配置的 # 注释打开,改为符合自己的配置。

authentication:
  users:
    root:
      password: root
    sharding:
      password: sharding 
      authorizedSchemas: sharding_db
props:
  max-connections-size-per-query: 1
  acceptor-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.
  proxy-transaction-type: LOCAL
  proxy-opentracing-enabled: false
  proxy-hint-enabled: false
  query-with-cipher-column: true
  sql-show: true
  check-table-metadata-enabled: false

2.3.2 config-sharding.yaml

配置分表规则config-sharding.yaml ,把最下面MySQL的注释改为符合自己的配置

# 只配置分表
schemaName: sharding_db
dataSourceCommon:
  username: root
  password: 123456
  connectionTimeoutMilliseconds: 30000
  idleTimeoutMilliseconds: 60000
  maxLifetimeMilliseconds: 1800000
  maxPoolSize: 50
  minPoolSize: 1
  maintenanceIntervalMilliseconds: 30000

dataSources:
  ds_0:
    url: jdbc:mysql://127.0.0.1:3306/ss_course_db_proxy_1?serverTimezone=UTC&useSSL=false
rules:
- !SHARDING
  tables:
    t_course:
      actualDataNodes: ds_${0}.t_course_${1..2}
      tableStrategy:
        standard:
          shardingColumn: cid
          shardingAlgorithmName: t_course_inline
      keyGenerateStrategy:
        column: cid
        keyGeneratorName: snowflake
  bindingTables:
    - t_course
  defaultDatabaseStrategy:
    standard:
      shardingColumn: user_id
      shardingAlgorithmName: database_inline
  defaultTableStrategy:
    none:
  shardingAlgorithms:
    database_inline:
      type: INLINE
      props:
        algorithm-expression: ds_0
    t_course_inline:
      type: INLINE
      props:
        algorithm-expression: t_course_${cid % 2 + 1}
  keyGenerators:
    snowflake:
      type: SNOWFLAKE
      props:
        worker-id: 123

如果需要自己制定ShardingSphere-Proxy创建的数据库名称,需要自己手动改下上面配置文件中的authorizedSchemasschemaName两个属性

2.3.3 启动ShardingSphere

这地方要注意的是:如果后端连接 MySQL数据库,需要手动下载MySQL对应版本的驱动包并将其放入 lib 目录下

启动ShardingSphere-Proxy服务,ShardingSphere-Proxy默认端口号 3307。由于我之前配置从数据库的端口是3307,所以此处在启动的时候我把ShardingSphere-Proxy端口设为3308

在这里插入图片描述
在这里插入图片描述

2.3.4 连接

连接方式和连接MySQL一样,使用MySQL命令行工具进行连接,并进行命令操作,看到只有一个库

在这里插入图片描述

sharding_db数据库创建表,该代理层会根据分库分表规则自动在后端对应的分库中创建表

在这里插入图片描述

向表添加一条记录

在这里插入图片描述

回到本地3306端口实际数据库中,看到已经创建好了表和添加数据

在这里插入图片描述

3. ShardingSphere-Proxy 分表

3.1 环境搭建

环境说明:SpringBoot 2.5.7+ MyBatisPlus + ShardingSphere-JDBC 5.0.0-alpha + Druid+ MySQL 8.0

3.1.1 pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.13</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.27</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

注意:此处在pom文件中,要移除shardingsphere-jdbc-core-spring-boot-starter依赖

3.1.2 编写业务代码

此处编写业务代码略,具体代码可以下面的源码地址,经过本人调试过。代码里集成了Swagger,用于方便测试。

3.1.3 配置文件

server.port=8006

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 此处要连接sharding-proxy库
spring.datasource.url=jdbc:mysql://localhost:3308/sharding_db?useServerPrepStmts=true&cachePrepStmts=true
spring.datasource.username=root
spring.datasource.password=root

此处要连接sharding-proxy库,和操作MySQL数据库一样

3.1.5 测试结果

启动程序,在浏览器输入:http://localhost:8006/swagger-ui.html

在这里插入图片描述

插入数据

在这里插入图片描述

Sharding-Proxy服务日志,忽略乱码吧

在这里插入图片描述

数据插入数据库成功

在这里插入图片描述

查询数据

在这里插入图片描述

Sharding-Proxy服务查询日志

在这里插入图片描述

4. 说明

源码地址:https://github.com/Hofanking/springboot-shardingsphere-example

源代码目录结构说明:

springboot-shardingsphere-example

​ |— shardingsphere-database (分库分表)

​ |— shardingsphere-database-table-write-read (分库分表读写分离)

​ |— shardingsphere-proxy-table (使用proxy分表)

​ |— shardingsphere-public (公共表)

​ |— shardingsphere-table (分表)

​ |— shardingsphere-write-read (读写分离)

5.坑

本人在实际操作的过程中,没有将shardingsphere-jdbc-core-spring-boot-starter依赖移除,导致项目运行不起来,看配置也没有问题,后来尝试将shardingsphere-jdbc-core-spring-boot-starter依赖移除,发现就可以了。

在项目的配置文件中,只需要配置数据源,而且写的sharding-proxy的地址,这样就完全将sharding-proxyMySQL一样用了。

可能是我本人一开始没有理解吧。对于基于sharding-proxy的分库分表,读写分离,就不在往下研究了,因为只需要把sharding-jdbc的配置研究明白,其他的都类似。只不过sharding-proxy的配置是在conf目录下的指定配置文件中,这一条路算是趟过了。

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

止步前行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值