sharding-proxy代理分库分表配置

原理看官网:https://shardingsphere.apache.org/document/current/cn/overview/

sharding proxy代理mysql实现分库的步骤:

  1. 下载

wget https://mirrors.tuna.tsinghua.edu.cn/apache/shardingsphere/4.1.0/apache-shardingsphere-4.1.0-sharding-proxy-bin.tar.gz

  1. 解压

tar xvf apache-shardingsphere-4.1.0-sharding-proxy-bin.tar.gz

  1. 使用mysql数据库时需要自己导入运行工具:

(1) 下载

wget https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.47.tar.gz

(2) 解压:

tar xvf mysql-connector-java-5.1.47

(3) 把运行 jar复制到sharding proxy lib/

cd cd mysql-connector-java-5.1.47

cp mysql-connector-java-5.1.47.jar /_mytools/sharding-proxy/v4.1.0/lib/

  1. sharding proxy基本配置server.yaml

cd apache-shardingsphere-4.1.0-sharding-proxy-bin/conf/

 

#用户配置

authentication:

  users: #用户列表

    root: #名为root的用户

      password: root  #连接密码

    sharding: #名为 sharding的用户

      password: sharding #密码

      authorizedSchemas: sharding_db #为sharding用户配置管理表的权限,不配置时为全部权限

 

#sharding proxy 基本参数配置

props:

  max.connections.size.per.query: 1 #每个查询可以打开的最大连接数量,默认为1

  acceptor.size: 12  # 用于设置接收客户端请求的工作线程个数,默认为CPU核数*2.

  executor.size: 6  # 工作线程数量,默认值: CPU核数

  proxy.frontend.flush.threshold: 128  # 对於单个大查询,每多少个网络包返回一次 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: LOCAL #默认为LOCAL事务,允许LOCAL,XA,BASE三个值,XA采用Atomikos作为事务管理器,BASE类型需要拷贝实现ShardingTransactionManager的接口的jar包至lib目录中

  proxy.opentracing.enabled: false  #是否开启链路追踪功能,默认为不开启

  proxy.hint.enabled: false  #是否启用hint算法强制路由 默认false

  query.with.cipher.column: true #是否使用密文列查询 默认false

  sql.show: false  #是否打印sql 默认false

  allow.range.query.with.inline.sharding: false  #允许范围查询,默认为 false ,要是我们分库分表是水平切分,可以想得到范围查询会像广播去查每一个表,比较耗性能能

 

 

  1. 数据源、分片配置config-sharding.yaml

(1) 逻辑库名称

schemaName: sharding_db

 

(2) 数据源配置


dataSources:
#数据源别名,保证唯一且最好与分片列保持规律性,
  ds_37:
#数据源地址,后面是为了统一日期和管理乱码,配置时物理库必须存在
    url: jdbc:mysql://192.168.2.170:3306/ds37?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
#连接超时时间
connectionTimeoutMilliseconds: 30000
#空闲连接回收超时毫秒数
idleTimeoutMilliseconds: 60000
#连接最大存活时间毫秒数
maxLifetimeMilliseconds: 1800000
#最大连接数
maxPoolSize: 50
ds_45:
url: jdbc:mysql://192.168.2.170:3306/ds45?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai
username: root
password: 123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50

 

(3) 分片规则


shardingRule:
tables: #数据分片规则,可配置多个
seal_data: #逻辑库中的逻辑表名
actualDataNodes: ds_${['37','45']}.seal_data #规则节点,逻辑数据源+逻辑表名 支持inline表达式。(缺省表示自动匹配全部逻辑库和逻辑表组合,每个库需要一个逻辑表,若没有自动创建一个,适用于只分库不分表且分库的逻辑表结构完全一致的情况)
# tableStrategy: #分表规则属性
# inline: #分表规则算法
# shardingColumn: area_code  #分表分片列
# algorithmExpression: seal_data_${area_code.toInteger().intdiv(100)} #分表规则表达式
# keyGenerator:  #分表主键属性
# type: SNOWFLAKE #主键生成算法
# column: seal_code #主键列
# bindingTables: # 绑定关系表
# - seal_data
databaseStrategy: #分库属性
standard:  #使用standard算法
shardingColumn: STR_AREA_CODE #分库分片列
preciseAlgorithmClassName: com.CustomShardingTest #自定义的分库算法(制作方式)
defaultDatabaseStrategy: #默认分库规则
none:
# inline:
# shardingColumn: STR_AREA_CODE
# algorithmExpression: ds_${STR_AREA_CODE.toInteger().intdiv(10000)}
# preciseAlgorithmClassName: splitDB-1.0-SNAPSHOT.jar
defaultTableStrategy:   #默认分表规则
none:

 

(4) 分片策略,官网有https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-yaml/

 

 

  1. 其他:

(1) 启动:

安装目录:/_mytools/sharding-proxy/v4.1.0/bin/start.sh

(2) 连接:

服务ip:3307   用户名和密码由上方配置获取

(3) 使用Navicat连接查看逻辑表时,注意数据库编码问题,可能导致字段长度被限制显示不完整

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章