一、前言

1、版本

apache-shardingsphere-5.0.0-shardingsphere-proxy-bin

下载页面 https://www.apache.org/dyn/closer.cgi/shardingsphere/5.0.0/apache-shardingsphere-5.0.0-shardingsphere-proxy-bin.tar.gz

二、快速开始

1、配置文件

分表需要配置两个文件(server.yaml、config-sharding.yaml)

server.yaml

  1. rules:
  2. - !AUTHORITY
  3. users:
  4. - root@%:root
  5. - sharding@:sharding
  6. provider:
  7. type: ALL_PRIVILEGES_PERMITTED
  8. - !TRANSACTION
  9. defaultType: XA
  10. providerType: Atomikos
  11. props:
  12. max-connections-size-per-query: 1
  13. kernel-executor-size: 16 # Infinite by default.
  14. proxy-frontend-flush-threshold: 128 # The default value is 128.
  15. proxy-opentracing-enabled: false
  16. proxy-hint-enabled: false
  17. sql-show: false
  18. check-table-metadata-enabled: false
  19. show-process-list-enabled: false
  20. # Proxy backend query fetch size. A larger value may increase the memory usage of ShardingSphere Proxy.
  21. # The default value is -1, which means set the minimum value for different JDBC drivers.
  22. proxy-backend-query-fetch-size: -1
  23. check-duplicate-table-enabled: false
  24. sql-comment-parse-enabled: false
  25. proxy-frontend-executor-size: 0 # Proxy frontend executor size. The default value is 0, which means let Netty decide.
  26. # Available options of proxy backend executor suitable: OLAP(default), OLTP. The OLTP option may reduce time cost of writing packets to client, but it may increase the latency of SQL execution
  27. # if client connections are more than proxy-frontend-netty-executor-size, especially executing slow SQL.
  28. proxy-backend-executor-suitable: OLAP
  29. proxy-frontend-max-connections: 0 # Less than or equal to 0 means no limitation.
  30. sql-federation-enabled: false

config-sharding.yaml

  1. schemaName: sharding_db
  2. dataSources:
  3. ds_0:
  4. url: jdbc:mysql://localhost:3306/smiler_user?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
  5. username: root
  6. password: 123456
  7. connectionTimeoutMilliseconds: 30000
  8. idleTimeoutMilliseconds: 60000
  9. maxLifetimeMilliseconds: 1800000
  10. maxPoolSize: 50
  11. minPoolSize: 1
  12. rules:
  13. - !SHARDING
  14. tables:
  15. smiler_user:
  16. actualDataNodes: ds_0.smiler_user_$->{1..2}
  17. tableStrategy:
  18. standard:
  19. shardingColumn: id
  20. shardingAlgorithmName: smiler-user-inline
  21. shardingAlgorithms:
  22. smiler-user-inline:
  23. type: INLINE
  24. props:
  25. algorithm-expression: smiler_user_${id % 2 + 1}

2、启动服务

image.png
执行如下命令,则可以启动

  1. ./start.sh

image.png
启动成功后看下输出的日志(在 logs 文件夹中)
image.png
端口默认是 3307

3、java 连接 proxy

https://github.com/supkingx/shardingjdbcTest/blob/master/src/main/resources/application.properties

4、客户端连接 proxy

image.png
image.png
这里不需要使用实际表(smiler_user_1 或者 smiler_user_2),而是可以使用 smiler_user,proxy会自动进行路由分片