ShardingSphere之sharding-proxy实战场景

shardingsphere-proxy安装与启动

下载shardingsphere-4.0.0-RC1版本(自己选择一个自己喜欢的版本即可)

http://mirrors.tuna.tsinghua.edu.cn/apache/incubator/shardingsphere/4.0.0-RC2/apache-shardingsphere-incubating-4.0.0-RC2-sharding-proxy-bin.tar.gz

解压

tar ‐zxvf apache‐shardingsphere‐incubating‐4.0.0‐RC2‐sharding‐proxy‐ bin.tar.gz 

解压后的目录:

引入依赖 

如果后端连接MySQL数据库,需要手动下载mysql驱动jar包,并将将mysqlconnector-java-${version}.jar拷贝到${sharding-proxy}\lib目录

启动配置

规则配置‐可根据需要选择

编辑%SHARDING_PROXY_HOME%\conf\config‐xxx.yaml

(config‐sharding.yaml‐数据分片设置,参考sharding‐jdbc)

(config‐master_slave.yaml‐主从读写分离设置,参考sharding‐jdbc)

(config‐encrypt.yaml‐数据脱敏设置,参考sharding‐jdbc)

全局配置:注册中心、认证信息以及公用属性 编辑%SHARDING_PROXY_HOME%\conf\server.yaml

数据分片 config-sharding.yam

schemaName: sharding_db #逻辑数据源名称

dataSources:
  ds0: 
    url: jdbc:mysql://127.0.0.1:3306/ds0?serverTimezone=UTC&useSSL=false
    username: root
    password: 
    connectionTimeoutMilliseconds: 30000 //连接超时毫秒数
    idleTimeoutMilliseconds: 60000 //空闲连接回收超时毫秒数
    maxLifetimeMilliseconds: 1800000 //连接最大存活时间毫秒数
    maxPoolSize: 65 //最大连接数
  ds1:
    url: jdbc:mysql://127.0.0.1:3306/ds1?serverTimezone=UTC&useSSL=false
    username: root
    password: 
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
#同sharding-jdbc设置
shardingRule:
  tables:
    t_order:
      actualDataNodes: ds${0..1}.t_order${0..1}
      databaseStrategy:
        inline:
          shardingColumn: user_id
          algorithmExpression: ds${user_id % 2}
      tableStrategy: 
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_id
    t_order_item:
      actualDataNodes: ds${0..1}.t_order_item${0..1}
      databaseStrategy:
        inline:
          shardingColumn: user_id
          algorithmExpression: ds${user_id % 2}
      tableStrategy:
        inline:
          shardingColumn: order_id
          algorithmExpression: t_order_item${order_id % 2}
      keyGenerator:
        type: SNOWFLAKE
        column: order_item_id
  bindingTables:
    - t_order,t_order_item
  defaultTableStrategy:
    none:

读写分离 config-master_slave.yaml

schemaName: master_slave_db

dataSources:
  ds_master:
    url: jdbc:mysql://127.0.0.1:3306/ds_master?serverTimezone=UTC&useSSL=false
    username: root
    password: 
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
  ds_slave0:
    url: jdbc:mysql://127.0.0.1:3306/ds_slave0?serverTimezone=UTC&useSSL=false
    username: root
    password: 
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
  ds_slave1:
    url: jdbc:mysql://127.0.0.1:3306/ds_slave1?serverTimezone=UTC&useSSL=false
    username: root
    password: 
    connectionTimeoutMilliseconds: 30000
    idleTimeoutMilliseconds: 60000
    maxLifetimeMilliseconds: 1800000
    maxPoolSize: 65
#主从路由规则
masterSlaveRule:
  name: ds_ms
  masterDataSourceName: ds_master
  slaveDataSourceNames: 
    - ds_slave0
    - ds_slave1
  loadBalanceAlgorithmType: ROUND_ROBIN

数据脱敏

dataSource: #省略数据源配置

encryptRule:
  encryptors:
    <encryptor-name>:
      type: #加解密器类型,可自定义或选择内置类型:MD5/AES 
      props: #属性配置, 注意:使用AES加密器,需要配置AES加密器的KEY属性:aes.key.value
        aes.key.value: 
  tables:
    <table-name>:
      columns:
        <logic-column-name>:
          plainColumn: #存储明文的字段
          cipherColumn: #存储密文的字段
          assistedQueryColumn: #辅助查询字段,针对ShardingQueryAssistedEncryptor类型的加解密器进行辅助查询
          encryptor: #加密器名字
props:
  query.with.cipher.column: true #是否使用密文列查询

全局配置 server.yaml

Sharding-Proxy使用conf/server.yaml配置注册中心、认证信息以及公用属性。

数据治理

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

认证信息(连接到sharding-proxy需要进行身份认证)
authentication:
  users:
    root: # 自定义用户名
      password: root # 自定义用户名
    sharding: # 自定义用户名
      password: sharding # 自定义用户名
      authorizedSchemas: sharding_db, masterslave_db(数据分片中配置的逻辑数据源)
      # 该用户授权可访问的数据库,多个用逗号分隔。缺省将拥有root权限,可访问全部数据库。
      
