前言
之前搭建了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:localhost
,port:3307
,username:root
,password: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-proxy
中server.yaml
文件中配置
配置中心的配置
之后可以看到Rule Config
菜单下可以配置rule
和datasource
可以看到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
,有发现的可以给出解决方案。