• 分库分表之sharding-proxy


    一、环境介绍

    • windows10
    • mysql 版本8.0.16
    • sharding-proxy版本5.0.0 :https://archive.apache.org/dist/shardingsphere/5.0.0/apache-shardingsphere-5.0.0-shardingsphere-proxy-bin.tar.gz

    二、安装

    • 将shardingsphere解压,保证lib中的jar包文件名称完整,同时下载mysql-connection-java-8.0.16 jar包放到lib目录下,修改conf中的server.yaml 和 config-sharding.yaml
    1. rules:
    2. - !AUTHORITY
    3. users:
    4. - root@%:xrq123
    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
    1. schemaName: sharding_db
    2. dataSources:
    3. ds_0:
    4. url: jdbc:mysql://127.0.0.1:3306/demo_ds_0?serverTimezone=UTC&useSSL=false
    5. username: root
    6. password: xrq123
    7. connectionTimeoutMilliseconds: 30000
    8. idleTimeoutMilliseconds: 60000
    9. maxLifetimeMilliseconds: 1800000
    10. maxPoolSize: 50
    11. minPoolSize: 1
    12. ds_1:
    13. url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
    14. username: root
    15. password: xrq123
    16. connectionTimeoutMilliseconds: 30000
    17. idleTimeoutMilliseconds: 60000
    18. maxLifetimeMilliseconds: 1800000
    19. maxPoolSize: 50
    20. minPoolSize: 1
    21. rules:
    22. - !SHARDING
    23. tables:
    24. t_order:
    25. actualDataNodes: ds_${0..1}.t_order_${0..1}
    26. tableStrategy:
    27. standard:
    28. shardingColumn: order_id
    29. shardingAlgorithmName: t_order_inline
    30. keyGenerateStrategy:
    31. column: order_id
    32. keyGeneratorName: snowflake
    33. t_order_item:
    34. actualDataNodes: ds_${0..1}.t_order_item_${0..1}
    35. tableStrategy:
    36. standard:
    37. shardingColumn: order_id
    38. shardingAlgorithmName: t_order_item_inline
    39. keyGenerateStrategy:
    40. column: order_item_id
    41. keyGeneratorName: snowflake
    42. bindingTables:
    43. - t_order,t_order_item
    44. defaultDatabaseStrategy:
    45. standard:
    46. shardingColumn: user_id
    47. shardingAlgorithmName: database_inline
    48. defaultTableStrategy:
    49. none:
    50. shardingAlgorithms:
    51. database_inline:
    52. type: INLINE
    53. props:
    54. algorithm-expression: ds_${user_id % 2}
    55. t_order_inline:
    56. type: INLINE
    57. props:
    58. algorithm-expression: t_order_${order_id % 2}
    59. t_order_item_inline:
    60. type: INLINE
    61. props:
    62. algorithm-expression: t_order_item_${order_id % 2}
    63. keyGenerators:
    64. snowflake:
    65. type: SNOWFLAKE
    66. props:
    67. worker-id: 123

     这里使用的分片算法是行表达式分片算法  INLINE,分片键即主键。如果分片键是非主键,取模的话,可以用MOD分片算法,增加属性值sharding-count

    分片算法参考链接:

    https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/builtin-algorithm/sharding/#%E8%87%AA%E5%8A%A8%E5%88%86%E7%89%87%E7%AE%97%E6%B3%95

    三、启动

    • 新建demo_ds_0、demo_ds_1数据库
    • 进入bin目录 ,cmd 执行start.bat 3316

    • 新启动一个cmd执行命令
    mysql -h 127.0.0.1 -P 3316 -uroot -pxrq123 -A
    show schemas;
    use sharding_db;
    CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id));
    insert into t_order(user_id,status) values(1,'ok'),(2,'fail'),(3,'hello world'),(4,'2022-04-21'),(5,'Hangzhou king of volume'),(6,'In learning sub - library sub - table');
    mysql> select * from t_order;
    +--------------------+---------+---------------------------------------+
    | order_id           | user_id | status                                |
    +--------------------+---------+---------------------------------------+
    | 742032272861409280 |       1 | ok                                    |
    | 742032272861409281 |       2 | fail                                  |
    | 742032272861409282 |       3 | hello world                           |
    | 742032272861409283 |       4 | 2022-04-21                            |
    | 742032272861409284 |       5 | Hangzhou king of volume               |
    | 742032272861409285 |       6 | In learning sub - library sub - table |
    +--------------------+---------+---------------------------------------+
    • 在两个数据库中分别可以看见t_order_0、t_order_1表,数据如下:

    参考链接: https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/builtin-algorithm/


    如果对大家有帮助的话,点赞支持哈,欢迎批评指正,相互交流!

  • 相关阅读:
    第318场周赛
    微信小程序python+uniapp+hbuiderx宠物美容用品商城领养寄养系统i843n
    十大排序算法(面试必备)
    递推算法(c++)
    java.sql.SQLException: Cannot set billDate: incompatible types.
    元宇宙iwemeta:《时代》杂志新封面,元宇宙将改变一切
    putty保存登录账号和密码
    【微软技术栈】C#.NET 正则表达式
    设计模式之模板方法模式C++实现
    【保姆级示例向】观察者模式
  • 原文地址:https://blog.csdn.net/xrq1995/article/details/126234870