{"id":192012,"date":"2024-11-12T17:42:33","date_gmt":"2024-11-12T09:42:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/192012\/"},"modified":"2024-11-12T17:42:34","modified_gmt":"2024-11-12T09:42:34","slug":"%e8%a9%b3%e8%a7%a3%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%86%e7%89%87%ef%bc%8c%e5%a4%a7%e5%b9%85%e6%8f%90%e5%8d%87spring-boot%e6%9f%a5%e8%a9%a2mysql%e6%80%a7%e8%83%bd","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/192012\/","title":{"rendered":"\u8a73\u89e3\u6578\u64da\u5eab\u5206\u7247\uff0c\u5927\u5e45\u63d0\u5347Spring Boot\u67e5\u8a62MySQL\u6027\u80fd"},"content":{"rendered":"<h1 id=\"%e8%a9%b3%e8%a7%a3%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%86%e7%89%87%ef%bc%8c%e5%a4%a7%e5%b9%85%e6%8f%90%e5%8d%87spring-boot%e6%9f%a5%e8%a9%a2mysql%e6%80%a7%e8%83%bd-PEBVNNOGgr\">\u8a73\u89e3\u6578\u64da\u5eab\u5206\u7247\uff0c\u5927\u5e45\u63d0\u5347Spring Boot\u67e5\u8a62MySQL\u6027\u80fd<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0c\u61c9\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u5c0d\u65bc\u7528\u6236\u9ad4\u9a57\u81f3\u95dc\u91cd\u8981\u3002\u5c24\u5176\u662f\u5c0d\u65bc\u4f7f\u7528Spring Boot\u6846\u67b6\u7684Java\u61c9\u7528\u7a0b\u5e8f\uff0c\u6578\u64da\u5eab\u67e5\u8a62\u7684\u6548\u7387\u76f4\u63a5\u5f71\u97ff\u5230\u6574\u9ad4\u6027\u80fd\u3002\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u55ae\u4e00\u6578\u64da\u5eab\u7684\u6027\u80fd\u74f6\u9838\u9010\u6f38\u986f\u73fe\uff0c\u9019\u6642\u5019\u6578\u64da\u5eab\u5206\u7247\u6280\u8853\u4fbf\u6210\u70ba\u4e86\u4e00\u7a2e\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%86%e7%89%87%ef%bc%9f-PEBVNNOGgr\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\u5206\u7247\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u5206\u7247\u662f\u5c07\u6578\u64da\u5eab\u4e2d\u7684\u6578\u64da\u5206\u6563\u5230\u591a\u500b\u7269\u7406\u6578\u64da\u5eab\u4e2d\u7684\u6280\u8853\u3002\u9019\u6a23\u505a\u7684\u76ee\u7684\u662f\u70ba\u4e86\u63d0\u9ad8\u67e5\u8a62\u6027\u80fd\u548c\u64f4\u5c55\u6027\u3002\u5206\u7247\u53ef\u4ee5\u6839\u64da\u4e0d\u540c\u7684\u7b56\u7565\u9032\u884c\uff0c\u4f8b\u5982\u57fa\u65bc\u7bc4\u570d\u3001\u54c8\u5e0c\u6216\u5217\u8868\u7b49\u3002\u6bcf\u500b\u5206\u7247\u90fd\u53ef\u4ee5\u7368\u7acb\u8655\u7406\u67e5\u8a62\uff0c\u5f9e\u800c\u6e1b\u5c11\u55ae\u4e00\u6578\u64da\u5eab\u7684\u8ca0\u64d4\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%86%e7%89%87%ef%bc%9f-PEBVNNOGgr\">\u70ba\u4ec0\u9ebc\u9078\u64c7\u6578\u64da\u5eab\u5206\u7247\uff1f<\/h2>\n<ul>\n<li><strong>\u6027\u80fd\u63d0\u5347\uff1a<\/strong>\u901a\u904e\u5c07\u67e5\u8a62\u5206\u6563\u5230\u591a\u500b\u6578\u64da\u5eab\uff0c\u67e5\u8a62\u7684\u97ff\u61c9\u6642\u9593\u53ef\u4ee5\u986f\u8457\u964d\u4f4e\u3002<\/li>\n<li><strong>\u64f4\u5c55\u6027\uff1a<\/strong>\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u9577\uff0c\u53ef\u4ee5\u8f15\u9b06\u5730\u6dfb\u52a0\u65b0\u7684\u5206\u7247\u4f86\u64f4\u5c55\u7cfb\u7d71\u3002<\/li>\n<li><strong>\u6545\u969c\u9694\u96e2\uff1a<\/strong>\u5982\u679c\u67d0\u500b\u5206\u7247\u51fa\u73fe\u6545\u969c\uff0c\u5176\u4ed6\u5206\u7247\u4ecd\u7136\u53ef\u4ee5\u6b63\u5e38\u904b\u884c\uff0c\u5f9e\u800c\u63d0\u9ad8\u7cfb\u7d71\u7684\u7a69\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8spring-boot%e4%b8%ad%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e5%88%86%e7%89%87%ef%bc%9f-PEBVNNOGgr\">\u5982\u4f55\u5728Spring Boot\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u5206\u7247\uff1f<\/h2>\n<p>\u5728Spring Boot\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u5206\u7247\u9700\u8981\u5e7e\u500b\u6b65\u9a5f\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u5be6\u73fe\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"1-%e8%a8%ad%e8%a8%88%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9e%b6%e6%a7%8b-PEBVNNOGgr\">1. \u8a2d\u8a08\u6578\u64da\u5eab\u67b6\u69cb<\/h3>\n<p>\u9996\u5148\uff0c\u9700\u8981\u6839\u64da\u696d\u52d9\u9700\u6c42\u8a2d\u8a08\u6578\u64da\u5eab\u7684\u5206\u7247\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u7528\u6236\u8868\uff0c\u6839\u64da\u7528\u6236ID\u9032\u884c\u54c8\u5e0c\u5206\u7247\u3002<\/p>\n<h3 id=\"2-%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e6%ba%90-PEBVNNOGgr\">2. \u914d\u7f6e\u6578\u64da\u6e90<\/h3>\n<p>\u5728Spring Boot\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u500b\u6578\u64da\u6e90\u4f86\u5be6\u73fe\u5206\u7247\u3002\u4ee5\u4e0b\u662f\u914d\u7f6e\u6578\u64da\u6e90\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nspring:\n  datasource:\n    shard1:\n      url: jdbc:mysql:\/\/localhost:3306\/shard1\n      username: root\n      password: password\n    shard2:\n      url: jdbc:mysql:\/\/localhost:3306\/shard2\n      username: root\n      password: password\n<\/code><\/pre>\n<h3 id=\"3-%e5%af%a6%e7%8f%be%e5%88%86%e7%89%87%e9%82%8f%e8%bc%af-PEBVNNOGgr\">3. \u5be6\u73fe\u5206\u7247\u908f\u8f2f<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u9700\u8981\u6839\u64da\u7528\u6236ID\u4f86\u6c7a\u5b9a\u67e5\u8a62\u7684\u6578\u64da\u6e90\u3002\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u5206\u7247\u8def\u7531\u5668\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\uff1a<\/p>\n<pre><code>\nimport org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;\n\npublic class ShardRoutingDataSource extends AbstractRoutingDataSource {\n    @Override\n    protected Object determineCurrentLookupKey() {\n        \/\/ \u6839\u64da\u7528\u6236ID\u6c7a\u5b9a\u4f7f\u7528\u54ea\u500b\u6578\u64da\u6e90\n        return UserContext.getUserId() % 2 == 0 ? \"shard1\" : \"shard2\";\n    }\n}\n<\/code><\/pre>\n<h3 id=\"4-%e4%bd%bf%e7%94%a8%e5%88%86%e7%89%87%e6%95%b8%e6%93%9a%e6%ba%90%e9%80%b2%e8%a1%8c%e6%9f%a5%e8%a9%a2-PEBVNNOGgr\">4. \u4f7f\u7528\u5206\u7247\u6578\u64da\u6e90\u9032\u884c\u67e5\u8a62<\/h3>\n<p>\u6700\u5f8c\uff0c\u5728\u9032\u884c\u6578\u64da\u67e5\u8a62\u6642\uff0c\u4f7f\u7528\u914d\u7f6e\u597d\u7684\u5206\u7247\u6578\u64da\u6e90\u5373\u53ef\uff1a<\/p>\n<pre><code>\n@Autowired\nprivate JdbcTemplate jdbcTemplate;\n\npublic User getUserById(Long userId) {\n    String sql = \"SELECT * FROM users WHERE id = ?\";\n    return jdbcTemplate.queryForObject(sql, new Object[]{userId}, new UserRowMapper());\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-PEBVNNOGgr\">\u7d50\u8ad6<\/h2>\n<p>\u6578\u64da\u5eab\u5206\u7247\u662f\u4e00\u7a2e\u6709\u6548\u7684\u6280\u8853\uff0c\u53ef\u4ee5\u986f\u8457\u63d0\u5347Spring Boot\u61c9\u7528\u7a0b\u5e8f\u67e5\u8a62MySQL\u7684\u6027\u80fd\u3002\u901a\u904e\u5408\u7406\u7684\u5206\u7247\u7b56\u7565\u548c\u914d\u7f6e\uff0c\u53ef\u4ee5\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u5b58\u53d6\uff0c\u5f9e\u800c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002\u96a8\u8457\u696d\u52d9\u7684\u589e\u9577\uff0c\u6578\u64da\u5eab\u7684\u64f4\u5c55\u6027\u548c\u7a69\u5b9a\u6027\u4e5f\u5c07\u5f97\u5230\u4fdd\u969c\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-PEBVNNOGgr\">\u7e3d\u7d50<\/h2>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u8655\u7406\u74b0\u5883\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684\u6578\u64da\u5eab\u67b6\u69cb\u81f3\u95dc\u91cd\u8981\u3002\u6578\u64da\u5eab\u5206\u7247\u4e0d\u50c5\u80fd\u63d0\u5347\u6027\u80fd\uff0c\u9084\u80fd\u63d0\u9ad8\u7cfb\u7d71\u7684\u53ef\u64f4\u5c55\u6027\u548c\u7a69\u5b9a\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u9700\u6c42\uff0c\u5c07\u662f\u660e\u667a\u7684\u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8a73\u89e3\u6578\u64da\u5eab\u5206\u7247\u6280\u8853\uff0c\u63d0\u5347Spring Boot\u67e5\u8a62MySQL\u6027\u80fd\uff0c\u512a\u5316\u6578\u64da\u8655\u7406\u6548\u7387\uff0c\u5be6\u73fe\u9ad8\u6548\u80fd\u61c9\u7528\u958b\u767c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-192012","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=192012"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192012\/revisions"}],"predecessor-version":[{"id":192013,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/192012\/revisions\/192013"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=192012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=192012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=192012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}