1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 分库分表中间件shardingsphere-proxy实战

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

时间:2023-03-16 08:36:45

相关推荐

分库分表中间件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: 123456props:max.connections.size.per.query: 1acceptor.size: 16executor.size: 16proxy.frontend.flush.threshold: 128proxy.transaction.type: LOCALproxy.opentracing.enabled: falseproxy.hint.enabled: falsequery.with.cipher.column: truesql.show: trueallow.range.query.with.inline.sharding: false

config-sharding.yaml配置如下:

schemaName: shardingdataSources:default_dataSource:url: jdbc:mysql://localhost:3306/sharding?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8username: rootpassword: 123456connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50:url: jdbc:mysql://localhost:3306/_insurance_order?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8username: rootpassword: 123456connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50:url: jdbc:mysql://localhost:3306/_insurance_order?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8username: rootpassword: 123456connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50:url: jdbc:mysql://localhost:3306/_insurance_order?userUnicode=ture&characterEncoding=utf8&serverTimezone=GMT%2B8username: rootpassword: 123456connectionTimeoutMilliseconds: 30000idleTimeoutMilliseconds: 60000maxLifetimeMilliseconds: 1800000maxPoolSize: 50shardingRule:tables://insurance_order 为逻辑表名insurance_order://actualDataNodes 是按照inline表达式生成的真实表名actualDataNodes: .insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}__${1..9},.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}__${10..12},.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}__${1..9},.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}__${10..12},.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}__${1..9},.insurance_order_${['pingan','taibao','yangguang','renbao','renshou']}__${10..12}databaseStrategy://分库策略采用的是standard标准分片策略standard://preciseAlgorithmClassName和rangeAlgorithmClassName指定分库实现类的全类名preciseAlgorithmClassName: com.caoaman.sharding.DatabaseByYearShardingAlgorithmrangeAlgorithmClassName: com.caoaman.sharding.RangeDatabaseShardingAlgorithmshardingColumn: createTime //按照createTime字段进行按年分库//分表策略采用的是complex复合分片策略tableStrategy:complex://algorithmClassName指定分表实现类的全类名algorithmClassName: com.caoaman.sharding.TableByCodeAndMonthShardingAlgorithmshardingColumns: 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连接,效果如下

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。