sharding-proxy+sharding-ui安裝使用配置
sharding官網
https://shardingsphere.apache.org/document/current/cn/manual/sharding-proxy/
運行mysql(已運行則略)

啟動zookeeper(已啟動則略)

sharding-proxy安裝啟動配置
- 下載sharding-proxy(這里沒有采用docker的方式)
wget https://mirrors.tuna.tsinghua.edu.cn/apache/shardingsphere/4.1.0/apache-shardingsphere-4.1.0-sharding-proxy-bin.tar.gz
- 解壓
tar -xf apache-shardingsphere-4.1.0-sharding-proxy-bin.tar.gz
- 進入conf
vim server.yaml
orchestration:
orchestration_ds:
orchestrationType: registry_center,config_center,distributed_lock_manager
instanceType: zookeeper
serverLists: 192.168.1.106:2181
namespace: orchestration
props:
overwrite: true
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: false
allow.range.query.with.inline.sharding: false
修改 config-sharding.yaml
schemaName: sharding_db
dataSources:
ds_0:
url: jdbc:mysql://192.168.1.104:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
ds_1:
url: jdbc:mysql://192.168.1.104:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: root
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
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}
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
defaultDatabaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
defaultTableStrategy:
none:
- mysql建測試庫和表
庫名表名要和配置文件內容相匹配,sharding-proxy不會自己給你新建真實庫和表

- 下載mysql連接驅動
wget https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.49.tar.gz
-
解壓后放到lib目錄下
-
啟動
bin/start.sh -

-
查看日志
tail -200f /root/apache-shardingsphere-4.1.0-sharding-proxy-bin/logs/stdout.log
-
啟動成功

-
Navicat連接proxy代理數據源
代理數據源的schema和table啟動后自己建好了 登錄信息在conf目錄下的server.yaml

sharding-ui啟動使用
- 下載
wget https://mirror.bit.edu.cn/apache/shardingsphere/shardingsphere-ui-4.1.0/apache-shardingsphere-4.1.0-shardingsphere-ui-bin.tar.gz
-
解壓

-
查看sharding-ui的配置文件

-
啟動sharding-ui
bin/start.sh
-
查看日志是否成功
-
瀏覽器訪問服務器ip:8088

端口、用戶名、密碼在sharding-ui下conf下application.properties配置
-
添加注冊中心

參考sharding-proxy下conf下server.yaml
-
連接

-
運行狀態

運行狀態下就可以看到運行的sharding-proxy服務節點了
-
添加服務中心

-
配置管理


連接后會自動把配置信息展示在界面上
比如我們可以更改數據源中的數據重新定義分庫分表規則
也可以定義多個數據源
后端微服務可以配置代理數據源即可實現分庫分表