#省略与Sharding-JDBC一致的配置属性
props:
  acceptor.size: #用于设置接收客户端请求的工作线程个数,默认为CPU核数*2
  proxy.transaction.type: #默认为LOCAL事务,允许LOCAL,XA,BASE三个值,
  XA采用Atomikos作为事务管理器,BASE类型需要拷贝实现ShardingTransactionManager的接口的jar包至lib目录中
  proxy.opentracing.enabled: #是否开启链路追踪功能,默认为不开启。
  check.table.metadata.enabled: #是否在启动时检查分表元数据一致性,默认值: false
  proxy.frontend.flush.threshold: # 对于单个大查询,每多少个网络包返回一次
  executor.size: 16
  sql.show: false (or true)是否打印SQL语句

数据治理服务启动特别注意:

以下示例:
authentication:
  users:
    root:
      password: root
    tuling:
      password: tuling
      authorizedSchemas: shop_ds_proxy
#
orchestration:
  name: tuling_orchestration_proxy
  overwrite: false
  registry:
    type: zookeeper
    serverLists: 192.168.241.198:2181
    namespace: sharding_zookeeper_proxy
    
启动sharing-proxy时需要先在zookeeper上创建对应的Node节点如下:
${namespace}/${orchestration.name}/config/authentication
对应上述配置节点Node应为:/sharding_zookeeper_proxy/tuling_orchestration_proxy/config/authentication
然后设置该数据节点数据为:
users:
  root:
    password: root
  tuling:
    password: tuling
    authorizedSchemas: shop_ds_proxy
如果不设置,则抛节点值null空指针异常,proxy服务无法正常启动   

Yaml语法说明

!! 表示实例化该类

- 表示可以包含一个或多个

[] 表示数组,可以与减号相互替换使用

启动proxy服务

使用默认配置项启动

${sharding-proxy}\bin\start.sh

配置端口启动(默认端口3307)

${sharding-proxy}\bin\start.sh ${port}

停止服务

${sharding-proxy}\bin\stop.sh

sharding-proxy的基本特性

自定义分片算法:

当用户需要使用自定义的分片算法类时,无法再通过简单的inline表达式在yaml文件进行配置。可通过以下方式配置使用自定义分片算法。

  1. 实现ShardingAlgorithm接口定义的算法实现类。
  2. 将上述java文件打包成jar包。
  3. 将上述jar包拷贝至ShardingProxy解压后的conf/lib目录下。
  4. 将上述自定义算法实现类的java文件引用配置在yaml文件里tableRule的algorithmClassName属性上

分布式事务

Sharding-Proxy接入的分布式事务API同Sharding-JDBC保持一致,支持LOCAL,XA,BASE类型的事务。

XA事务

Sharding-Proxy原生支持XA事务,默认的事务管理器为Atomikos。 可以通过在Sharding-Proxy的conf目录中添加jta.properties来定制化Atomikos配置项。

BASE事务

BASE目前没有打包到Sharding-Proxy中,使用时需要将实现了ShardingTransactionManagerSPI的jar拷贝至conf/lib目录,然后切换事务类型为BASE。

SCTL(Sharding-Proxy control language)

SCTL为Sharding-Proxy特有的控制语句,可以在运行时修改和查询Sharding-Proxy的状态,目前支持的语法为:

语句

说明

sctl:set transaction_type=XX

修改当前TCP连接的事务类型, 支持LOCAL,XA,BASE。例:sctl:set transaction_type=XA

sctl:show transaction_type

查询当前TCP连接的事务类型

sctl:show cached_connections

查询当前TCP连接中缓存的物理数据库连接个数

sctl:explain SQL语句

查看逻辑SQL的执行计划,例:sctl:explain select * from t_order;

sctl:hint set MASTER_ONLY=true

针对当前TCP连接,是否将数据库操作强制路由到主库

sctl:hint set DatabaseShardingValue=yy

针对当前TCP连接,设置hint仅对数据库分片有效,并添加分片值,yy:数据库分片值

sctl:hint addDatabaseShardingValue xx=yy

针对当前TCP连接,为表xx添加分片值yy,xx:逻辑表名称,yy:数据库分片值

sctl:hint addTableShardingValue xx=yy

针对当前TCP连接,为表xx添加分片值yy,xx:逻辑表名称,yy:表分片值

sctl:hint clear

针对当前TCP连接,清除hint所有设置

sctl:hint show status

针对当前TCP连接,查询hint状态,master_only:true/false,sharding_type:databases_only/databases_tables

sctl:hint show table status

针对当前TCP连接,查询逻辑表的hint分片值

Sharding-Proxy 默认不支持hint,如需支持,请在conf/server.yaml中,将props的属性proxy.hint.enabled设置为true。在Sharding-Proxy中,HintShardingAlgorithm的泛型只能是String类型。

使用说明:

像使用SQL一样正常使用; 可以通过mysql-client工具连接使用

mysql -h 192.168.241.198 -P3308 -u tuling(authentication下配置的user) -ptuling((authentication下配置的pwd) 登录进来后执行sctl语句,如下图所示

 

注意事项

  1. Sharding-Proxy默认使用3307端口,可以通过启动脚本追加参数作为启动端口号。如: bin/start.sh 3308
  2. Sharding-Proxy使用conf/server.yaml配置注册中心、认证信息以及公用属性。
  3. Sharding-Proxy支持多逻辑数据源,每个以config-前缀命名的yaml配置文件,即为一个逻辑数据源。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小强同志

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

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

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

打赏作者

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

抵扣说明:

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

余额充值