{"id":15933,"date":"2026-04-26T13:52:09","date_gmt":"2026-04-26T05:52:09","guid":{"rendered":"https:\/\/top.duoku.icu\/libs\/15933.html"},"modified":"2026-04-26T13:52:09","modified_gmt":"2026-04-26T05:52:09","slug":"shardingjdbc-%e5%88%86%e5%ba%93%e5%88%86%e8%a1%a8%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.srclibs.com\/index.php\/2026\/04\/26\/shardingjdbc-%e5%88%86%e5%ba%93%e5%88%86%e8%a1%a8%e8%af%a6%e7%bb%86%e6%95%99%e7%a8%8b\/","title":{"rendered":"ShardingJDBC \u5206\u5e93\u5206\u8868\u8be6\u7ec6\u6559\u7a0b"},"content":{"rendered":"<div style=\"text-align:center;margin:30px 0;\"><img decoding=\"async\" src=\"https:\/\/top.duoku.icu\/wp-content\/uploads\/2026\/04\/image-94.png\" alt=\"ShardingJDBC \u5206\u5e93\u5206\u8868\u8be6\u7ec6\u6559\u7a0b\" style=\"max-width:100%;height:auto;border-radius:12px;box-shadow:0 4px 12px rgba(0,0,0,0.1);\"><\/div>\n<h1>ShardingJDBC \u5206\u5e93\u5206\u8868\u8be6\u7ec6\u6559\u7a0b<\/h1>\n<h2>ShardingJDBC \u6982\u8ff0<\/h2>\n<p>Sharding-JDBC \u662f\u4e00\u6b3e\u5f00\u6e90\u7684 Java \u751f\u6001\u4e0b\u7684\u5206\u5e03\u5f0f\u6570\u636e\u5e93\u89e3\u51b3\u65b9\u6848\uff0c\u7531\u5f53\u5f53\u7f51\u5f00\u6e90\uff0c\u73b0\u96b6\u5c5e\u4e8e Apache ShardingSphere \u9879\u76ee\u3002\u5b83\u4ee5 JDBC \u9a71\u52a8\u7684\u5f62\u5f0f\u63d0\u4f9b\uff0c\u5bf9\u5e94\u7528\u900f\u660e\uff0c\u65e0\u9700\u4fee\u6539\u4ee3\u7801\u5373\u53ef\u5b9e\u73b0\u6570\u636e\u5e93\u7684\u5206\u5e93\u5206\u8868\u3001\u8bfb\u5199\u5206\u79bb\u3001\u5206\u5e03\u5f0f\u4e8b\u52a1\u7b49\u80fd\u529b\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u9700\u8981\u5206\u5e93\u5206\u8868\uff1f<\/h3>\n<p><strong>\u5355\u5e93\u5355\u8868\u74f6\u9888<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5b58\u50a8\u74f6\u9888<\/strong>\uff1a\u5355\u8868\u6570\u636e\u91cf\u8d85\u8fc7 1000 \u4e07\uff0c\u67e5\u8be2\u6027\u80fd\u4e0b\u964d<\/li>\n<li><strong>\u6027\u80fd\u74f6\u9888<\/strong>\uff1a\u5355\u8868\u884c\u6570\u8d85\u8fc7 2000 \u4e07\uff0c\u7d22\u5f15\u6548\u7387\u964d\u4f4e<\/li>\n<li><strong>\u5e76\u53d1\u74f6\u9888<\/strong>\uff1a\u5355\u5e93\u8fde\u63a5\u6570\u9650\u5236\uff08\u901a\u5e38 1000-2000 \u8fde\u63a5\uff09<\/li>\n<li><strong>\u53ef\u7528\u6027\u74f6\u9888<\/strong>\uff1a\u5355\u70b9\u6545\u969c\u5f71\u54cd\u6574\u4e2a\u4e1a\u52a1<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5782\u76f4\u62c6\u5206<\/strong>\uff1a\u6309\u4e1a\u52a1\u6a21\u5757\u62c6\u5206\u4e0d\u540c\u6570\u636e\u5e93<\/li>\n<li><strong>\u6c34\u5e73\u62c6\u5206<\/strong>\uff1a\u6309\u6570\u636e\u91cf\u62c6\u5206\u5206\u5e93\u5206\u8868<\/li>\n<\/ul>\n<h3>Sharding-JDBC \u6838\u5fc3\u7279\u6027<\/h3>\n<table border=\"1\" cellpadding=\"5\">\n<tr>\n<th>\u7279\u6027<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<tr>\n<td><strong>\u5206\u5e93\u5206\u8868<\/strong><\/td>\n<td>\u652f\u6301\u6309\u591a\u79cd\u7b56\u7565\u5206\u7247<\/td>\n<\/tr>\n<tr>\n<td><strong>\u8bfb\u5199\u5206\u79bb<\/strong><\/td>\n<td>\u81ea\u52a8\u8def\u7531\u8bfb\/\u5199\u8bf7\u6c42<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5206\u5e03\u5f0f\u4e8b\u52a1<\/strong><\/td>\n<td>\u652f\u6301\u591a\u79cd\u4e8b\u52a1\u6a21\u5f0f<\/td>\n<\/tr>\n<tr>\n<td><strong>\u6570\u636e\u5e93\u5f02\u6784<\/strong><\/td>\n<td>\u652f\u6301 MySQL\u3001Oracle\u3001PostgreSQL \u7b49<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5206\u7247\u952e\u8def\u7531<\/strong><\/td>\n<td>\u667a\u80fd\u8def\u7531\uff0c\u51cf\u5c11\u8de8\u5e93\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td><strong>\u6570\u636e\u5e93\u805a\u5408<\/strong><\/td>\n<td>\u652f\u6301\u591a\u8868\u805a\u5408\u67e5\u8be2<\/td>\n<\/tr>\n<\/table>\n<h2>Sharding-JDBC \u6838\u5fc3\u6982\u5ff5<\/h2>\n<h3>1. \u5206\u7247\uff08Sharding\uff09<\/h3>\n<p>\u5206\u7247\u662f\u5c06\u6570\u636e\u6309\u89c4\u5219\u5206\u6563\u5230\u591a\u4e2a\u7269\u7406\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p><strong>\u5206\u7247\u7ef4\u5ea6<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5206\u5e93<\/strong>\uff1a\u5c06\u6570\u636e\u5206\u6563\u5230\u591a\u4e2a\u6570\u636e\u5e93\u5b9e\u4f8b<\/li>\n<li><strong>\u5206\u8868<\/strong>\uff1a\u5728\u5355\u4e2a\u6570\u636e\u5e93\u4e2d\u5206\u6563\u5230\u591a\u4e2a\u8868<\/li>\n<\/ul>\n<p><strong>\u5206\u7247\u952e\uff08Sharding Column\uff09<\/strong>\uff1a<\/p>\n<ul>\n<li>\u7528\u4e8e\u8ba1\u7b97\u5206\u7247\u89c4\u5219\u7684\u5173\u952e\u5b57\u6bb5<\/li>\n<li>\u5982\uff1auser_id\u3001order_id\u3001tenant_id<\/li>\n<li>\u5206\u7247\u952e\u5fc5\u987b\u5b58\u5728\u4e8e\u67e5\u8be2\u6761\u4ef6\u4e2d\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u5168\u8def\u7531<\/li>\n<\/ul>\n<h3>2. \u6570\u636e\u8282\u70b9<\/h3>\n<p>\u6570\u636e\u8282\u70b9\u662f\u5206\u7247\u7684\u57fa\u672c\u5355\u5143\uff0c\u7531\u6570\u636e\u6e90 + \u8868\u7ec4\u6210\u3002<\/p>\n<p>&#8220;`yaml<br \/>\n\u6570\u636e\u8282\u70b9\uff1ads0.t_order_0<br \/>\n         \u2193<br \/>\n     \u6570\u636e\u6e90 \u8868\u540d<\/p>\n<pre><code>\n<h3>3. \u5206\u7247\u7b56\u7565<\/h3>\n\nSharding-JDBC \u63d0\u4f9b\u591a\u79cd\u5206\u7247\u7b56\u7565\u3002\n\n<strong>\u5206\u7247\u7b97\u6cd5\u7c7b\u578b<\/strong>\uff1a\n<ul>\n<li><strong>\u7cbe\u786e\u5206\u7247\uff08Exact\uff09<\/strong>\uff1a\u4f7f\u7528 IN \u5173\u952e\u5b57<\/li>\n<li><strong>\u8303\u56f4\u5206\u7247\uff08Range\uff09<\/strong>\uff1a\u4f7f\u7528 BETWEEN\u3001>\u3001< \u7b49<\/li>\n<li><strong>\u590d\u5408\u5206\u7247\uff08Complex\uff09<\/strong>\uff1a\u591a\u5206\u7247\u952e\u7ec4\u5408<\/li>\n<\/ul>\n\n<strong>\u5206\u7247\u7b97\u6cd5\u6a21\u5f0f<\/strong>\uff1a\n<ul>\n<li><strong>\u6807\u51c6\u5206\u7247\uff08StandardShardingStrategy\uff09<\/strong>\uff1a\u5355\u4e2a\u5206\u7247\u952e<\/li>\n<li><strong>\u590d\u5408\u5206\u7247\uff08ComplexShardingStrategy\uff09<\/strong>\uff1a\u591a\u4e2a\u5206\u7247\u952e<\/li>\n<li><strong>\u901a\u7528\u5206\u7247\uff08InlineShardingStrategy\uff09<\/strong>\uff1a\u8868\u8fbe\u5f0f\u5206\u7247<\/li>\n<\/ul>\n\n<h3>4. \u6570\u636e\u6e90\u7ba1\u7406<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\ndataSources:<br \/>\n  ds0:<br \/>\n    dataSourceClassName: com.zaxxer.hikari.HikariDataSource<br \/>\n    driverClassName: com.mysql.cj.jdbc.Driver<br \/>\n    jdbcUrl: jdbc:mysql:\/\/localhost:3306\/sharding_db_0<br \/>\n    username: root<br \/>\n    password: root<\/p>\n<p>  ds1:<br \/>\n    dataSourceClassName: com.zaxxer.hikari.HikariDataSource<br \/>\n    driverClassName: com.mysql.cj.jdbc.Driver<br \/>\n    jdbcUrl: jdbc:mysql:\/\/localhost:3306\/sharding_db_1<br \/>\n    username: root<br \/>\n    password: root<\/p>\n<pre><code>\n<h2>Sharding-JDBC \u914d\u7f6e\u793a\u4f8b<\/h2>\n\n<h3>\u5b8c\u6574\u914d\u7f6e\u793a\u4f8b<\/h3>\n\n<\/code><\/pre>\n<p>yaml<\/p>\n<h1>application.yml<\/h1>\n<p>spring:<br \/>\n  shardingsphere:<br \/>\n    # \u6570\u636e\u6e90\u914d\u7f6e<br \/>\n    data-sources:<br \/>\n      ds:<br \/>\n        type: com.zaxxer.hikari.HikariDataSource<br \/>\n        configuration:<br \/>\n          driver: com.mysql.cj.jdbc.Driver<br \/>\n          jdbcUrl: jdbc:mysql:\/\/localhost:3306\/sharding_db<br \/>\n          username: root<br \/>\n          password: root<\/p>\n<p>    # \u89c4\u5219\u914d\u7f6e<br \/>\n    rules:<br \/>\n      sharding:<br \/>\n        # \u5206\u7247\u8868\u914d\u7f6e<br \/>\n        tables:<br \/>\n          t_order:<br \/>\n            actual-data-nodes: ds$->{0..1}.t_order$->{0..3}<br \/>\n            # \u5206\u5e93\u7b56\u7565<br \/>\n            database-strategy:<br \/>\n              standard:<br \/>\n                sharding-column: user_id<br \/>\n                sharding-algorithm-name: database-inline<br \/>\n            # \u5206\u8868\u7b56\u7565<br \/>\n            table-strategy:<br \/>\n              standard:<br \/>\n                sharding-column: order_id<br \/>\n                sharding-algorithm-name: table-inline<br \/>\n            # \u4e3b\u952e\u7b56\u7565\uff08\u81ea\u52a8\u589e ID\uff09<br \/>\n            key-generate-strategy:<br \/>\n              column: order_id<br \/>\n              key-generator-name: snowflake<\/p>\n<p>        # \u5206\u7247\u7b97\u6cd5\u914d\u7f6e<br \/>\n        sharding-algorithms:<br \/>\n          database-inline:<br \/>\n            type: INLINE<br \/>\n            props:<br \/>\n              algorithm-expression: ds$->{user_id % 2}<br \/>\n          table-inline:<br \/>\n            type: INLINE<br \/>\n            props:<br \/>\n              algorithm-expression: t_order$->{order_id % 4}<\/p>\n<p>        # \u5206\u5e03\u5f0f\u4e3b\u952e\u751f\u6210\u5668<br \/>\n        key-generators:<br \/>\n          snowflake:<br \/>\n            type: SNOWFLAKE<br \/>\n            props:<br \/>\n              worker-id: 1<\/p>\n<p>    # \u5176\u4ed6\u914d\u7f6e<br \/>\n    props:<br \/>\n      sql-show: true  # \u6253\u5370 SQL<br \/>\n      exception-check-enabled: true<\/p>\n<pre><code>\n<h3>\u4ee3\u7801\u914d\u7f6e\u793a\u4f8b\uff08Java\uff09<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n@Configuration<br \/>\npublic class ShardingConfig {<\/p>\n<p>    @Bean<br \/>\n    public DataSource dataSource() throws SQLException {<br \/>\n        \/\/ \u914d\u7f6e\u6570\u636e\u6e90<br \/>\n        Properties dsProps = new Properties();<br \/>\n        dsProps.put(&#8220;user&#8221;, &#8220;root&#8221;);<br \/>\n        dsProps.put(&#8220;password&#8221;, &#8220;root&#8221;);<br \/>\n        dsProps.put(&#8220;jdbcUrl&#8221;, &#8220;jdbc:mysql:\/\/localhost:3306\/sharding_db&#8221;);<br \/>\n        dsProps.put(&#8220;driver&#8221;, &#8220;com.mysql.cj.jdbc.Driver&#8221;);<\/p>\n<p>        Map<String, DataSource> dataSourceMap = new HashMap<>();<br \/>\n        dataSourceMap.put(&#8220;ds&#8221;, new HikariDataSource(dsProps));<\/p>\n<p>        \/\/ \u914d\u7f6e\u5206\u7247\u89c4\u5219<br \/>\n        ShardingRuleConfig shardingRuleConfig = new ShardingRuleConfig();<\/p>\n<p>        \/\/ \u5206\u7247\u7b97\u6cd5<br \/>\n        ShardingAlgorithmConfig databaseAlgorithm = new ShardingAlgorithmConfig();<br \/>\n        databaseAlgorithm.setType(&#8220;INLINE&#8221;);<br \/>\n        databaseAlgorithm.setProps(PropsValue.create(&#8220;algorithm-expression&#8221;, &#8220;ds$->{user_id % 2}&#8221;));<\/p>\n<p>        ShardingAlgorithmConfig tableAlgorithm = new ShardingAlgorithmConfig();<br \/>\n        tableAlgorithm.setType(&#8220;INLINE&#8221;);<br \/>\n        tableAlgorithm.setProps(PropsValue.create(&#8220;algorithm-expression&#8221;, &#8220;t_order$->{order_id % 4}&#8221;));<\/p>\n<p>        \/\/ \u5206\u7247\u7b56\u7565<br \/>\n        ShardingColumn shardingColumn = new ShardingColumn();<br \/>\n        shardingColumn.setShardingColumnName(&#8220;user_id&#8221;);<\/p>\n<p>        shardingRuleConfig.setDatabaseShardingStrategy(<br \/>\n            new StandardShardingStrategyConfig(shardingColumn, databaseAlgorithm)<br \/>\n        );<\/p>\n<p>        shardingColumn.setShardingColumnName(&#8220;order_id&#8221;);<br \/>\n        shardingRuleConfig.setTableShardingStrategy(<br \/>\n            new StandardShardingStrategyConfig(shardingColumn, tableAlgorithm)<br \/>\n        );<\/p>\n<p>        \/\/ \u8868\u914d\u7f6e<br \/>\n        ShardingTableConfig tableConfig = new ShardingTableConfig(&#8220;t_order&#8221;,<br \/>\n            &#8220;ds$->{0..1}.t_order$->{0..3}&#8221;);<br \/>\n        tableConfig.setDatabaseShardingStrategy(&#8220;database-inline&#8221;);<br \/>\n        tableConfig.setTableShardingStrategy(&#8220;table-inline&#8221;);<br \/>\n        tableConfig.setKeyGenerateStrategy(<br \/>\n            new KeyGenerateStrategyConfig(&#8220;order_id&#8221;, &#8220;snowflake&#8221;)<br \/>\n        );<\/p>\n<p>        shardingRuleConfig.getTables().add(tableConfig);<\/p>\n<p>        \/\/ \u6ce8\u518c\u5206\u7247\u89c4\u5219<br \/>\n        ShardingRuleBuilder shardingRuleBuilder = new ShardingRuleBuilder();<br \/>\n        shardingRuleBuilder.setDataSourceMap(dataSourceMap);<br \/>\n        shardingRuleBuilder.setShardingRule(shardingRuleConfig);<\/p>\n<p>        return shardingRuleBuilder.build();<br \/>\n    }<br \/>\n}<\/p>\n<pre><code>\n<h2>\u5206\u7247\u7b56\u7565\u8be6\u89e3<\/h2>\n\n<h3>\u7b56\u7565\u4e00\uff1a\u53d6\u6a21\u5206\u7247<\/h3>\n\n\u6700\u5e38\u7528\u7684\u5206\u7247\u7b56\u7565\uff0c\u6839\u636e\u5206\u7247\u952e\u53d6\u6a21\u8ba1\u7b97\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\ndatabase-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: user_id<br \/>\n    sharding-algorithm-name: database-mod<br \/>\nsharding-algorithms:<br \/>\n  database-mod:<br \/>\n    type: MOD<br \/>\n    props:<br \/>\n      sharding-count: 2  # 2 \u4e2a\u5e93<\/p>\n<pre><code>\n<strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u6570\u636e\u5747\u5300\u5206\u5e03\uff0c\u67e5\u8be2\u6761\u4ef6\u5305\u542b\u5206\u7247\u952e\n\n<strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a\n<ul>\n<li>\u2705 \u6570\u636e\u5206\u5e03\u5747\u5300<\/li>\n<li>\u2705 \u5b9e\u73b0\u7b80\u5355<\/li>\n<li>\u274c \u6269\u5bb9\u9700\u8981\u6570\u636e\u8fc1\u79fb<\/li>\n<\/ul>\n\n<h3>\u7b56\u7565\u4e8c\uff1a\u8303\u56f4\u5206\u7247<\/h3>\n\n\u6839\u636e\u8303\u56f4\u503c\u5206\u7247\uff0c\u9002\u7528\u4e8e\u65f6\u95f4\u8303\u56f4\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\ndatabase-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: create_time<br \/>\n    sharding-algorithm-name: database-range<br \/>\nsharding-algorithms:<br \/>\n  database-range:<br \/>\n    type: RANGE-HASH-MOD<br \/>\n    props:<br \/>\n      sharding-count: 4<br \/>\n      mod-base: 24  # \u65f6\u95f4\u7c92\u5ea6\uff1a\u5c0f\u65f6<\/p>\n<pre><code>\n<strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u6309\u65f6\u95f4\u8303\u56f4\u5206\u7247\uff08\u65e5\u5fd7\u3001\u8ba2\u5355\uff09\n\n<strong>\u4f18\u7f3a\u70b9<\/strong>\uff1a\n<ul>\n<li>\u2705 \u9002\u5408\u65f6\u95f4\u8303\u56f4\u67e5\u8be2<\/li>\n<li>\u274c \u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u503e\u659c<\/li>\n<\/ul>\n\n<h3>\u7b56\u7565\u4e09\uff1a\u590d\u5408\u5206\u7247<\/h3>\n\n\u4f7f\u7528\u591a\u4e2a\u5206\u7247\u952e\u8fdb\u884c\u5206\u7247\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\ntable-strategy:<br \/>\n  complex:<br \/>\n    sharding-columns: user_id,order_id<br \/>\n    sharding-algorithm-name: table-complex<br \/>\nsharding-algorithms:<br \/>\n  table-complex:<br \/>\n    type: COMPLEX<br \/>\n    props:<br \/>\n      strategy: MOD<br \/>\n      columns: user_id,order_id<\/p>\n<pre><code>\n<strong>\u9002\u7528\u573a\u666f<\/strong>\uff1a\u591a\u6761\u4ef6\u5206\u7247\uff0c\u9700\u8981\u66f4\u7ec6\u7c92\u5ea6\u63a7\u5236\n\n<h3>\u7b56\u7565\u56db\uff1a\u76d1\u542c\u5206\u7247<\/h3>\n\n\u7ed3\u5408\u591a\u79cd\u5206\u7247\u7b56\u7565\uff0c\u7075\u6d3b\u914d\u7f6e\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\ntable-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: order_id<br \/>\n    sharding-algorithm-name: table-hint<br \/>\nsharding-algorithms:<br \/>\n  table-hint:<br \/>\n    type: HINT<br \/>\n    props:<br \/>\n      sharding-algorithm-expression: t_order$->{order_id % 4}<\/p>\n<pre><code>\n<strong>\u7279\u70b9<\/strong>\uff1a\u901a\u8fc7 Hint \u624b\u52a8\u6307\u5b9a\u5206\u7247\uff0c\u9002\u5408\u590d\u6742\u67e5\u8be2\n\n<h2>\u8bfb\u5199\u5206\u79bb<\/h2>\n\n<h3>\u914d\u7f6e\u793a\u4f8b<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\nspring:<br \/>\n  shardingsphere:<br \/>\n    rules:<br \/>\n      sharding:<br \/>\n        data-sources:<br \/>\n          ds:<br \/>\n            readwrite-writing-data-source: ds-master<br \/>\n            readwrite:<br \/>\n              read-data-sources: ds-slave1,ds-slave2<\/p>\n<p>        readwrite:<br \/>\n          name-rule: readwrite-splitting-rs<br \/>\n          sharding-algorithm:<br \/>\n            type: LOAD_BALANCE  # \u8f6e\u8be2\u7b97\u6cd5<br \/>\n          props:<br \/>\n            write-data-source-name: ds-master<br \/>\n            read-data-source-names: ds-slave1,ds-slave2<\/p>\n<pre><code>\n<h3>\u4ee3\u7801\u4f7f\u7528<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n\/\/ \u5199\u64cd\u4f5c\uff08\u81ea\u52a8\u8def\u7531\u5230\u4e3b\u5e93\uff09<br \/>\n@PostMapping(&#8220;\/order&#8221;)<br \/>\npublic Order createOrder(@RequestBody Order order) {<br \/>\n    return orderMapper.insert(order);  \/\/ \u5199\u5165\u4e3b\u5e93<br \/>\n}<\/p>\n<p>\/\/ \u8bfb\u64cd\u4f5c\uff08\u81ea\u52a8\u8def\u7531\u5230\u4ece\u5e93\uff09<br \/>\n@GetMapping(&#8220;\/order\/{id}&#8221;)<br \/>\npublic Order getOrder(@PathVariable Long id) {<br \/>\n    return orderMapper.selectById(id);  \/\/ \u8bfb\u53d6\u4ece\u5e93<br \/>\n}<\/p>\n<p>\/\/ \u5f3a\u5236\u4e3b\u5e93\u67e5\u8be2<br \/>\n@Query(value = &#8220;SELECT * FROM t_order WHERE order_id = ?1 FOR UPDATE&#8221;,<br \/>\n       readOnly = false)<br \/>\nOptional<Order> getByIdWithLock(Long id);<\/p>\n<pre><code>\n<h3>\u8bfb\u5199\u5206\u79bb\u6700\u4f73\u5b9e\u8df5<\/h3>\n\n<\/code><\/pre>\n<p>yaml<br \/>\nprops:<br \/>\n  readwrite-splitting-enabled: true<br \/>\n  readwrite-splitting-query-with-catchable: true  # \u5224\u65ad\u662f\u5426\u9700\u8981\u8bfb\u4e3b\u5e93<\/p>\n<pre><code>\n<h2>\u5206\u5e03\u5f0f\u4e8b\u52a1<\/h2>\n\n<h3>1. \u672c\u5730\u6a21\u5f0f\uff08Local\uff09<\/h3>\n\n\u5355\u6570\u636e\u5e93\u4e8b\u52a1\uff0c\u6027\u80fd\u6700\u4f73\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\nprops:<br \/>\n  transaction:<br \/>\n    type: LOCAL<\/p>\n<pre><code>\n<h3>2. \u6700\u5927\u52aa\u529b\u4ea4\u4ed8\u6a21\u5f0f\uff08Max Effort\uff09<\/h3>\n\n\u6700\u7ec8\u4e00\u81f4\u6027\uff0c\u9002\u5408\u975e\u5173\u952e\u4e1a\u52a1\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\nprops:<br \/>\n  transaction:<br \/>\n    type: MAX<\/p>\n<pre><code>\n<h3>3. \u4e8b\u52a1\u6a21\u5f0f\uff08XA\uff09<\/h3>\n\n\u5f3a\u4e00\u81f4\u6027\uff0c\u6027\u80fd\u8f83\u5dee\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\nprops:<br \/>\n  transaction:<br \/>\n    type: XA<\/p>\n<pre><code>\n<h3>4. Seata \u6a21\u5f0f<\/h3>\n\n\u96c6\u6210 Seata \u5206\u5e03\u5f0f\u4e8b\u52a1\u6846\u67b6\u3002\n\n<\/code><\/pre>\n<p>yaml<br \/>\nprops:<br \/>\n  transaction:<br \/>\n    type: SEATA<\/p>\n<pre><code>\n<h3>\u4e8b\u52a1\u4f7f\u7528\u793a\u4f8b<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n@Transactional<br \/>\npublic void createOrderWithInventory(Order order) {<br \/>\n    \/\/ 1. \u63d2\u5165\u8ba2\u5355\uff08\u4e0d\u540c\u5e93\uff09<br \/>\n    orderMapper.insert(order);<\/p>\n<p>    \/\/ 2. \u6263\u51cf\u5e93\u5b58\uff08\u540c\u4e00\u5e93\uff09<br \/>\n    inventoryMapper.decrease(order.getProductId(), order.getQuantity());<\/p>\n<p>    \/\/ 3. \u8bb0\u5f55\u65e5\u5fd7\uff08\u540c\u5e93\uff09<br \/>\n    logMapper.insert(order.getId(), &#8220;ORDER_CREATED&#8221;);<\/p>\n<p>    \/\/ \u81ea\u52a8\u63d0\u4ea4\u6216\u56de\u6eda<br \/>\n}<\/p>\n<pre><code>\n<h2>\u5e38\u89c1\u573a\u666f\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h2>\n\n<h3>\u573a\u666f\u4e00\uff1a\u7535\u5546\u8ba2\u5355\u7cfb\u7edf<\/h3>\n\n<strong>\u5206\u5e93\u5206\u8868\u7b56\u7565<\/strong>\uff1a\n<\/code><\/pre>\n<p>yaml<\/p>\n<h1>\u5206\u5e93\uff1a\u6309\u7528\u6237 ID \u53d6\u6a21<\/h1>\n<p>database-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: user_id<br \/>\n    sharding-algorithm-name: user-database<\/p>\n<h1>\u5206\u8868\uff1a\u6309\u8ba2\u5355 ID \u53d6\u6a21<\/h1>\n<p>table-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: order_id<br \/>\n    sharding-algorithm-name: order-table<\/p>\n<p>\u5b9e\u9645\u6570\u636e\u8282\u70b9\uff1ads$->{0..3}.t_order$->{0..7}<\/p>\n<pre><code>\n<strong>\u4f18\u52bf<\/strong>\uff1a\n<ul>\n<li>\u7528\u6237\u76f8\u5173\u8ba2\u5355\u96c6\u4e2d\u5728\u540c\u4e00\u5e93<\/li>\n<li>\u8ba2\u5355\u6570\u636e\u5747\u5300\u5206\u5e03<\/li>\n<li>\u67e5\u8be2\u7528\u6237\u8ba2\u5355\u6027\u80fd\u9ad8<\/li>\n<\/ul>\n\n<h3>\u573a\u666f\u4e8c\uff1a\u65e5\u5fd7\u7cfb\u7edf<\/h3>\n\n<strong>\u5206\u5e93\u5206\u8868\u7b56\u7565<\/strong>\uff1a\n<\/code><\/pre>\n<p>yaml<\/p>\n<h1>\u6309\u65f6\u95f4\u8303\u56f4\u5206\u8868<\/h1>\n<p>table-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: create_time<br \/>\n    sharding-algorithm-name: time-table<\/p>\n<p>\u5b9e\u9645\u6570\u636e\u8282\u70b9\uff1ads0.log$->{0..11}  # \u6309\u6708\u5206\u8868<\/p>\n<pre><code>\n<strong>\u4f18\u52bf<\/strong>\uff1a\n<ul>\n<li>\u6309\u65f6\u95f4\u8303\u56f4\u67e5\u8be2\u9ad8\u6548<\/li>\n<li>\u5386\u53f2\u6570\u636e\u53ef\u5f52\u6863<\/li>\n<\/ul>\n\n<h3>\u573a\u666f\u4e09\uff1a\u591a\u79df\u6237\u7cfb\u7edf<\/h3>\n\n<strong>\u5206\u5e93\u5206\u8868\u7b56\u7565<\/strong>\uff1a\n<\/code><\/pre>\n<p>yaml<\/p>\n<h1>\u6309\u79df\u6237 ID \u5206\u5e93<\/h1>\n<p>database-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: tenant_id<br \/>\n    sharding-algorithm-name: tenant-database<\/p>\n<h1>\u8868\u5185\u5206\u7247<\/h1>\n<p>table-strategy:<br \/>\n  standard:<br \/>\n    sharding-column: user_id<br \/>\n    sharding-algorithm-name: user-table<\/p>\n<pre><code>\n<strong>\u4f18\u52bf<\/strong>\uff1a\n<ul>\n<li>\u79df\u6237\u6570\u636e\u9694\u79bb<\/li>\n<li>\u8d44\u6e90\u72ec\u7acb\u7ba1\u7406<\/li>\n<\/ul>\n\n<h2>\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h2>\n\n<h3>1. \u5206\u7247\u952e\u9009\u62e9<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n\/\/ \u2705 \u597d\u7684\u5206\u7247\u952e<br \/>\n\/\/ &#8211; \u67e5\u8be2\u6761\u4ef6\u4e2d\u5e38\u7528<br \/>\n\/\/ &#8211; \u6570\u636e\u5206\u5e03\u5747\u5300<br \/>\n\/\/ &#8211; \u503c\u57df\u8303\u56f4\u5927<\/p>\n<p>\/\/ \u274c \u907f\u514d\u7684\u5206\u7247\u952e<br \/>\n\/\/ &#8211; \u67e5\u8be2\u6761\u4ef6\u4e2d\u4e0d\u5e38\u7528<br \/>\n\/\/ &#8211; \u6570\u636e\u5206\u5e03\u4e0d\u5747<br \/>\n\/\/ &#8211; \u503c\u57df\u8303\u56f4\u5c0f\uff08\u5bfc\u81f4\u5206\u7247\u4e0d\u5747\uff09<\/p>\n<pre><code>\n<h3>2. \u8de8\u5e93\u67e5\u8be2\u5904\u7406<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n\/\/ \u907f\u514d\uff1a\u5168\u5e93\u626b\u63cf<br \/>\n\/\/ SELECT * FROM t_order WHERE create_time > ?<\/p>\n<p>\/\/ \u63a8\u8350\uff1a\u4f7f\u7528\u5206\u7247\u952e<br \/>\n\/\/ SELECT * FROM t_order WHERE user_id = ? AND create_time > ?<\/p>\n<p>\/\/ \u6216\uff1a\u4f7f\u7528\u5e7f\u64ad\u8868<br \/>\n\/\/ \u914d\u7f6e\u5e38\u91cf\u8868\u4e3a\u5e7f\u64ad\u8868<\/p>\n<pre><code>\n<h3>3. \u6269\u5bb9\u7b56\u7565<\/h3>\n\n<\/code><\/pre>\n<p>yaml<\/p>\n<h1>\u6570\u636e\u8fc1\u79fb\u65b9\u6848<\/h1>\n<ol>\n<li>\u65b0\u5e93\u521b\u5efa\uff08ds_new\uff09<\/li>\n<li>\u53cc\u5199\uff08\u540c\u65f6\u5199\u65e7\u5e93\u548c\u65b0\u5e93\uff09<\/li>\n<li>\u6570\u636e\u540c\u6b65\uff08\u6279\u91cf + \u589e\u91cf\uff09<\/li>\n<li>\u5207\u6362\u6d41\u91cf\uff08\u7070\u5ea6\u5207\u6362\uff09<\/li>\n<li>\u505c\u6b62\u65e7\u5e93<\/li>\n<\/ul>\n<pre><code>\n<h3>4. \u76d1\u63a7\u544a\u8b66<\/h3>\n\n<\/code><\/pre>\n<p>java<br \/>\n\/\/ \u5173\u952e\u6307\u6807\u76d1\u63a7<\/p>\n<ul>\n<li>\u67e5\u8be2\u54cd\u5e94\u65f6\u95f4<\/li>\n<li>\u5206\u7247\u8def\u7531\u5931\u8d25\u7387<\/li>\n<li>\u8de8\u5e93\u67e5\u8be2\u6bd4\u4f8b<\/li>\n<li>\u6570\u636e\u503e\u659c\u7a0b\u5ea6<\/li>\n<\/ul>\n<pre><code>\n<h3>5. SQL \u89c4\u8303<\/h3>\n\n<\/code><\/pre>\n<p>sql<br \/>\n&#8212; \u2705 \u63a8\u8350<br \/>\nSELECT * FROM t_order WHERE user_id = ?;<br \/>\nSELECT * FROM t_order WHERE user_id IN (?, ?, ?);<\/p>\n<p>&#8212; \u274c \u907f\u514d<br \/>\nSELECT * FROM t_order;  &#8212; \u5168\u8def\u7531<br \/>\nSELECT * FROM t_order WHERE order_time > ?;  &#8212; \u8de8\u5e93<\/p>\n<pre><code>\n<h2>\u5b9e\u9645\u6848\u4f8b\u5206\u6790<\/h2>\n\n<h3>\u6848\u4f8b\uff1a\u67d0\u7535\u5546\u5e73\u53f0\u8ba2\u5355\u7cfb\u7edf<\/h3>\n\n<strong>\u80cc\u666f<\/strong>\uff1a\n<ul>\n<li>\u8ba2\u5355\u91cf\uff1a\u65e5\u5747 100 \u4e07\uff0c\u5cf0\u503c 500 \u4e07<\/li>\n<li>\u5355\u8868\u884c\u6570\uff1a\u8d85\u8fc7 1 \u4ebf<\/li>\n<li>\u67e5\u8be2\u5ef6\u8fdf\uff1aP99 \u8d85\u8fc7 2 \u79d2<\/li>\n<\/ul>\n\n<strong>\u5206\u7247\u65b9\u6848<\/strong>\uff1a\n<ol>\n<li><strong>\u5206\u5e93<\/strong>\uff1a4 \u4e2a\u6570\u636e\u5e93\uff08\u6309 user_id \u53d6\u6a21\uff09<\/li>\n<li><strong>\u5206\u8868<\/strong>\uff1a\u6bcf\u4e2a\u5e93 8 \u5f20\u8868\uff08\u6309 order_id \u53d6\u6a21\uff09<\/li>\n<li><strong>\u8bfb\u5199\u5206\u79bb<\/strong>\uff1a1 \u4e3b 2 \u4ece<\/li>\n<li><strong>\u4e3b\u952e\u751f\u6210<\/strong>\uff1a\u96ea\u82b1\u7b97\u6cd5<\/li>\n<\/ul>\n\n<strong>\u6548\u679c<\/strong>\uff1a\n<ul>\n<li>\u67e5\u8be2\u5ef6\u8fdf\uff1aP99 \u964d\u81f3 200ms<\/li>\n<li>\u541e\u5410\u91cf\uff1a\u63d0\u5347 10 \u500d<\/li>\n<li>\u7a33\u5b9a\u6027\uff1a99.99%<\/li>\n<\/ul>\n\n<h2>\u603b\u7ed3<\/h2>\n\nSharding-JDBC \u4f5c\u4e3a\u6210\u719f\u7684\u5206\u5e93\u5206\u8868\u89e3\u51b3\u65b9\u6848\uff0c\u5177\u6709\uff1a\n\n<ul>\n<li><strong>\u96f6\u4fb5\u5165<\/strong>\uff1a\u65e0\u9700\u4fee\u6539\u4e1a\u52a1\u4ee3\u7801<\/li>\n<li><strong>\u9ad8\u6027\u80fd<\/strong>\uff1a\u667a\u80fd\u8def\u7531\uff0c\u51cf\u5c11\u8de8\u5e93\u67e5\u8be2<\/li>\n<li><strong>\u6613\u6269\u5c55<\/strong>\uff1a\u652f\u6301\u52a8\u6001\u6269\u5bb9<\/li>\n<li><strong>\u529f\u80fd\u4e30\u5bcc<\/strong>\uff1a\u8bfb\u5199\u5206\u79bb\u3001\u5206\u5e03\u5f0f\u4e8b\u52a1\u3001\u6570\u636e\u5e93\u805a\u5408<\/li>\n<\/ul>\n\n<strong>\u6838\u5fc3\u8981\u70b9<\/strong>\uff1a\n<ol>\n<li>\u5408\u7406\u9009\u62e9\u5206\u7247\u952e<\/li>\n<li>\u8bbe\u8ba1\u5408\u9002\u7684\u5206\u7247\u7b56\u7565<\/li>\n<li>\u907f\u514d\u8de8\u5e93\u67e5\u8be2<\/li>\n<li>\u505a\u597d\u76d1\u63a7\u544a\u8b66<\/li>\n<li>\u5236\u5b9a\u6269\u5bb9\u65b9\u6848<\/li>\n<\/ul>\n\n\u901a\u8fc7\u5408\u7406\u5e94\u7528 Sharding-JDBC\uff0c\u53ef\u4ee5\u6709\u6548\u89e3\u51b3\u5355\u5e93\u5355\u8868\u7684\u6027\u80fd\u74f6\u9888\uff0c\u652f\u6491\u6d77\u91cf\u6570\u636e\u548c\u9ad8\u5e76\u53d1\u573a\u666f\u3002\n\n---\n\n<strong>Maven \u4f9d\u8d56<\/strong>\uff1a\n<\/code><\/pre>\n<p>xml<br \/>\n<dependency><br \/>\n    <groupId>org.apache.shardingsphere<\/groupId><br \/>\n    <artifactId>shardingsphere-jdbc-core<\/artifactId><br \/>\n    <version>5.3.2<\/version><br \/>\n<\/dependency><br \/>\n&#8220;`<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ShardingJDBC \u5206\u5e93\u5206\u8868\u8be6\u7ec6\u6559\u7a0b ShardingJDBC \u6982\u8ff0 Sharding-JDBC \u662f\u4e00\u6b3e&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-15933","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/comments?post=15933"}],"version-history":[{"count":0,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/posts\/15933\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/media?parent=15933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/categories?post=15933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.srclibs.com\/index.php\/wp-json\/wp\/v2\/tags?post=15933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}