分库分表中间件shardingsphere-proxy实战

前言:

         在我的上一篇文章中介绍了如何使用shardingsphere做分库分表 shardingsphere按年月分库分表实战_ITwenhao的博客-CSDN博客 我们可以看到,在数据库中存在的是通过分表规则创建的真实表,而我们实际业务中是想通过逻辑表查询数据,此时我们需要借助shardingsphere-proxy中间件,作为数据库的代理服务,他可以定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。他可以兼容MySQL/PostgreSQL 协议的访问客户端,比如navicat,SQLyog等客户端工具

下载地址: Index of /dist/shardingsphere

我这里使用的是4.1.0版本,由于上篇文中分库分表使用的4.x的版本,这里为了避免问题使用了对应版本,下载apache-shardingsphere-4.1.0-sharding-jdbc-bin.tar.gz

由于官网上默认没有mysql连接驱动包,首先我们需要下载一个mysql驱动包放在lib包下

在conf目录下有几个配置文件,主要关注的是server.yaml和config-sharding.yaml

server.yaml配置如下:

authentication:
  users:
    root:
      password: 123456
props:
  max.connections.size.per.query: 1
  acceptor.size: 16
  executor.size: 16
  proxy.frontend.flush.threshold: 128
  proxy.transaction.type: LOCAL
  proxy.opentracing.enabled: false
  proxy.hint.enabled: false
  query.with.cipher.column: true
  sql.show: true
  allow.range.query.with.inline.sharding: false

config-sharding.yaml配置如下:


schemaName: sharding
dataSources:
  default_dataSource:
    url: jdbc:mysql://localhost:3306/sharding?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  2020:
    url: jdbc:mysql://localhost:3306/2020_insurance_order?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  2021:
    url: jdbc:mysql://localhost:3306/2021_insurance_order?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
  2022:
    url: jdbc:mysql://localhost:3306/2022_insurance_order?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: 123456
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 50
shardingRule:
  tables:
  //insurance_order 为逻辑表名
    insurance_order:
    //actualDataNodes 是按照inline表达式生成的真实表名
      actualDataNodes: 2020.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}_2020_${1..9},2020.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}_2020_${10..12},2021.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}_2021_${1..9},2021.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}_2021_${10..12},2022.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}_2022_${1..9},2022.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}_2022_${10..12}
      databaseStrategy:
      	//分库策略采用的是standard标准分片策略
        standard:
        //preciseAlgorithmClassName和rangeAlgorithmClassName指定分库实现类的全类名
          preciseAlgorithmClassName: com.caoaman.sharding.DatabaseByYearShardingAlgorithm
          rangeAlgorithmClassName: com.caoaman.sharding.RangeDatabaseShardingAlgorithm
          shardingColumn: createTime //按照createTime字段进行按年分库
        //分表策略采用的是complex复合分片策略
      tableStrategy:
        complex:
        //algorithmClassName指定分表实现类的全类名
          algorithmClassName: com.caoaman.sharding.TableByCodeAndMonthShardingAlgorithm
          shardingColumns: createTime,insurCompanyCode //按照createTime和insurCompanyCode复合字段进行按业务code和月分表
  defaultDataSourceName: default_dataSource

上述配置中的全类名需要将源文件打成jar包放入lib中,不然启动shardingsphere-proxy会报错

将java文件打成jar包请参考: idea中将单个java类导出为jar包文件的方法 - 走看看

双击运行start.bat(我这里使用的是windows,如果是linux使用start.sh),发现启动起来了,端口为3307

通过navicat15发现可以连接成功  

 

 但是进入数据库时,报错如下

这里是navicat15的兼容bug,可以使用低版本的比如navicat12,或者使用SQLyog连接,效果如下  

上图没有带分片键条件查询,通过后台日志可以看到,会进行全路由查询,这样会极大影响效率,失去了分库分表的意义

下图带上分片键作为条件查询,则只路由到按分片键规则的表上,缩小了查询范围

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值