{"id":10006136,"date":"2022-08-08T19:24:08","date_gmt":"2022-08-08T11:24:08","guid":{"rendered":"https:\/\/ityet.com\/?p=10006136"},"modified":"2022-08-08T19:24:08","modified_gmt":"2022-08-08T11:24:08","slug":"%e5%bc%80%e6%ba%90%e9%a1%b9%e7%9b%ae%e5%8f%af%e8%a7%82%e6%b5%8b%e3%80%81%e6%98%93%e4%bd%bf%e7%94%a8%e7%9a%84springboot%e7%ba%bf%e7%a8%8b%e6%b1%a0","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006136","title":{"rendered":"[\u5f00\u6e90\u9879\u76ee]\u53ef\u89c2\u6d4b\u3001\u6613\u4f7f\u7528\u7684SpringBoot\u7ebf\u7a0b\u6c60"},"content":{"rendered":"<p>\u5728\u5f00\u53d1spring boot\u5e94\u7528\u670d\u52a1\u7684\u65f6\u5019\uff0c\u96be\u514d\u4f1a\u4f7f\u7528\u5230\u5f02\u6b65\u4efb\u52a1\u53ca\u7ebf\u7a0b\u6c60\u3002spring boot\u7684\u7ebf\u7a0b\u6c60\u662f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u7ecf\u5e38\u4f1a\u5728\u9879\u76ee\u91cc\u9762\u770b\u5230\u7c7b\u4f3c\u4e8e\u4e0b\u9762\u8fd9\u6837\u7684\u4ee3\u7801<\/p>\n<pre><code>@Bean\npublic Executor taskExecutor() {\n    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\n    executor.setCorePoolSize(config.getCorePoolSize());\n    executor.setMaxPoolSize(config.getMaxPoolSize());\n    executor.setQueueCapacity(config.getQueueCapacity());\n    executor.setThreadNamePrefix(&quot;TaskExecutePool-&quot;);\n    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());\n    executor.setWaitForTasksToCompleteOnShutdown(true);\n    executor.initialize();\n    return executor;\n}\n<\/code><\/pre>\n<p>\u4f7f\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\uff0c\u4f46\u662f\u8fd9\u6837\u505a\u6709\u51e0\u4e2a\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u5f00\u53d1\u4eba\u5458\u5728\u4ee3\u7801\u91cc\u9762\u968f\u610f\u5b9a\u4e49\u7ebf\u7a0b\u6c60\uff0c\u5f00\u53d1\u4eba\u5458A\u81ea\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5f00\u53d1\u4eba\u5458B\u81ea\u5b9a\u4e49\u4e00\u4e2a\u7ebf\u7a0b\u6c60\u3002\u7ebf\u7a0b\u6c60\u7684\u8d44\u6e90\u7684\u4f7f\u7528\u6ca1\u6709\u89c4\u5212\u4e0e\u5408\u7406\u7684\u5b89\u6392\uff0c\u540e\u671f\u7ef4\u62a4\u7684\u6210\u672c\u5347\u9ad8\u3002<\/li>\n<li>\u4e00\u65e6\u53d1\u73b0\u7ebf\u7a0b\u6c60\u6570\u91cf\u4e0d\u8db3\u6216\u8d44\u6e90\u6ee1\u8f7d\uff0c\u5f88\u96be\u8c03\u6574\u914d\u7f6e\uff0c\u53ea\u80fd\u8c03\u6574\u4ee3\u7801\uff0c\u91cd\u65b0\u90e8\u7f72\u3002<\/li>\n<li>\u591a\u4eba\u7f16\u5199\u4ee3\u7801\uff0c\u5f88\u96be\u7edf\u8ba1\u90a3\u4e2a\u670d\u52a1\u91cc\u9762\u5b58\u5728\u7ebf\u7a0b\u6c60\uff0c\u6709\u51e0\u4e2a\u7ebf\u7a0b\u6c60\u3002<\/li>\n<li>\u5982\u679c\u4e0d\u53bb\u8ddf\u8e2a\u4ee3\u7801\uff0c\u4f60\u5f88\u96be\u77e5\u9053\u5b83\u7684\u4f7f\u7528\u60c5\u51b5\u3002\u5b9a\u4e49\u4e8650\u4e2a\u7ebf\u7a0b\uff0c\u5b58\u4e0d\u5b58\u5728\u8d44\u6e90\u6d6a\u8d39\uff1f\u5b58\u4e0d\u5b58\u5728\u8d44\u6e90\u7b49\u5f85\uff1f<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u7684\u95ee\u9898\uff0c\u6211\u5f00\u53d1\u4e86\u4e00\u4e2aSpring Boot Starter\uff08\u5f00\u6e90\u9879\u76ee\u5730\u5740\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=zw1qGBnKuT1cB85nnvcsdA%3D%3D.gzReWWszPdt34az5jgRULZfDOk1T6%2Bw6keuxThryjmDncm4BOCCXpac6akDpHoK06g1I48ebARPd6aLWhDSj%2FA%3D%3D\" rel=\"nofollow\">https:\/\/gitee.com\/hanxt\/zimug&#8230;<\/a> \uff09\uff0c\u65b9\u4fbf\u96c6\u6210\u5230Spring Boot\u9879\u76ee\u91cc\u9762\u53bb\u3002\u76ee\u6807\u662f\uff1a<strong>\u5728\u4e0d\u6539\u53d8SpringBoot\u7ebf\u7a0b\u6c60\u7684\u6838\u5fc3\u5b9e\u73b0\u7684\u57fa\u7840\u4e0a\uff0c\u4f7f\u5176\u53ef\u89c6\u5316\u3001\u6613\u89c2\u6d4b\u3001\u6613\u914d\u7f6e\u3001\u6613\u4f7f\u7528<\/strong>\u3002<\/p>\n<blockquote><p>\u9700\u8981\u8bf4\u660e\u7684\u662f\uff1azimug-monitor-threadpool\u5e76\u672a\u6539\u53d8SpringBoot\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0\uff0c\u53ea\u662f\u5728\u5176\u57fa\u7840\u4e0a\u6dfb\u52a0\u4e86\u521d\u59cb\u5316\u9636\u6bb5\u7684\u914d\u7f6e\u81ea\u52a8\u5316\u52a0\u8f7d\uff0c\u8fd0\u884c\u65f6\u7684\u72b6\u6001\u76d1\u63a7\u3002\u6240\u4ee5\u4efb\u4f55\u6709\u5173Spring Boot\u7ebf\u7a0b\u6c60\u8fd0\u884c\u65f6\u6027\u80fd\u7684\u8ba8\u8bba\uff0c\u90fd\u4e0e\u672c\u6587\u53ca\u5176\u5b9e\u73b0\u65e0\u5173\u3002<\/p><\/blockquote>\n<h2>\u4e00\u3001\u6613\u96c6\u6210\u3001\u6613\u914d\u7f6e<\/h2>\n<p>\u901a\u8fc7\u4e0a\u6587\u7684\u9879\u76ee\u5730\u5740\u83b7\u53d6\u6e90\u7801\uff0c\u7136\u540emaven\u7f16\u8bd1install\u672c\u5730m2\u4ed3\u5e93\u3002\u7136\u540e\u901a\u8fc7\u4e0b\u9762\u7684maven\u5750\u6807\u5f15\u5165<\/p>\n<pre><code>&lt;dependency&gt;\n    &lt;groupId&gt;com.zimug&lt;\/groupId&gt;\n    &lt;artifactId&gt;zimug-monitor-threadpool&lt;\/artifactId&gt;\n    &lt;version&gt;1.0&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u5982\u4e0b\u914d\u7f6espring boot YAML(application.yml)\u6240\u793a\uff0c\u914d\u7f6e\u4e86\u4e24\u4e2a\u7ebf\u7a0b\u6c60\uff0c\u5206\u522b\u662ftest\u3001test2\u3002\u5f53<code>thread-pool.enable=true<\/code>\u7684\u65f6\u5019\u7ebf\u7a0b\u6c60\u914d\u7f6e\u751f\u6548\u3002<\/p>\n<pre><code>thread-pool:\n  enable: true\n  poolLists:\n    - poolId: test    #\u7ebf\u7a0b\u6c60\u552f\u4e00\u6807\u8bc6\n      poolName: \u6d4b\u8bd51   #\u7ebf\u7a0b\u6c60\u7684\u4e2d\u6587\u63cf\u8ff0\uff0c\u6bd4\u5982\u7ebf\u7a0b\u6c60\u7ed9\u8c01\u7528\uff1f\n      coreSize: 5  #\u7ebf\u7a0b\u6c60\u521d\u59cb\u5316\u6838\u5fc3\u7ebf\u7a0b\u6570\u91cf\n      maxSize: 10  #\u7ebf\u7a0b\u6c60\u6700\u5927\u7ebf\u7a0b\u5bb9\u91cf\n      queueCapacity: 10  #\u7ebf\u7a0b\u6c60\u7b49\u5f85\u961f\u5217\u7684\u5bb9\u91cf\n    - poolId: test2\n      poolName: \u6d4b\u8bd52\n      coreSize: 5\n      maxSize: 10\n      queueCapacity: 10<\/code><\/pre>\n<p>\u901a\u8fc7\u4e0b\u9762\u7684\u8fd9\u5f20\u56fe\u7406\u89e3\u4e0a\u9762\u7684\u914d\u7f6e\u4fe1\u606f<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc1Adl\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u5f53\u7ebf\u7a0b\u4efb\u52a1\u6570\u91cfcore_size\u88ab\u6d3b\u8dc3\u4efb\u52a1\u7ebf\u7a0b\u5360\u6ee1\u4e4b\u540e\uff0c\u7ebf\u7a0b\u4efb\u52a1\u4f1a\u88ab\u653e\u5165\u7b49\u5f85\u961f\u5217(queueCapacity=10)<\/li>\n<li>\u5f53\u7b49\u5f85\u961f\u5217queueCapacity\u4e5f\u88ab\u5360\u6ee1\u4e4b\u540e\uff0c\u624d\u4f1a\u6269\u5927\u7ebf\u7a0b\u6c60\u7684\u5bb9\u91cf<\/li>\n<li>\u7ebf\u7a0b\u6c60\u7684\u5bb9\u91cf\u6700\u5927\u6269\u5c55\u5230maxSize\u3002\u5982\u679cmaxSize\u548cqueueCapacity\u90fd\u6ee1\u4e86\uff0c\u4efb\u52a1\u5c31\u963b\u585e\u4e86\u3002<\/li>\n<\/ul>\n<h2>\u4e8c\u3001\u6613\u4f7f\u7528<\/h2>\n<p>\u4f7f\u7528\u65b9\u5f0f\u548cSpringBoot \u4ee3\u7801\u65b9\u5f0f\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528\u65b9\u5f0f\u662f\u4e00\u6837\u7684\u3002\u4f7f\u7528<code>@Async<\/code>\u6ce8\u89e3\u7684\u503c\u662ftest\uff0c\u8c03\u7528\u8be5\u6ce8\u89e3\u6807\u8bc6\u7684\u51fd\u6570\u5c31\u4f1a\u653e\u5165\u4e0a\u6587\u4e2d\u914d\u7f6e\u7684test\u7ebf\u7a0b\u6c60\u91cc\u9762\u53bb\u6267\u884c\u3002<\/p>\n<pre><code class=\"java\">@Component\npublic class TestTask {\n    @Async(&quot;test&quot;)   \/\/\u6ce8\u610f\u8fd9\u91cc\uff0ctest\u662f\u7ebf\u7a0b\u6c60\u914d\u7f6e\u7684poolId\n    public Future&lt;String&gt; test() throws Exception {\n        System.out.println(&quot;\u5f53\u524d\u7ebf\u7a0b\uff1a&quot; + Thread.currentThread().getName());\n        return new AsyncResult&lt;&gt;(&quot;\u6d4b\u8bd5\u4efb\u52a1&quot;);\n    }\n}<\/code><\/pre>\n<h2>\u4e09\u3001\u53ef\u89c6\u5316\u6613\u89c2\u6d4b<\/h2>\n<p>\u5728\u9879\u76ee\u4e2d\u5f15\u5165zimug-monitor-threadpool\u4e4b\u540e\uff0c\u8fdb\u884c\u7ebf\u7a0b\u6c60\u914d\u7f6e\uff0c\u4f7f\u7528\u7ebf\u7a0b\u6c60\u3002\u8bbf\u95ee\u670d\u52a1\u7684<code>\/pool.html<\/code>\u5373\u53ef\u83b7\u53d6\u5f53\u524dSpringBoot\u670d\u52a1\u7684\u7ebf\u7a0b\u6c60\u914d\u7f6e\u4fe1\u606f\uff0c\u4ee5\u53ca\u8fd0\u884c\u65f6\u72b6\u6001\u4fe1\u606f\u3002<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc1Adm\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li>\u7ebf\u7a0b\u6c60ID\u3001\u63cf\u8ff0\u3001\u521d\u59cb\u5316\u7ebf\u7a0b\u6570\u3001\u6700\u5927\u7ebf\u7a0b\u6570\u3001\u4efb\u52a1\u7b49\u5f85\u961f\u5217\u7684\u5bb9\u91cf\u662f\u4e0a\u6587\u4e2dyaml\u9759\u6001\u914d\u7f6e<\/li>\n<li>\u5f53\u524d\u7ebf\u7a0b\u6c60\u7684\u5bb9\u91cf\uff0c\u5373\uff1a\u7ebf\u7a0b\u6c60\u5f53\u524d\u7684\u7ebf\u7a0b\u6570\u91cf\uff08\u6d3b\u8dc3+\u975e\u6d3b\u8dc3\u7ebf\u7a0b\u6570\u603b\u548c\uff09<\/li>\n<li>\u5f53\u524d\u6d3b\u8dc3\u7ebf\u7a0b\u6570\uff0c\u5373\uff1a\u6b63\u5728\u8fd0\u884c\u7a0b\u5e8f\u4efb\u52a1\u7684\u7ebf\u7a0b\u6570\u91cf<\/li>\n<li>\u7ebf\u7a0b\u6c60\u6d3b\u8dc3\u7ebf\u7a0b\u7684\u6700\u5927\u5cf0\u503c\uff0c\u5982\u679c\u8be5\u503c\u7b49\u4e8e\u521d\u59cb\u5316\u7ebf\u7a0b\u6570\uff0c\u8bf4\u660e\u66fe\u7ecf\u51fa\u73b0\u4e86\u4efb\u52a1\u7b49\u5f85\uff0c\u5373\uff1a\u4efb\u52a1\u653e\u5165\u7b49\u5f85\u961f\u5217\uff0c\u6548\u7387\u8f83\u4f4e\u3002\u5982\u679c\u8be5\u503c\u5927\u4e8e\u521d\u59cb\u5316\u7ebf\u7a0b\u6570\uff0c\u8bf4\u660e\u4efb\u52a1\u7b49\u5f85\u961f\u5217\u66fe\u7ecf\u6ee1\u8f7d\uff0c\u9700\u8981\u6269\u5bb9\u3002\u5982\u679c\u8be5\u503c\u63a5\u8fd1\u7b49\u4e8e\u6700\u5927\u7ebf\u7a0b\u6570\uff0c\u5c31\u9700\u8981\u6269\u5927\u6700\u5927\u7ebf\u7a0b\u6570\u7684\u503c\u3002<\/li>\n<li>\u5f53\u524d\u4efb\u52a1\u7b49\u5f85\u961f\u5217\u5269\u4f59\u7684\u5bb9\u91cf\uff0c\u5269\u7684\u8d8a\u5c11\uff0c\u8bf4\u660e\u6b63\u5728\u7b49\u5f85\u6267\u884c\u7684\u4efb\u52a1\u5c31\u8d8a\u591a\u3002<\/li>\n<\/ul>\n<h2>\u56db\u3001\u5b9e\u73b0\u539f\u7406<\/h2>\n<p>zimug-monitor-threadpool\u7684\u5b9e\u73b0\u539f\u7406\u4e5f\u975e\u5e38\u7b80\u5355\uff0c\u7b80\u5355\u8bf4\u4e00\u4e0b\u539f\u7406\uff0c\u5177\u4f53\u5b9e\u73b0\u53c2\u8003\u6e90\u7801\u3002<\/p>\n<ul>\n<li>\u9996\u5148\u901a\u8fc7SpringBoot\u52a0\u8f7dyaml\u914d\u7f6e\u4fe1\u606f\uff0c\u914d\u7f6e\u52a0\u8f7d\u5b8c\u6210\u4e4b\u540e\u81ea\u5b9a\u4e49\u5b9e\u73b0\u914d\u7f6e\u81ea\u52a8\u5316\u52a0\u8f7d\u3002\u8fd9\u4e2a\u5b9e\u73b0\u539f\u7406\u53ca\u5b9e\u73b0\u65b9\u6cd5\u7f51\u4e0a\u5230\u5904\u90fd\u662f\uff0c\u6211\u5c31\u4e0d\u5199\u4e86\u3002<\/li>\n<li>\n<p>\u5c06\u914d\u7f6e\u4fe1\u606f\u52a0\u8f7d\u4e4b\u540enew \u4e00\u4e2aThreadPoolTaskExecutor \u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7Spring\u7684ConfigurableBeanFactory\u5c06\u7ebf\u7a0b\u6c60\u5bf9\u8c61\u7684bean\u6ce8\u518c\u5230Spring\u4e0a\u4e0b\u6587\u73af\u5883\u4e2d\uff0cbean\u7684id\u662fpoolId\u914d\u7f6e\u3002\u5c31\u53ef\u4ee5\u63d0\u4f9b\u7ed9\u8fd0\u884c\u65f6\u4efb\u52a1\u4f7f\u7528\u4e86\u3002<\/p>\n<pre><code>configurableBeanFactory.registerSingleton(pool.getPoolId(), taskExecutor);<\/code><\/pre>\n<\/li>\n<li>\n<p>\u5f85\u9700\u8981\u76d1\u6d4b\u7ebf\u7a0b\u6c60\u8fd0\u884c\u65f6\u72b6\u6001\u7684\u65f6\u5019\uff0c\u518d\u628a\u7ebf\u7a0b\u6c60\u5bf9\u8c61\u901a\u8fc7getBean\u65b9\u6cd5\u83b7\u53d6\u5230\uff0c\u4ece\u800c\u83b7\u53d6\u8fd0\u884c\u65f6\u4fe1\u606f\u8fd4\u56de\u7ed9\u524d\u53f0\u8bf7\u6c42\u3002<\/p>\n<pre><code>ThreadPoolTaskExecutor memThreadPool = (ThreadPoolTaskExecutor) applicationContext.getBean(poolModel.getPoolId());<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u516c\u544a\u53f7\uff1a\u5b57\u6bcd\u54e5\u6742\u8c08\uff0c\u56de\u590d003\u8d60\u9001\u672c\u6587\u6240\u5728\u4e13\u680f\u300adocker\u4fee\u70bc\u4e4b\u9053\u300b\u7684PDF\u7248\u672c\uff0c30\u4f59\u7bc7\u7cbe\u54c1docker\u6587\u7ae0\u3002\u5b57\u6bcd\u54e5\u535a\u5ba2\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=cf5ubMDKiE0DP4DKqzzTGg%3D%3D.5JgaepP4NOFXE2tRuTwhrvURxWpb2WjTaUKbXInu9ec%3D\" rel=\"nofollow\">zimug.com<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042284199\">https:\/\/segmentfault.com\/a\/1190000042284199<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u53d1spring boot\u5e94\u7528\u670d\u52a1\u7684\u65f6\u5019\uff0c\u96be\u514d\u4f1a\u4f7f\u7528\u5230\u5f02\u6b65\u4efb\u52a1\u53ca\u7ebf\u7a0b\u6c60\u3002spring boot\u7684\u7ebf\u7a0b\u6c60\u662f\u53ef\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006136","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006136"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006136\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}