文章目录
环境准备
准备 ShardingSphere-Proxy 与 ShardingSphere-UI , 这次使用 5.0.0-alpha 版本.
ShardingSphere-Proxy 下载地址: https://archive.apache.org/dist/shardingsphere/5.0.0-alpha/
ShardingSphere-UI 下载地址:https://archive.apache.org/dist/shardingsphere/shardingsphere-ui-5.0.0-alpha/
ShardingSphere-Proxy 配置
将 ShardingSphere-Proxy 5.0.0-alpha 下载并解压后, 针对 conf 目录下的 server.yaml、config-xxx.yaml 文件做修改.
另外还需将 mysql 的驱动包放在 ext-lib 目录下 (没有就新建一个)
server.yaml 服务配置
server.yaml 中需要放开 Zookeeper 的链接配置、权限配置、以及属性配置
governance:
name: governance_ds
registryCenter:
type: ZooKeeper
serverLists: localhost:2181
props:
retryIntervalMilliseconds: 500
timeToLiveSeconds: 60
maxRetries: 3
operationTimeoutMilliseconds: 500
overwrite: false
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
check-table-metadata-enabled: false
config-replica-query.yaml 读写分离
config-replica-query.yaml 中需要放开 mysql 部分的配置, 以及链接信息少量修改
schemaName: readwrite-splitting_db
dataSourceCommon:
username: root
password: 123
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
dataSources:
write_ds:
url: jdbc:mysql://127.0.0.1:3306/demo_write_ds?serverTimezone=UTC&useSSL=false
read_ds_0:
url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_0?serverTimezone=UTC&useSSL=false
read_ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_read_ds_1?serverTimezone=UTC&useSSL=false
rules:
- !REPLICA_QUERY
dataSources:
pr_ds:
name: pr_ds
primaryDataSourceName: write_ds
replicaDataSourceNames:
- read_ds_0
- read_ds_1
config-sharding.yaml 数据分片
config-sharding.yaml 同样是放开 mysql 部分配置即可
schemaName: sharding_db
dataSourceCommon:
username: root
password: 123
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
maintenanceIntervalMilliseconds: 30000
dataSources:
ds_0:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_inline
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_${0..1}
tableStrategy:
standard:
shardingColumn: order_id
shardingAlgorithmName: t_order_item_inline
keyGenerateStrategy:
column: order_item_id
keyGeneratorName: snowflake
bindingTables:
- t_order,t_order_item
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database_inline
defaultTableStrategy:
none:
shardingAlgorithms:
database_inline:
type: INLINE
props:
algorithm-expression: ds_${user_id % 2}
t_order_inline:
type: INLINE
props:
algorithm-expression: t_order_${order_id % 2}
t_order_item_inline:
type: INLINE
props:
algorithm-expression: t_order_item_${order_id % 2}
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123
启动 ShardingSphere-Proxy
由于我是 linux 环境下, 直接运行 bin 目录下的 start.sh
脚本即可.
观察 logs/stdout.log
日志发现错误信息并排查.
需要注意的是, 如果是配置错误导致启动 Proxy 失败, 即使是修改好配置文件, 下次依旧是旧的错误日志信息. 这是因为错误配置已经写入 Zookeeper 中.
解决办法是链接 Zookeeper 将错误配置的节点删除即可.
成功打开后, 通过可视化工具观测 Zookeeper 节点的信息如下:
启动 ShardingSphere-UI
直接运行 bin 目录下的 start.sh
, 并观察 logs/stdout.log
日志.
启动成功后通过 web 端登录, 地址 localhost:8088
进入之后先配置注册中心, 也就是需要链接上 Zookeeper 并选择一个要治理的节点
由于是要治理的节点, 需要与 Proxy 中 server.yaml 里配置的 Zookeeper 节点一致, 这样才能读到 Proxy 中的配置信息.
配置完毕后, 点击 “连接” 激活注册中心的连接.
成功连接后, 可以在 “配置管理” 中看到 Proxy 上传到 Zookeeper 上的配置信息.
小结
ShardingSphere-UI 提供的管理配置信息机制, 其原理还是来自 ShardingSphere-Proxy 与 Zookeeper 的结合.
ShardingSphere-Proxy 将所有配置信息托管到 Zookeeper 中, 并在之后的使用中直接读取 Zookeeper 的信息.
而 ShardingSphere-UI 做的事情, 仅是可视化的操作 Zookeeper 上关于 Proxy 的配置信息.