{"id":135091,"date":"2024-10-31T07:41:57","date_gmt":"2024-10-30T23:41:57","guid":{"rendered":"https:\/\/server.hk\/cnblog\/135091\/"},"modified":"2024-10-31T07:41:58","modified_gmt":"2024-10-30T23:41:58","slug":"spring%e6%95%b8%e6%93%9a%e5%ba%ab%e8%b7%af%e7%94%b1%ef%bc%9a%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%e5%84%aa%e5%8c%96%e6%96%b9%e6%a1%88%ef%bc%88spring%e6%95%b8%e6%93%9a%e5%ba%ab%e8%b7%af%e7%94%b1","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/135091\/","title":{"rendered":"Spring\u6578\u64da\u5eab\u8def\u7531\uff1a\u8ca0\u8f09\u5747\u8861\u512a\u5316\u65b9\u6848\uff08spring\u6578\u64da\u5eab\u8def\u7531\uff09"},"content":{"rendered":"<h1 id=\"spring%e6%95%b8%e6%93%9a%e5%ba%ab%e8%b7%af%e7%94%b1%ef%bc%9a%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%e5%84%aa%e5%8c%96%e6%96%b9%e6%a1%88-pjfCXDuYvm\">Spring\u6578\u64da\u5eab\u8def\u7531\uff1a\u8ca0\u8f09\u5747\u8861\u512a\u5316\u65b9\u6848<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u96a8\u8457\u7528\u6236\u6578\u91cf\u7684\u589e\u52a0\uff0c\u55ae\u4e00\u6578\u64da\u5eab\u7684\u8ca0\u8f09\u53ef\u80fd\u6703\u6210\u70ba\u74f6\u9838\uff0c\u9019\u6642\u5019\u6578\u64da\u5eab\u8def\u7531\u6280\u8853\u4fbf\u6210\u70ba\u4e86\u4e00\u500b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u63a2\u8a0eSpring\u6846\u67b6\u4e2d\u7684\u6578\u64da\u5eab\u8def\u7531\u6280\u8853\uff0c\u4e26\u4ecb\u7d39\u5982\u4f55\u901a\u904e\u8ca0\u8f09\u5747\u8861\u4f86\u512a\u5316\u6578\u64da\u5eab\u6027\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%e8%b7%af%e7%94%b1%ef%bc%9f-pjfCXDuYvm\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\u8def\u7531\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u8def\u7531\u662f\u6307\u6839\u64da\u7279\u5b9a\u7684\u898f\u5247\u5c07\u6578\u64da\u8acb\u6c42\u5206\u767c\u5230\u4e0d\u540c\u7684\u6578\u64da\u5eab\u5be6\u4f8b\u4e2d\u3002\u9019\u7a2e\u6280\u8853\u53ef\u4ee5\u6709\u6548\u5730\u5206\u6563\u8ca0\u8f09\uff0c\u6e1b\u5c11\u55ae\u4e00\u6578\u64da\u5eab\u7684\u58d3\u529b\uff0c\u5f9e\u800c\u63d0\u9ad8\u6574\u9ad4\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u53ef\u7528\u6027\u3002\u5728Spring\u6846\u67b6\u4e2d\uff0c\u6578\u64da\u5eab\u8def\u7531\u901a\u5e38\u8207Spring\u7684\u4e8b\u52d9\u7ba1\u7406\u548c\u6578\u64da\u6e90\u914d\u7f6e\u76f8\u7d50\u5408\uff0c\u4ee5\u5be6\u73fe\u9748\u6d3b\u7684\u6578\u64da\u5eab\u8a2a\u554f\u3002<\/p>\n<h2 id=\"spring%e4%b8%ad%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e8%b7%af%e7%94%b1%e5%af%a6%e7%8f%be-pjfCXDuYvm\">Spring\u4e2d\u7684\u6578\u64da\u5eab\u8def\u7531\u5be6\u73fe<\/h2>\n<p>\u5728Spring\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u8def\u7531\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684\u8def\u7531\u6578\u64da\u6e90\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u5728Spring\u4e2d\u914d\u7f6e\u8def\u7531\u6578\u64da\u6e90\uff1a<\/p>\n<pre><code>\nimport org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;\n\npublic class DynamicDataSource extends AbstractRoutingDataSource {\n    @Override\n    protected Object determineCurrentLookupKey() {\n        \/\/ \u6839\u64da\u7576\u524d\u4e0a\u4e0b\u6587\u6c7a\u5b9a\u4f7f\u7528\u54ea\u500b\u6578\u64da\u6e90\n        return DataSourceContextHolder.getDataSourceType();\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba <code>DynamicDataSource<\/code> \u7684\u985e\uff0c\u7e7c\u627f\u81ea <code>AbstractRoutingDataSource<\/code>\u3002\u901a\u904e\u91cd\u5beb <code>determineCurrentLookupKey<\/code> \u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u7576\u524d\u7684\u4e0a\u4e0b\u6587\u4f86\u6c7a\u5b9a\u4f7f\u7528\u54ea\u500b\u6578\u64da\u6e90\u3002<\/p>\n<h2 id=\"%e8%b2%a0%e8%bc%89%e5%9d%87%e8%a1%a1%e7%ad%96%e7%95%a5-pjfCXDuYvm\">\u8ca0\u8f09\u5747\u8861\u7b56\u7565<\/h2>\n<p>\u5728\u5be6\u73fe\u6578\u64da\u5eab\u8def\u7531\u6642\uff0c\u9078\u64c7\u5408\u9069\u7684\u8ca0\u8f09\u5747\u8861\u7b56\u7565\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u8ca0\u8f09\u5747\u8861\u7b56\u7565\uff1a<\/p>\n<ul>\n<li><strong>\u8f2a\u8a62\uff08Round Robin\uff09<\/strong>\uff1a\u5c07\u8acb\u6c42\u4f9d\u6b21\u5206\u914d\u7d66\u6bcf\u500b\u6578\u64da\u5eab\u5be6\u4f8b\uff0c\u9069\u5408\u8acb\u6c42\u91cf\u76f8\u5c0d\u5747\u8861\u7684\u60c5\u6cc1\u3002<\/li>\n<li><strong>\u6700\u5c11\u9023\u63a5\uff08Least Connections\uff09<\/strong>\uff1a\u5c07\u8acb\u6c42\u5206\u914d\u7d66\u7576\u524d\u9023\u63a5\u6578\u6700\u5c11\u7684\u6578\u64da\u5eab\u5be6\u4f8b\uff0c\u9069\u5408\u9023\u63a5\u6578\u91cf\u4e0d\u5747\u7684\u60c5\u6cc1\u3002<\/li>\n<li><strong>\u52a0\u6b0a\u8f2a\u8a62\uff08Weighted Round Robin\uff09<\/strong>\uff1a\u6839\u64da\u6bcf\u500b\u6578\u64da\u5eab\u7684\u6027\u80fd\u6307\u6a19\uff08\u5982\u8655\u7406\u80fd\u529b\uff09\u4f86\u5206\u914d\u8acb\u6c42\uff0c\u9069\u5408\u6027\u80fd\u5dee\u7570\u8f03\u5927\u7684\u60c5\u6cc1\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8%e6%a1%88%e4%be%8b-pjfCXDuYvm\">\u5be6\u969b\u61c9\u7528\u6848\u4f8b<\/h2>\n<p>\u5047\u8a2d\u4e00\u500b\u96fb\u5b50\u5546\u52d9\u5e73\u53f0\u9700\u8981\u8655\u7406\u5927\u91cf\u7684\u7528\u6236\u8acb\u6c42\uff0c\u9019\u6642\u5019\u53ef\u4ee5\u4f7f\u7528Spring\u7684\u6578\u64da\u5eab\u8def\u7531\u6280\u8853\u4f86\u5be6\u73fe\u8ca0\u8f09\u5747\u8861\u3002\u901a\u904e\u5c07\u7528\u6236\u7684\u8acb\u6c42\u5206\u767c\u5230\u591a\u500b\u6578\u64da\u5eab\u5be6\u4f8b\uff0c\u7cfb\u7d71\u53ef\u4ee5\u5728\u9ad8\u5cf0\u671f\u4fdd\u6301\u7a69\u5b9a\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u7576\u7528\u6236\u9032\u884c\u67e5\u8a62\u6642\uff0c\u7cfb\u7d71\u53ef\u4ee5\u6839\u64da\u7576\u524d\u7684\u8ca0\u8f09\u60c5\u6cc1\uff0c\u9078\u64c7\u4e00\u500b\u8ca0\u8f09\u8f03\u4f4e\u7684\u6578\u64da\u5eab\u5be6\u4f8b\u4f86\u8655\u7406\u8acb\u6c42\u3002\u9019\u6a23\u4e0d\u50c5\u63d0\u9ad8\u4e86\u67e5\u8a62\u6548\u7387\uff0c\u9084\u80fd\u6e1b\u5c11\u55ae\u4e00\u6578\u64da\u5eab\u7684\u58d3\u529b\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-pjfCXDuYvm\">\u7d50\u8ad6<\/h2>\n<p>Spring\u6578\u64da\u5eab\u8def\u7531\u6280\u8853\u70ba\u61c9\u7528\u7a0b\u5f0f\u63d0\u4f9b\u4e86\u4e00\u7a2e\u6709\u6548\u7684\u8ca0\u8f09\u5747\u8861\u65b9\u6848\u3002\u901a\u904e\u5408\u7406\u914d\u7f6e\u8def\u7531\u6578\u64da\u6e90\u548c\u9078\u64c7\u5408\u9069\u7684\u8ca0\u8f09\u5747\u8861\u7b56\u7565\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u986f\u8457\u63d0\u9ad8\u7cfb\u7d71\u7684\u6027\u80fd\u548c\u53ef\u64f4\u5c55\u6027\u3002\u5728\u7576\u524d\u7684\u96f2\u8a08\u7b97\u74b0\u5883\u4e2d\uff0c\u9019\u7a2e\u6280\u8853\u5c24\u70ba\u91cd\u8981\uff0c\u56e0\u70ba\u5b83\u80fd\u5920\u5e6b\u52a9\u4f01\u696d\u66f4\u597d\u5730\u61c9\u5c0d\u4e0d\u65b7\u589e\u9577\u7684\u6578\u64da\u9700\u6c42\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\u7684\u4fe1\u606f\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Spring\u6578\u64da\u5eab\u8def\u7531\u7684\u8ca0\u8f09\u5747\u8861\u512a\u5316\u65b9\u6848\uff0c\u63d0\u5347\u6578\u64da\u5eab\u6027\u80fd\u8207\u53ef\u7528\u6027\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\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-135091","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135091","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=135091"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135091\/revisions"}],"predecessor-version":[{"id":135092,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/135091\/revisions\/135092"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=135091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=135091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=135091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}