背景
上一篇文章了解完jdbc示例,本文了解一下 ShardingSphere Proxy 相关的功能。
ShardingSphere-Proxy 相对于 ShardingSphere-JDBC 这种处于应用层的方案,是非侵入式,我个人理解是处于连接层,最为一个client存在,对应上层应用,屏蔽了数据库,以一个独立的server
数据库
数据库不需要初始化,因为在example-proxy里,会自动在关联的schema里创建相关的表
Proxy模块配置
server.yaml
全包名:shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/server.yaml
放开rule及以下的配置
rules:
- !AUTHORITY
users:
- root@%:root
- sharding@:sharding
provider:
type: NATIVE
props:
max-connections-size-per-query: 1
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
xa-transaction-manager-type: Atomikos
proxy-opentracing-enabled: false
proxy-hint-enabled: false
sql-show: false
check-table-metadata-enabled: false
lock-wait-timeout-milliseconds: 50000 # The maximum time to wait for a lock
show-process-list-enabled: false
# Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
# The default value is -1, which means set the minimum value for different JDBC drivers.
proxy-backend-query-fetch-size: -1
check-duplicate-table-enabled: false
启动proxy
找到启动类:shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/java/org/apache/shardingsphere/proxy/Bootstrap.java,点击main方法启动
数据分片
config-sharding.yaml
修改配置文件中的数据库密码为测试数据库账户密码
shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-sharding.yaml
启动相对应的example工程代码,控制台出现对应日志
Logic SQL: SELECT * FROM t_order;SQLStatement: MySQLSelectStatement(limit=Optional.empty, lock=Optional.empty, window=Optional.empty)Actual SQL: ds_0 ::: SELECT * FROM t_order_0 ORDER BY order_id ASC ;Actual SQL: ds_0 ::: SELECT * FROM t_order_1 ORDER BY order_id ASC ;Actual SQL: ds_1 ::: SELECT * FROM t_order_0 ORDER BY order_id ASC ;Actual SQL: ds_1 ::: SELECT * FROM t_order_1 ORDER BY order_id ASC ;
读写分离
config-readwrite-splitting.yaml
修改配置文件中的数据库密码为测试数据库账户密码 shardingsphere-proxy/shardingsphere-proxy-bootstrap/src/main/resources/conf/config-readwrite-splitting.yaml