首页 > 其他分享 >Sharding-Proxy 分库分表示例

Sharding-Proxy 分库分表示例

时间:2023-01-10 23:32:29浏览次数:55  
标签:分库 sharding Proxy Sharding table true ds proxy


我司当前的一个数据库,分了51个数据库及表,使用 Sharding-JDBC 去独立连接。(Sharding-JDBC是Sharding-Sphere的第一个产品,也是Sharding-Sphere的前身。 它定位为轻量级Java框架,在Java的JDBC层提供的额外服务。它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。) 问题是,我们的51个分库都在同一个实例上,MySQL 实例的连接成了瓶颈。 仅仅这 51 个数据库,7个应用去连接,若每个连接设置 10 个连接池,总共就使用了 3570( 51*7*10)个MySQL连接。还有公司人员,因为数据库太分散不好统计数据,又单独配置 mycat 给内部人员使用,占用一部分连接池。由于 MySQL 使用阿里云RDS,不能调整连接,只能加钱升级配置。

 

Sharding-JDBC 架构:

Sharding-Proxy 分库分表示例_MySQL

 

在使用 mycat 也遇到许多问题,所以再部署 Sharding-Proxy 探索对比看看。Sharding-Proxy是Sharding-Sphere的第二个产品。 它定位为透明化的数据库代理端,提供封装了数据库二进制协议的服务端版本,用于完成对异构语言的支持。 目前先提供MySQL版本,它可以使用任何兼容MySQL协议的访问客户端
 

Sharding-Proxy 架构:

Sharding-Proxy 分库分表示例_mysql_02

 

Sharding-Proxy 最新发行版地址 :

​https://github.com/sharding-sphere/sharding-sphere-doc/raw/master/dist/sharding-proxy-3.0.0.tar.gz​

文件解压可使用,Sharding-Proxy 很简洁,只有4个文件夹(bin、conf、lib、logs)。

以下只是一个被剪辑过的示例,仅供参考。

 

配置分表规则:

vim /opt/sharding-proxy-3.0.0/conf/config-sharding.yaml
######################################################################################################
#
# Here you can configure the rules for the proxy.
# This example is configuration of sharding rule.
#
# If you want to use sharding, please refer to this file;
# if you want to use master-slave, please refer to the config-master_slave.yaml.
#
######################################################################################################

# 逻辑数据库名称(自定义)
schemaName: testdb

# 各个节点的名称及数据库连接地址
dataSources:
ds_Default:
url: jdbc:mysql://10.10.10.10:3306/testdb?characterEncoding=utf8&useSSL=true
username: root
password: mysql
autoCommit: true
connectionTimeout: 30000
idleTimeout: 60000
maxLifetime: 1800000
maximumPoolSize: 5
ds_0:
url: jdbc:mysql://10.10.10.10:3306/testdb_d0?characterEncoding=utf8&useSSL=true
username: root
password: mysql
autoCommit: true
connectionTimeout: 30000
idleTimeout: 60000
maxLifetime: 1800000
maximumPoolSize: 5
ds_1:
url: jdbc:mysql://10.10.10.10:3306/testdb_d1?characterEncoding=utf8&useSSL=true
username: root
password: mysql
autoCommit: true
connectionTimeout: 30000
idleTimeout: 60000
maxLifetime: 1800000
maximumPoolSize: 5
ds_2:
url: jdbc:mysql://10.10.10.10:3306/testdb_d2?characterEncoding=utf8&useSSL=true
username: root
password: mysql
autoCommit: true
connectionTimeout: 30000
idleTimeout: 60000
maxLifetime: 1800000
maximumPoolSize: 5

# 分表规则
shardingRule:
tables:
table_a: #逻辑表名(与原表保持一致吧)
actualDataNodes: ds_${0..2}.table_a #各个节点实际的表名。ds_${0..2} 表示:ds_0,ds_1,ds_2
#这里还可以设置分表规则,现在统一在下面的 defaultDatabaseStrategy 设置
table_b:
actualDataNodes: ds_${0..2}.table_b
table_c:
actualDataNodes: ds_Default.table_c # 不分库的表,配置到该节点 ds_Default
table_d:
actualDataNodes: ds_Default.table_d

#绑定表规则列表
bindingTables:
- table_a,table_b,table_c,table_d
#默认数据库分片策略,同分库策略。根据表字段 user_id 哈希路由到三个节点:ds_0,ds_1,ds_2
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 3}
#默认表分片策略,同分库策略
defaultTableStrategy:
none:
#默认自增列值生成器类名称
defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator

