{"id":260,"date":"2024-07-22T01:09:31","date_gmt":"2024-07-21T17:09:31","guid":{"rendered":"http:\/\/8.138.155.216\/?p=260"},"modified":"2024-07-22T01:09:31","modified_gmt":"2024-07-21T17:09:31","slug":"spring-boot%e4%bd%bf%e7%94%a8%e7%ba%bf%e7%a8%8b%e6%b1%a0%e4%b8%8d%e5%90%8c%e4%bb%bb%e5%8a%a1%e7%b1%bb%e5%9e%8b%e7%9a%84%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/blog.iwonder.cc\/index.php\/2024\/07\/22\/spring-boot%e4%bd%bf%e7%94%a8%e7%ba%bf%e7%a8%8b%e6%b1%a0%e4%b8%8d%e5%90%8c%e4%bb%bb%e5%8a%a1%e7%b1%bb%e5%9e%8b%e7%9a%84%e8%ae%be%e7%bd%ae\/","title":{"rendered":"Spring Boot\u4f7f\u7528\u7ebf\u7a0b\u6c60\u4e0d\u540c\u4efb\u52a1\u7c7b\u578b\u7684\u8bbe\u7f6e"},"content":{"rendered":"<p>\u5728 Spring Boot \u5e94\u7528\u4e2d\uff0c\u5408\u7406\u914d\u7f6e\u7ebf\u7a0b\u6c60\u5bf9\u4e8e\u4f18\u5316\u6027\u80fd\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406 CPU \u5bc6\u96c6\u578b\u548c IO \u5bc6\u96c6\u578b\u4efb\u52a1\u65f6\u3002\u8fd9\u4e24\u79cd\u7c7b\u578b\u7684\u4efb\u52a1\u5bf9\u7ebf\u7a0b\u6c60\u7684\u914d\u7f6e\u9700\u6c42\u6709\u663e\u8457\u5dee\u5f02\uff1a<\/p>\n<h2>CPU \u5bc6\u96c6\u578b\u4efb\u52a1<\/h2>\n<p>CPU \u5bc6\u96c6\u578b\u4efb\u52a1\u4e3b\u8981\u6d89\u53ca\u5927\u91cf\u8ba1\u7b97\uff0c\u5f88\u5c11\u6216\u6ca1\u6709 I\/O \u64cd\u4f5c\u3002\u8fd9\u7c7b\u4efb\u52a1\u4f1a\u6301\u7eed\u5360\u7528 CPU \u8d44\u6e90\uff0c\u56e0\u6b64\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\u5e94\u8be5\u4e0e\u7cfb\u7edf\u53ef\u7528\u7684\u5904\u7406\u5668\u6838\u5fc3\u6570\u76f8\u5339\u914d\uff0c\u4ee5\u907f\u514d\u8fc7\u591a\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u548c\u8c03\u5ea6\u5f00\u9500\u3002\u901a\u5e38\uff0c\u7ebf\u7a0b\u6c60\u5927\u5c0f\u8bbe\u7f6e\u4e3a\u7cfb\u7edf\u7684\u6838\u5fc3\u6570\u6216\u8005\u7a0d\u5fae\u5927\u4e00\u70b9\uff08\u4f8b\u5982\u6838\u5fc3\u6570+1\uff09\uff0c\u8fd9\u6837\u53ef\u4ee5\u5145\u5206\u5229\u7528\u6240\u6709\u6838\u5fc3\uff0c\u540c\u65f6\u907f\u514d\u8fc7\u5ea6\u7ade\u4e89\u3002<\/p>\n<h2>IO \u5bc6\u96c6\u578b\u4efb\u52a1<\/h2>\n<p>IO \u5bc6\u96c6\u578b\u4efb\u52a1\u5219\u6d89\u53ca\u5230\u5927\u91cf\u7684\u8f93\u5165\u8f93\u51fa\u64cd\u4f5c\uff0c\u5982\u8bfb\u5199\u6587\u4ef6\u3001\u7f51\u7edc\u901a\u4fe1\u7b49\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u4f1a\u5bfc\u81f4\u7ebf\u7a0b\u963b\u585e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\u5e94\u8be5\u8bbe\u7f6e\u5f97\u66f4\u5927\u4e00\u4e9b\uff0c\u4ee5\u4fbf\u5728\u67d0\u4e9b\u7ebf\u7a0b\u56e0\u7b49\u5f85 I\/O \u64cd\u4f5c\u800c\u963b\u585e\u65f6\uff0c\u5176\u4ed6\u7ebf\u7a0b\u4ecd\u80fd\u7ee7\u7eed\u5904\u7406\u4efb\u52a1\u3002\u901a\u5e38\u5efa\u8bae\u7684\u7ebf\u7a0b\u6c60\u5927\u5c0f\u662f\u5904\u7406\u5668\u6838\u5fc3\u6570\u7684 2 \u5230 4 \u500d\uff0c\u751a\u81f3\u66f4\u9ad8\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u6027\u4ee5\u53ca I\/O \u64cd\u4f5c\u7684\u9891\u7387\u548c\u6301\u7eed\u65f6\u95f4\u3002<\/p>\n<h2>Spring Boot \u4e2d\u7684\u914d\u7f6e<\/h2>\n<p>\u5728 Spring Boot \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 ThreadPoolTaskExecutor \u6216 ScheduledThreadPoolExecutor \u6765\u914d\u7f6e\u7ebf\u7a0b\u6c60\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5173\u952e\u5c5e\u6027\uff1a<\/p>\n<ul>\n<li>corePoolSize: \u7ebf\u7a0b\u6c60\u7684\u57fa\u672c\u5927\u5c0f\u3002<\/li>\n<li>maxPoolSize: \u7ebf\u7a0b\u6c60\u5141\u8bb8\u7684\u6700\u5927\u5927\u5c0f\u3002<\/li>\n<li>keepAliveTime: \u5f53\u7ebf\u7a0b\u7a7a\u95f2\u65f6\uff0c\u7ebf\u7a0b\u6c60\u4fdd\u6301\u7ebf\u7a0b\u5b58\u6d3b\u7684\u65f6\u95f4\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e CPU \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\uff1a<\/p>\n<pre><code class=\"language-java\">@Configuration\npublic class ExecutorConfig {\n\n    @Bean\n    public ThreadPoolTaskExecutor taskExecutor() {\n        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\n        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());\n        executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() + 1);\n        executor.setQueueCapacity(100);\n        executor.setThreadNamePrefix(&quot;CPU-Intensive-&quot;);\n        executor.initialize();\n        return executor;\n    }\n}\n<\/code><\/pre>\n<p>\u5bf9\u4e8e IO \u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u4f60\u53ef\u4ee5\u8bbe\u7f6e\uff1a<\/p>\n<pre><code class=\"language-java\">@Configuration\npublic class ExecutorConfig {\n\n    @Bean\n    public ThreadPoolTaskExecutor taskExecutor() {\n        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\n        int coreCount = Runtime.getRuntime().availableProcessors();\n        executor.setCorePoolSize(coreCount * 2);\n        executor.setMaxPoolSize(coreCount * 4);\n        executor.setQueueCapacity(1000);\n        executor.setThreadNamePrefix(&quot;IO-Intensive-&quot;);\n        executor.initialize();\n        return executor;\n    }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u914d\u7f6e\u5e94\u6839\u636e\u5b9e\u9645\u7684\u7cfb\u7edf\u8d1f\u8f7d\u548c\u4efb\u52a1\u7279\u6027\u8fdb\u884c\u8c03\u6574\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u5e94\u7528\u4e2d\uff0c\u5408\u7406\u914d\u7f6e\u7ebf\u7a0b\u6c60\u5bf9\u4e8e\u4f18\u5316\u6027\u80fd\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406 CPU \u5bc6\u96c6\u578b\u548c IO  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[10,25],"class_list":["post-260","post","type-post","status-publish","format-standard","hentry","category-be","tag-java","tag-25"],"_links":{"self":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts\/260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/comments?post=260"}],"version-history":[{"count":2,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts\/260\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/posts\/260\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/media?parent=260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/categories?post=260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iwonder.cc\/index.php\/wp-json\/wp\/v2\/tags?post=260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}