sharding-ui实现sharding-proxy动态更新分库分表规则

在这里插入图片描述

前言

之前搭建了sharding-proxy,提供给客户端只有一个数据库连接,但是每次更新都需要重启服务,比较麻烦,看了下官方文档,支持zookeeper更新,还提供了页面更新,这里给出详细的搭建步骤。

环境

中间件 版本
zookeeper 3.4.13
sharding-proxy 4.1.1
sharding-ui 4.1.1

zookeeper搭建

这个之前搭建过,可以查看之前的文章。

shardingsphere

我们这次搭建使用的源码启动的。

sharding-proxy启动

在这里插入图片描述
sharding-proxy-bootstrap这个目录是启动目录,是一个标准的springboot项目,进到项目里面,需要配置两个配置文件

在这里插入图片描述

  • 配置config-sharding文件,配置的是数据库连接和分库分表规则
schemaName: fdc_do_facade

dataSources:
    fdc_do_0:
        url:  jdbc:mysql://{
    
    ip:prot}?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull
        username: {
    
    username}
        password: {
    
    password}
        connectionTimeoutMilliseconds: 30000
        idleTimeoutMilliseconds: 60000
        maxLifetimeMilliseconds: 1800000
        maxPoolSize: 50

shardingRule: #分库分表的规则
    tables: # 多个表就往下边追加
        fdc_stock: # 逻辑表的名字
            actualDataNodes: fdc_do_$->{
    
    0..7}.fdc_stock_$->{
    
    0..7}   #实际执行的节点
            databaseStrategy:
                standard:
                    shardingColumn:  XXX #分表字段
                    preciseAlgorithmClassName: XXX  #分表规则
    bindingTables:
        - fdc_stock
    defaultDatabaseStrategy:
        none:
    defaultTableStrategy:
        none:
  • server.yaml文件,配置sharding-proxy的账号和密码,以及配置zookeeper的地址

orchestration:
  orchestration_ds:
    orchestrationType: registry_center,config_center,distributed_lock_manager
    instanceType: zookeeper
    serverLists: 192.168.56.120:2181
    namespace: orchestration
    props:
      overwrite: false
      retryIntervalMilliseconds: 500
      timeToLiveSeconds: 60
      maxRetries: 3
      operationTimeoutMilliseconds: 500
      
authentication:
  users:
    root:
      password: root
    sharding:
      password: sharding
      authorizedSchemas: sharding_db
#
props:
#  max.connections.size.per.query: 1
  acceptor.size: 16  # The default value is available processors count * 2.
#  executor.size: 16  # Infinite by default.
#  proxy.frontend.flush.threshold: 128  # The default value is 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
#  proxy.opentracing.enabled: false
#  proxy.hint.enabled: false
#  query.with.cipher.column: true
  sql.show: true
#  allow.range.query.with.inline.sharding: false

  • 启动org.apache.shardingsphere.shardingproxy.Bootstrap这个文件,启动服务,使用navicat连接

ip:localhostport:3307username:rootpassword:root ,连接成功,说明搭建的没有问题。

sharding-ui搭建

在这里插入图片描述
分为两个工程:

  • shardingsphere-ui-backend这个工程是后端工程,提供接口服务,springboot项目。
  • shardingsphere-ui-frontend前端工程,vue项目。

启动后端工程,启动org.apache.shardingsphere.ui.Bootstrap文件

启动前端工程,先到shardingsphere-ui-frontend根目录下,然后执行npm install,之后npm run dev启动项目,之后访问http://localhost:8080/

在这里插入图片描述
账号密码都是admin
在这里插入图片描述

添加注册中心,配置可以复制sharding-proxyserver.yaml文件中配置
在这里插入图片描述

配置中心的配置

在这里插入图片描述

之后可以看到Rule Config菜单下可以配置ruledatasource

在这里插入图片描述
可以看到rule中的配置确实只有一个数据库的分库分表配置,接下来可以动态修改,sharding-proxy也会动态的修改。
在这里插入图片描述

动态添加分库分表规则

目前只有一张表,接下来可以在配置另外一张表。
在这里插入图片描述
打开rule配置添加另外一张表。

在这里插入图片描述
点击发布可以看到,数据库中有多了一另外一张表。

在这里插入图片描述
当我们打开新增表的数据的时候可以发现并没有数据

在这里插入图片描述
我们换一种方式查询,指定你要查询的字段,可以看到确实有数据了。

在这里插入图片描述

那为什么select *,就没有数据了呢,debug了下源码发现

org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.execute.callback.ProxySQLExecuteCallback#executeSQL()这个方法返回的 org.apache.shardingsphere.shardingproxy.backend.response.query.QueryHeader这个数据为空,select具体字段的时候就会返回的。这个是个bug,有发现的可以给出解决方案。

猜你喜欢

转载自blog.csdn.net/qq_37362891/article/details/109256506