服务器级别配置:

vim /opt/sharding-proxy-3.0.0/conf/server.yaml
######################################################################################################
#
# If you want to configure orchestration, authorization and proxy properties, please refer to this file.
#
######################################################################################################

#dataSources: #省略数据源配置
#shardingRule: #省略分片规则配置
#masterSlaveRule: #省略读写分离规则配置

#orchestration:
# name: orchestration_ds # 数据治理实例名称
# overwrite: true # 本地配置是否覆盖注册中心配置。如果可覆盖,每次启动都以本地配置为准
# registry: #注册中心配置
# serverLists: localhost:2181 #连接注册中心服务器的列表。包括IP地址和端口号。多个地址用逗号分隔。如: host1:2181,host2:2181
# namespace: orchestration #注册中心的命名空间
# digest: #连接注册中心的权限令牌。缺省为不需要权限验证
# operationTimeoutMilliseconds: #操作超时的毫秒数,默认500毫秒
# maxRetries: #连接失败后的最大重试次数,默认3次
# retryIntervalMilliseconds: #重试间隔毫秒数,默认500毫秒
# timeToLiveSeconds: #临时节点存活秒数,默认60秒

# 设置 sharding-proxy 的登录账号密码
authentication:
username: admin
password: adminpwd

props:
# max.connections.size.per.query: 1
acceptor.size: 8 # 用于设置接收客户端请求的工作线程个数,默认为CPU核数*2
executor.size: 4 # 工作线程数量,默认值: CPU核数
proxy.transaction.enabled: false # 是否开启事务, 目前仅支持XA事务,默认为不开启
proxy.opentracing.enabled: false # 是否开启链路追踪功能,默认为不开启
sql.show: true # 是否开启SQL显示,默认值: false

启动:

cd /opt/sharding-proxy-3.0.0/bin
./start.sh


#默认端口 3307 ,可自定义端口使用
./start.sh 3308

日志查看,

tail -f /opt/sharding-proxy-3.0.0/logs/stdout.log

 

 

标签:分库,sharding,Proxy,Sharding,table,true,ds,proxy
From: https://blog.51cto.com/hzc2012/6000777

相关文章

  • but was actually of type 'com.sun.proxy.$Proxy49'
    org.springframework.beans.factory.UnsatisfiedDependencyException:Errorcreatingbeanwithname'com.jeesite.modules.tianqi.project.service.ProjectServiceTest'......
  • 使用Es6提供的构造函数Proxy实现数据绑定
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</......
  • .net core 基于Dapper 的分库分表开源框架(core-data)
    一、前言感觉很久没写文章了,最近也比较忙,写的相对比较少,抽空分享基于Dapper的分库分表开源框架core-data的强大功能,更好的提高开发过程中的效率;在数据库的数据日积月累......
  • Error occurred while proxying request localhost:端口 报错500的解决方法
    '/AuthServer/api/':{target:'https://localhost:44319',secure:false,//这是签名认证,http和https区分的参数设置changeOrigin:true,......
  • Proxy
    Object.defineProperty缺陷:1.设计的初衷不是为了监听一个对象中的所有属性,初衷是定义普通的属性2.无法对新增属性、删除属性进行监听Proxy代理对象/*constp=n......
  • shardingsphere
    shardingsphere目录shardingsphere1.面试题1.1业务增长-数据库性能优化2.分库分表带来的优点3.分库分表带来的问题4.分库分表核心概念4.1垂直分表例子:商品详情一般是拆......
  • Mysql ProxySQL
    rpm-ivhproxysql-2.4.1-1-centos7.x86_64.rpm版本:proxysql--version启动:serviceproxysqlstart暂停:serviceproxysqlstop重启:serviceproxysqlrestart状态:service......
  • nginx中proxy_set_header Host $host的作用
    nginx中proxy_set_headerHost$host的作用路由配置:起初没有配置 proxy_set_headerHost$host 等参数,请求总是报400(badrequest). server{listen80;......
  • 一文读懂数据库优化之分库分表
       本文从5W1H角度介绍了分库分表手段,其在解决如IO瓶颈、读写性能、物理存储瓶颈、内存瓶颈、单机故障影响面等问题的同时也带来如事务性、主键冲突、跨库joi......
  • 一文读懂数据库优化之分库分表
       本文从5W1H角度介绍了分库分表手段,其在解决如IO瓶颈、读写性能、物理存储瓶颈、内存瓶颈、单机故障影响面等问题的同时也带来如事务性、主键冲突、跨库joi......