{"id":1808,"date":"2025-12-30T18:46:19","date_gmt":"2025-12-30T10:46:19","guid":{"rendered":"https:\/\/eve2333.top\/?p=1808"},"modified":"2025-12-31T22:57:12","modified_gmt":"2025-12-31T14:57:12","slug":"%e5%8a%a8%e6%80%81%e7%ba%bf%e7%a8%8b%e6%b1%a0onethread%e7%b3%bb%e7%bb%9f-%e7%ac%ac%e5%8d%81%e5%85%ad%e9%83%a8%e5%88%86-%e8%a7%82%e5%af%9f%e8%80%85%e6%a8%a1%e5%bc%8f%e9%87%8d%e6%9e%84","status":"publish","type":"post","link":"https:\/\/eve2333.top\/?p=1808","title":{"rendered":"\u52a8\u6001\u7ebf\u7a0b\u6c60oneThread\u7cfb\u7edf \u2014 \u7b2c\u5341\u516d\u90e8\u5206\u00a0\u89c2\u5bdf\u8005\u6a21\u5f0f\u91cd\u6784\u914d\u7f6e\u52a8\u6001\u5237\u65b0"},"content":{"rendered":"\n<p>\u8fd9\u7bc7\u6587\u7ae0\u6df1\u5165\u5256\u6790\u4e86 <code>oneThread<\/code> \u52a8\u6001\u7ebf\u7a0b\u6c60\u6846\u67b6\u4e2d\u5982\u4f55\u5229\u7528 <strong>\u89c2\u5bdf\u8005\u6a21\u5f0f\uff08Observer Pattern\uff09<\/strong> \u6765\u89e3\u51b3\u67b6\u6784\u4e2d\u7684\u6a21\u5757\u89e3\u8026\u95ee\u9898\u3002\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u7ecf\u5178\u7684\u67b6\u6784\u8bbe\u8ba1\u6848\u4f8b\uff0c\u7279\u522b\u662f\u7ed3\u5408 Spring \u7684\u4e8b\u4ef6\u673a\u5236\u6765\u5b9e\u73b0\uff0c\u65e2\u4f18\u96c5\u53c8\u5b9e\u7528\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u8ba9\u4f60\u900f\u5f7b\u7406\u89e3\uff0c\u6211\u5c06\u5185\u5bb9\u62c6\u89e3\u4e3a\u56db\u4e2a\u90e8\u5206\u8fdb\u884c\u8be6\u7ec6\u8bb2\u89e3\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u67b6\u6784\u6311\u6218\u4e0e\u75db\u70b9<\/strong>\uff1a\u4e3a\u4ec0\u4e48\u8981\u7528\u89c2\u5bdf\u8005\u6a21\u5f0f\uff1f<\/li>\n\n\n\n<li><strong>\u6838\u5fc3\u539f\u7406<\/strong>\uff1a\u89c2\u5bdf\u8005\u6a21\u5f0f\u4e0e Spring \u4e8b\u4ef6\u673a\u5236\u3002<\/li>\n\n\n\n<li><strong>\u6e90\u7801\u5b9e\u73b0\u89e3\u6790<\/strong>\uff1aoneThread \u4e2d\u662f\u5982\u4f55\u843d\u5730\u5b9e\u73b0\u7684\uff1f<\/li>\n\n\n\n<li><strong>\u6269\u5c55\u6027\u4f18\u52bf<\/strong>\uff1a\u5982\u4f55\u5229\u7528\u8be5\u6a21\u5f0f\u8f7b\u677e\u6269\u5c55\u65b0\u529f\u80fd\uff1f<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e00\u90e8\u5206\uff1a\u67b6\u6784\u6311\u6218\u4e0e\u75db\u70b9 \u2014\u2014 \u4e3a\u4ec0\u4e48\u8981\u89e3\u8026\uff1f<\/h3>\n\n\n\n<p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u6216\u7ec4\u4ef6\u5316\u5f00\u53d1\u4e2d\uff0c<strong>\u8de8\u6a21\u5757\u901a\u4fe1<\/strong>\u4e00\u76f4\u662f\u4e2a\u96be\u9898\u3002\u5728 <code>oneThread<\/code> \u6846\u67b6\u4e2d\uff0c\u8fd9\u4e2a\u95ee\u9898\u5c24\u4e3a\u7a81\u51fa\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u6a21\u5757\u7ed3\u6784\u73b0\u72b6<\/h4>\n\n\n\n<p><code>oneThread<\/code> \u7684\u6a21\u5757\u5206\u4e3a\u4e24\u7c7b\u89d2\u8272\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53d1\u5e03\u8005\uff08\u914d\u7f6e\u4e2d\u5fc3\uff09<\/strong>\uff1a\u8d1f\u8d23\u76d1\u542c Nacos\u3001Apollo \u7b49\u914d\u7f6e\u4e2d\u5fc3\u7684\u53d8\u5316\uff08\u5982 <code>nacos-cloud-spring-boot-starter<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8ba2\u9605\u8005\uff08\u5177\u4f53\u7ec4\u4ef6\uff09<\/strong>\uff1a\u8d1f\u8d23\u5b9e\u9645\u6267\u884c\u7ebf\u7a0b\u6c60\u7684\u4fee\u6539\uff08\u5982 <code>web-spring-boot-starter<\/code> \u4fee\u6539 Tomcat \u7ebf\u7a0b\u6c60\uff0c\u6216\u4e1a\u52a1\u4ee3\u7801\u4e2d\u7684\u81ea\u5b9a\u4e49\u7ebf\u7a0b\u6c60\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"660\" src=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767055691-image-1024x660.png\" alt=\"\" class=\"wp-image-1812\" style=\"width:638px;height:auto\" srcset=\"https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767055691-image-1024x660.png 1024w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767055691-image-300x193.png 300w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767055691-image-768x495.png 768w, https:\/\/eve2333.top\/wp-content\/uploads\/2025\/12\/1767055691-image.png 1109w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u5982\u679c\u4e0d\u4f7f\u7528\u89c2\u5bdf\u8005\u6a21\u5f0f\uff08\u76f4\u63a5\u4f9d\u8d56\uff09<\/h4>\n\n\n\n<p>\u5982\u679c\u91c7\u7528\u4f20\u7edf\u7684\u76f4\u63a5\u8c03\u7528\u65b9\u5f0f\uff0c\u4ee3\u7801\u4f1a\u53d8\u6210\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u914d\u7f6e\u4e2d\u5fc3\u6a21\u5757\u7684\u4ee3\u7801\npublic class AbstractDynamicThreadPoolRefresher {\n\n    \/\/ 1. \u5fc5\u987b\u76f4\u63a5\u4f9d\u8d56\u6240\u6709\u53ef\u80fd\u7684\u4e0b\u6e38\u7ec4\u4ef6\n    @Autowired(required = false)\n    private WebThreadPoolService webService; \/\/ Tomcat\/Jetty\u7ebf\u7a0b\u6c60\n\n    @Autowired(required = false) \n    private RocketMQThreadPoolService rocketMQService; \/\/ MQ\u7ebf\u7a0b\u6c60\n\n    public void refreshConfig(String config) {\n        \/\/ 2. \u6bcf\u6b21\u914d\u7f6e\u53d8\u66f4\uff0c\u90fd\u8981\u663e\u5f0f\u8c03\u7528\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u65b9\u6cd5\n        if (webService != null) {\n            webService.update(config);\n        }\n        if (rocketMQService != null) {\n            rocketMQService.update(config);\n        }\n        \/\/ \u5982\u679c\u672a\u6765\u52a0\u4e86 Dubbo \u7ebf\u7a0b\u6c60\uff0c\u8fd9\u91cc\u8fd8\u5f97\u6539\u4ee3\u7801\uff01\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u8fd9\u79cd\u65b9\u5f0f\u7684\u81f4\u547d\u7f3a\u9677<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5f3a\u8026\u5408<\/strong>\uff1a\u914d\u7f6e\u4e2d\u5fc3\u6a21\u5757\u5fc5\u987b\u201c\u8ba4\u8bc6\u201d\u6240\u6709\u4e0b\u6e38\u7ec4\u4ef6\u3002\u8fd9\u5c31\u597d\u6bd4\u62a5\u793e\u5fc5\u987b\u8ba4\u8bc6\u6bcf\u4e00\u4e2a\u8bfb\u8005\u7684\u8138\u624d\u80fd\u53d1\u62a5\u7eb8\u3002<\/li>\n\n\n\n<li><strong>\u8fdd\u53cd\u201c\u5f00\u95ed\u539f\u5219\u201d<\/strong>\uff1a\u6bcf\u5f53\u589e\u52a0\u4e00\u79cd\u65b0\u7684\u7ebf\u7a0b\u6c60\u7c7b\u578b\uff08\u6bd4\u5982\u65b0\u589e Dubbo \u652f\u6301\uff09\uff0c\u5c31\u5fc5\u987b\u4fee\u6539\u914d\u7f6e\u4e2d\u5fc3\u7684\u6838\u5fc3\u4ee3\u7801\uff08<code>refreshConfig<\/code> \u65b9\u6cd5\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5305\u4f53\u79ef\u81a8\u80c0<\/strong>\uff1a\u4e3a\u4e86\u7f16\u8bd1\u901a\u8fc7\uff0c\u914d\u7f6e\u4e2d\u5fc3\u53ef\u80fd\u9700\u8981\u5f15\u5165\u6240\u6709\u4e0b\u6e38\u7ec4\u4ef6\u7684\u4f9d\u8d56\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a\u6211\u4eec\u9700\u8981\u4e00\u79cd\u673a\u5236\uff0c\u8ba9\u914d\u7f6e\u4e2d\u5fc3\u53ea\u7ba1\u201c\u558a\u4e00\u58f0\u201d\u914d\u7f6e\u53d8\u4e86\uff0c\u81f3\u4e8e\u8c01\u5173\u5fc3\u8fd9\u4e2a\u53d8\u5316\uff0c\u7531\u5173\u5fc3\u8005\u81ea\u5df1\u53bb\u5904\u7406\u3002\u8fd9\u5c31\u662f<strong>\u89c2\u5bdf\u8005\u6a21\u5f0f<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e8c\u90e8\u5206\uff1a\u6838\u5fc3\u539f\u7406 \u2014\u2014 Spring \u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b<\/h3>\n\n\n\n<p><code>oneThread<\/code> \u5e76\u6ca1\u6709\u4ece\u96f6\u624b\u5199\u89c2\u5bdf\u8005\u6a21\u5f0f\uff08\u7ef4\u62a4\u4e00\u4e2a List\uff09\uff0c\u800c\u662f\u5de7\u5999\u5229\u7528\u4e86 Spring \u5bb9\u5668\u539f\u751f\u7684 <strong>\u4e8b\u4ef6\u673a\u5236\uff08ApplicationEvent\uff09<\/strong>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u89d2\u8272\u6620\u5c04<\/h4>\n\n\n\n<p>Spring \u7684\u4e8b\u4ef6\u673a\u5236\u5929\u7136\u5c31\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">\u89c2\u5bdf\u8005\u6a21\u5f0f\u89d2\u8272<\/th><th class=\"has-text-align-left\" data-align=\"left\">Spring \u7ec4\u4ef6<\/th><th class=\"has-text-align-left\" data-align=\"left\">oneThread \u4e2d\u7684\u5b9e\u73b0<\/th><th class=\"has-text-align-left\" data-align=\"left\">\u804c\u8d23<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Subject (\u4e3b\u9898\/\u88ab\u89c2\u5bdf\u8005)<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>ApplicationEventPublisher<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>AbstractDynamicThreadPoolRefresher<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u8d1f\u8d23\u89e3\u6790\u914d\u7f6e\uff0c\u5e76\u201c\u5e7f\u64ad\u201d\u53d8\u66f4\u4e8b\u4ef6\u3002<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Event (\u4e8b\u4ef6\/\u6d88\u606f)<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>ApplicationEvent<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>ThreadPoolConfigUpdateEvent<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">\u627f\u8f7d\u6570\u636e\u7684\u8f7d\u4f53\uff08\u5305\u542b\u4e86\u6700\u65b0\u7684\u7ebf\u7a0b\u6c60\u914d\u7f6e\u53c2\u6570\uff09\u3002<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Observer (\u89c2\u5bdf\u8005\/\u76d1\u542c\u8005)<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>ApplicationListener<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>WebThreadPoolRefreshListener<\/code> \u7b49<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u76d1\u542c\u7279\u5b9a\u4e8b\u4ef6\uff0c\u6536\u5230\u901a\u77e5\u540e\u6267\u884c\u5177\u4f53\u7684\u66f4\u65b0\u903b\u8f91\u3002<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><strong>Event Bus (\u4e8b\u4ef6\u603b\u7ebf)<\/strong><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>ApplicationContext<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">Spring \u5bb9\u5668<\/td><td class=\"has-text-align-left\" data-align=\"left\">\u8d1f\u8d23\u5c06\u4e8b\u4ef6\u8def\u7531\u7ed9\u6240\u6709\u5339\u914d\u7684\u76d1\u542c\u5668\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u6d41\u7a0b\u56fe\u89e3<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u914d\u7f6e\u53d8\u66f4<\/strong>\uff1aNacos\/Apollo \u6536\u5230\u65b0\u914d\u7f6e\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u6790\u4e0e\u53d1\u5e03<\/strong>\uff1aRefresher \u89e3\u6790\u914d\u7f6e\uff0c\u8c03\u7528 <code>publishEvent()<\/code> \u53d1\u5e03\u4e8b\u4ef6\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u8def\u7531<\/strong>\uff1aSpring \u5bb9\u5668\u67e5\u627e\u6240\u6709\u76d1\u542c\u4e86 <code>ThreadPoolConfigUpdateEvent<\/code> \u7684 Bean\u3002<\/li>\n\n\n\n<li><strong>\u5404\u53f8\u5176\u804c<\/strong>\uff1aWebListener \u66f4\u65b0 Tomcat\uff0cCommonListener \u66f4\u65b0\u4e1a\u52a1\u7ebf\u7a0b\u6c60\u3002<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e09\u90e8\u5206\uff1a\u6e90\u7801\u843d\u5730\u89e3\u6790 \u2014\u2014 \u4ee3\u7801\u5c42\u9762\u7684\u7cbe\u5999\u4e4b\u5904<\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u770b\u770b <code>oneThread<\/code> \u5177\u4f53\u662f\u5982\u4f55\u5199\u4ee3\u7801\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u5b9a\u4e49\u4e8b\u4ef6\uff08\u4fe1\u5c01\uff09<\/h4>\n\n\n\n<p>\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u7c7b\uff0c\u628a\u914d\u7f6e\u4fe1\u606f\u5305\u5728\u91cc\u9762\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u7ee7\u627f ApplicationEvent\uff0c\u4f7f\u5176\u6210\u4e3a Spring \u4f53\u7cfb\u5185\u7684\u4e8b\u4ef6\npublic class ThreadPoolConfigUpdateEvent extends ApplicationEvent {\n\n    @Getter\n    @Setter\n    private BootstrapConfigProperties bootstrapConfigProperties;\n\n    public ThreadPoolConfigUpdateEvent(Object source, BootstrapConfigProperties properties) {\n        super(source);\n        this.bootstrapConfigProperties = properties;\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u53d1\u5e03\u4e8b\u4ef6\uff08\u53d1\u4fe1\u4eba\uff09<\/h4>\n\n\n\n<p>\u5728\u914d\u7f6e\u4e2d\u5fc3\u6a21\u5757\u7684\u57fa\u7c7b\u4e2d\uff0c\u89e3\u6790\u5b8c\u914d\u7f6e\u540e\uff0c<strong>\u53ea\u505a\u4e00\u4ef6\u4e8b\uff1a\u53d1\u5e03\u4e8b\u4ef6<\/strong>\u3002\u5b83\u5b8c\u5168\u4e0d\u77e5\u9053\u8c01\u4f1a\u6d88\u8d39\u8fd9\u4e2a\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public abstract class AbstractDynamicThreadPoolRefresher {\n\n    public void refreshThreadPoolProperties(String configInfo) {\n        \/\/ 1. \u89e3\u6790\u914d\u7f6e\u6587\u4ef6\uff08\u5c06 YAML\/JSON \u8f6c\u4e3a Java \u5bf9\u8c61\uff09\n        BootstrapConfigProperties properties = parseConfig(configInfo);\n\n        \/\/ 2. \u3010\u5173\u952e\u3011\u53d1\u5e03\u4e8b\u4ef6\uff01\n        \/\/ \u8fd9\u4e00\u884c\u4ee3\u7801\u5b9e\u73b0\u4e86\u5b8c\u5168\u89e3\u8026\u3002\u6211\u53ea\u7ba1\u53d1\uff0c\u4e0d\u7ba1\u8c01\u6536\u3002\n        ApplicationContextHolder.publishEvent(\n            new ThreadPoolConfigUpdateEvent(this, properties)\n        );\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u76d1\u542c\u4e8b\u4ef6\uff08\u6536\u4fe1\u4eba\uff09<\/h4>\n\n\n\n<p>\u4e0d\u540c\u7684\u6a21\u5757\u5b9e\u73b0\u5404\u81ea\u7684\u76d1\u542c\u5668\u3002<\/p>\n\n\n\n<p><strong>\u573a\u666f A\uff1a\u66f4\u65b0 Web \u5bb9\u5668\uff08Tomcat\uff09\u7ebf\u7a0b\u6c60<\/strong><br>\u8fd9\u4e2a\u7c7b\u4f4d\u4e8e <code>web-spring-boot-starter<\/code> \u5305\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@RequiredArgsConstructor\npublic class WebThreadPoolRefreshListener implements ApplicationListener&lt;ThreadPoolConfigUpdateEvent&gt; {\n\n    private final WebThreadPoolService webThreadPoolService;\n\n    @Override\n    public void onApplicationEvent(ThreadPoolConfigUpdateEvent event) {\n        \/\/ 1. \u4ece\u4e8b\u4ef6\u4e2d\u62ff\u51fa\u914d\u7f6e\n        var config = event.getBootstrapConfigProperties().getWeb();\n\n        \/\/ 2. \u6821\u9a8c\u914d\u7f6e\u662f\u5426\u6709\u6548\n        if (config == null) return;\n\n        \/\/ 3. \u6267\u884c Web \u76f8\u5173\u7684\u66f4\u65b0\u903b\u8f91\n        webThreadPoolService.updateThreadPool(config);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>\u573a\u666f B\uff1a\u66f4\u65b0\u666e\u901a\u4e1a\u52a1\u7ebf\u7a0b\u6c60<\/strong><br>\u8fd9\u4e2a\u7c7b\u4f4d\u4e8e <code>common-spring-boot-starter<\/code> \u5305\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class DynamicThreadPoolRefreshListener implements ApplicationListener&lt;ThreadPoolConfigUpdateEvent&gt; {\n    @Override\n    public void onApplicationEvent(ThreadPoolConfigUpdateEvent event) {\n        \/\/ \u903b\u8f91\u7c7b\u4f3c\uff0c\u4f46\u5904\u7406\u7684\u662f\u901a\u7528\u7ebf\u7a0b\u6c60\u5bf9\u8c61\n        \/\/ ...\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u56db\u90e8\u5206\uff1a\u6269\u5c55\u6027\u4e0e\u603b\u7ed3 \u2014\u2014 \u4e3a\u4ec0\u4e48\u8981\u8fd9\u4e48\u505a\uff1f<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u6781\u81f4\u7684\u6269\u5c55\u6027\uff08Open\/Closed \u539f\u5219\uff09<\/h4>\n\n\n\n<p>\u5047\u8bbe\u73b0\u5728\u4f60\u9700\u8981\u589e\u52a0\u5bf9 <strong>RocketMQ \u6d88\u8d39\u8005\u7ebf\u7a0b\u6c60<\/strong> \u7684\u52a8\u6001\u8c03\u6574\u652f\u6301\uff0c\u4f60<strong>\u4e0d\u9700\u8981\u4fee\u6539<\/strong> <code>AbstractDynamicThreadPoolRefresher<\/code>\uff08\u53d1\u5e03\u8005\uff09\u7684\u4efb\u4f55\u4e00\u884c\u4ee3\u7801\u3002<\/p>\n\n\n\n<p><strong>\u4f60\u53ea\u9700\u8981\u505a\u4e24\u4ef6\u4e8b\uff1a<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5728\u914d\u7f6e\u7c7b\u4e2d\u589e\u52a0 RocketMQ \u7684\u5b57\u6bb5\u3002<\/li>\n\n\n\n<li>\u65b0\u5efa\u4e00\u4e2a\u7c7b <code>RocketMQListener<\/code> \u5b9e\u73b0 <code>ApplicationListener&lt;ThreadPoolConfigUpdateEvent&gt;<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component\npublic class RocketMQListener implements ApplicationListener&lt;ThreadPoolConfigUpdateEvent&gt; {\n    @Override\n    public void onApplicationEvent(ThreadPoolConfigUpdateEvent event) {\n        \/\/ \u53ea\u8981\u914d\u7f6e\u91cc\u6709 rocketMQ \u7684\u914d\u7f6e\uff0c\u6211\u5c31\u66f4\u65b0\n        var mqConfig = event.getBootstrapConfigProperties().getRocketMQ();\n        if (mqConfig != null) {\n            rocketMQService.resize(mqConfig);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>Spring \u4f1a\u81ea\u52a8\u626b\u63cf\u5230\u8fd9\u4e2a\u65b0\u7ec4\u4ef6\uff0c\u5e76\u628a\u5b83\u52a0\u5165\u5230\u89c2\u5bdf\u8005\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u201c\u63d2\u4ef6\u5f0f\u201d\u7684\u5f00\u53d1\u4f53\u9a8c\u662f\u975e\u5e38\u723d\u7684\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2. Spring \u4e8b\u4ef6 vs Guava EventBus<\/h4>\n\n\n\n<p>\u6587\u4e2d\u63d0\u5230\u4e86 Guava\uff0c\u4e24\u8005\u7684\u533a\u522b\u5728\u4e8e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Spring Events<\/strong>\uff1a\u96f6\u4f9d\u8d56\uff08\u53ea\u8981\u662f Spring \u9879\u76ee\u5c31\u6709\uff09\uff0c\u5929\u7136\u96c6\u6210 IOC \u5bb9\u5668\uff08\u76d1\u542c\u5668\u81ea\u52a8\u6ce8\u5165\uff09\uff0cIDE \u652f\u6301\u597d\uff08\u5728\u53d1\u5e03\u4e8b\u4ef6\u7684\u5730\u65b9\u53ef\u4ee5\u770b\u5230\u8c01\u5728\u76d1\u542c\uff09\u3002<\/li>\n\n\n\n<li><strong>Guava<\/strong>\uff1a\u8f7b\u91cf\u7ea7\uff0c\u4f46\u9700\u8981\u989d\u5916\u5f15\u5165 Jar \u5305\uff0c\u4e14\u9700\u8981\u624b\u52a8\u6ce8\u518c\u76d1\u542c\u5668\u3002<\/li>\n\n\n\n<li><strong>\u7ed3\u8bba<\/strong>\uff1a\u5728 Spring Boot \u9879\u76ee\u4e2d\uff0c\u4f18\u5148\u4f7f\u7528 Spring Events\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u9664\u4e86\u524d\u9762\u63d0\u5230\u7684\u67b6\u6784\u89e3\u8026\u3001\u6269\u5c55\u6027\u4f18\u52bf\u4e4b\u5916\uff0c\u5173\u4e8e <strong>Spring \u4e8b\u4ef6\u9a71\u52a8\uff08\u89c2\u5bdf\u8005\u6a21\u5f0f\uff09<\/strong> \u5728\u5b9e\u9645\u751f\u4ea7\u843d\u5730\u65f6\uff0c\u8fd8\u6709\u51e0\u4e2a<strong>\u201c\u9690\u85cf\u7684\u5751\u201d<\/strong>\u548c<strong>\u201c\u9ad8\u9636\u6280\u5de7\u201d<\/strong>\u503c\u5f97\u6df1\u5165\u63a2\u8ba8\u3002<\/h4>\n\n\n\n<p>\u8fd9\u4e5f\u662f\u9762\u8bd5\u4e2d\u6216\u8005\u505a\u67b6\u6784\u8bc4\u5ba1\u65f6\uff0c\u7ecf\u5e38\u4f1a\u88ab\u95ee\u5230\u7684\u7ec6\u8282\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u540c\u6b65 vs \u5f02\u6b65\uff1a\u6027\u80fd\u4e0e\u4e00\u81f4\u6027\u7684\u535a\u5f08<\/h3>\n\n\n\n<p><strong>\u9ed8\u8ba4\u884c\u4e3a<\/strong>\uff1a<br>Spring \u7684 <code>ApplicationEvent<\/code> \u9ed8\u8ba4\u662f <strong>\u540c\u6b65\uff08Synchronous\uff09<\/strong> \u7684\u3002<br>\u8fd9\u610f\u5473\u7740\uff1a<code>publishEvent<\/code> \u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u53d1\u5e03\u8005\u7684\u7ebf\u7a0b\uff08\u4f8b\u5982 Nacos \u7684\u76d1\u542c\u7ebf\u7a0b\uff09\u4f1a<strong>\u963b\u585e<\/strong>\uff0c\u76f4\u5230\u6240\u6709\u76d1\u542c\u5668\uff08WebListener\u3001RocketMQListener\u2026\uff09\u7684 <code>onApplicationEvent<\/code> \u65b9\u6cd5\u90fd\u6267\u884c\u5b8c\u6bd5\uff0c\u4ee3\u7801\u624d\u4f1a\u7ee7\u7eed\u5f80\u4e0b\u8d70\u3002<\/p>\n\n\n\n<p><strong>\u8fd9\u91cc\u6709\u4e24\u4e2a\u6f5c\u5728\u98ce\u9669<\/strong>\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u963b\u585e<\/strong>\uff1a\u5982\u679c\u67d0\u4e2a\u76d1\u542c\u5668\u91cc\u6267\u884c\u4e86\u8017\u65f6\u64cd\u4f5c\uff08\u6bd4\u5982\u53d1\u90ae\u4ef6\u3001\u8bf7\u6c42\u5916\u90e8 API\uff09\uff0c\u4f1a\u5361\u4f4f\u6574\u4e2a\u914d\u7f6e\u5237\u65b0\u6d41\u7a0b\uff0c\u751a\u81f3\u5bfc\u81f4 Nacos\/Apollo \u5ba2\u6237\u7aef\u7684\u5fc3\u8df3\u8d85\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u4e8b\u52a1\u95ee\u9898<\/strong>\uff1a\u5982\u679c\u5728\u4e8b\u52a1\u5185\u53d1\u5e03\u4e8b\u4ef6\uff0c\u9ed8\u8ba4\u4e5f\u662f\u5728\u540c\u4e00\u4e2a\u4e8b\u52a1\u4e0a\u4e0b\u6587\u4e2d\uff08\u867d\u7136\u52a8\u6001\u7ebf\u7a0b\u6c60\u5237\u65b0\u901a\u5e38\u4e0d\u6d89\u53ca\u6570\u636e\u5e93\u4e8b\u52a1\uff0c\u4f46\u5728\u5176\u4ed6\u4e1a\u52a1\u573a\u666f\u9700\u6ce8\u610f\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>oneThread \u7684\u9009\u62e9<\/strong>\uff1a<br>\u5bf9\u4e8e\u52a8\u6001\u7ebf\u7a0b\u6c60\u914d\u7f6e\u5237\u65b0\uff0c<strong>\u4fdd\u6301\u540c\u6b65<\/strong>\u901a\u5e38\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u56e0<\/strong>\uff1a\u6211\u4eec\u9700\u8981\u786e\u4fdd\u201c\u914d\u7f6e\u53d8\u66f4\u201d\u7acb\u523b\u751f\u6548\u3002\u5982\u679c\u5f02\u6b65\u6267\u884c\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u914d\u7f6e\u4e2d\u5fc3\u663e\u793a\u201c\u5df2\u53d1\u5e03\u201d\uff0c\u4f46\u5e94\u7528\u5185\u90e8\u5b9e\u9645\u4e0a\u8fd8\u6ca1\u5207\u8fc7\u53bb\uff0c\u9020\u6210\u72b6\u6001\u4e0d\u4e00\u81f4\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u5316<\/strong>\uff1a\u5982\u679c\u76d1\u542c\u5668\u4e2d\u6709\u53d1\u90ae\u4ef6\/\u53d1\u77ed\u4fe1\u8fd9\u79cd\u975e\u6838\u5fc3\u903b\u8f91\uff08\u5982\u6d41\u7a0b\u56fe\u4e2d\u7684 <code>EmailSubscriber<\/code>\uff09\uff0c\u5efa\u8bae\u5728\u76d1\u542c\u5668\u5185\u90e8\u4f7f\u7528 <code>@Async<\/code> \u6216\u8005\u6254\u5230\u72ec\u7acb\u7684\u7ebf\u7a0b\u6c60\u53bb\u6267\u884c\uff0c\u4e0d\u8981\u963b\u585e\u6838\u5fc3\u53c2\u6570\u7684\u4fee\u6539\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5f02\u5e38\u9694\u79bb\uff1a\u9632\u6b62\u201c\u4e00\u9897\u8001\u9f20\u5c4e\u574f\u4e86\u4e00\u9505\u7ca5\u201d<\/h3>\n\n\n\n<p>\u7531\u4e8e\u9ed8\u8ba4\u662f\u540c\u6b65\u6267\u884c\u7684\uff0c\u8fd9\u5c31\u5e26\u6765\u4e86\u4e00\u4e2a<strong>\u5f02\u5e38\u4f20\u64ad<\/strong>\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p><strong>\u573a\u666f<\/strong>\uff1a<br>\u5047\u8bbe\u6211\u4eec\u6709 3 \u4e2a\u76d1\u542c\u5668\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>WebThreadPoolListener<\/code> (Tomcat)<\/li>\n\n\n\n<li><code>RocketMQThreadPoolListener<\/code> (MQ)<\/li>\n\n\n\n<li><code>CommonThreadPoolListener<\/code> (\u4e1a\u52a1)<\/li>\n<\/ol>\n\n\n\n<p>\u5982\u679c <code>WebThreadPoolListener<\/code> \u4ee3\u7801\u6709 Bug\uff0c\u629b\u51fa\u4e86\u4e00\u4e2a <code>RuntimeException<\/code>\uff0c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p><strong>\u540e\u679c<\/strong>\uff1a<br>Spring \u7684\u4e8b\u4ef6\u5e7f\u64ad\u5faa\u73af\u4f1a\u88ab\u4e2d\u65ad\uff0c<strong>\u5269\u4e0b\u7684 MQ \u548c \u4e1a\u52a1\u7ebf\u7a0b\u6c60\u76d1\u542c\u5668\u5c06\u6c38\u8fdc\u4e0d\u4f1a\u6536\u5230\u901a\u77e5<\/strong>\uff0c\u800c\u4e14\u5f02\u5e38\u4f1a\u5192\u6ce1\u5230\u53d1\u5e03\u8005\uff08\u914d\u7f6e\u4e2d\u5fc3\u7ec4\u4ef6\uff09\uff0c\u5bfc\u81f4\u6574\u4e2a\u5237\u65b0\u4efb\u52a1\u5931\u8d25\u3002<\/p>\n\n\n\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a<br>\u5728\u5b9e\u73b0\u76d1\u542c\u5668\u65f6\uff0c<strong>\u52a1\u5fc5\u8fdb\u884c Try-Catch \u515c\u5e95<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component\n@Slf4j\npublic class WebThreadPoolRefreshListener implements ApplicationListener&lt;ThreadPoolConfigUpdateEvent&gt; {\n\n    @Override\n    public void onApplicationEvent(ThreadPoolConfigUpdateEvent event) {\n        try {\n            \/\/ \u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\n            webThreadPoolService.update(event.getConfig());\n        } catch (Exception e) {\n            \/\/ \u541e\u6389\u5f02\u5e38\uff0c\u6253\u5370\u9519\u8bef\u65e5\u5fd7\uff0c\u7edd\u4e0d\u5f71\u54cd\u5176\u4ed6\u76d1\u542c\u5668\n            log.error(\"Failed to update Web ThreadPool\", e);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u63a7\u5236\u987a\u5e8f\uff1a\u8c01\u5148\u8c01\u540e\uff1f<\/h3>\n\n\n\n<p>\u867d\u7136\u89c2\u5bdf\u8005\u6a21\u5f0f\u901a\u5e38\u4e0d\u4f9d\u8d56\u987a\u5e8f\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b<strong>\u6709\u5e8f\u6267\u884c<\/strong>\u3002<br>\u6bd4\u5982\uff1a\u5148\u5237\u65b0\u6838\u5fc3\u4e1a\u52a1\u7ebf\u7a0b\u6c60\uff0c\u518d\u5237\u65b0 Web \u5bb9\u5668\u7ebf\u7a0b\u6c60\uff0c\u6700\u540e\u6267\u884c\u65e5\u5fd7\u8bb0\u5f55\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<br>Spring \u7684\u76d1\u542c\u5668\u652f\u6301 <code>@Order<\/code> \u6ce8\u89e3\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Component\n@Order(1) \/\/ \u4f18\u5148\u7ea7\u6700\u9ad8\uff0c\u5148\u6267\u884c\npublic class HighPriorityListener implements ApplicationListener&lt;ThreadPoolConfigUpdateEvent&gt; { ... }\n\n@Component\n@Order(99) \/\/ \u4f18\u5148\u7ea7\u4f4e\uff0c\u540e\u6267\u884c\npublic class LowPriorityListener implements ApplicationListener&lt;ThreadPoolConfigUpdateEvent&gt; { ... }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u907f\u514d\u201c\u4e8b\u4ef6\u98ce\u66b4\u201d (Event Storm)<\/h3>\n\n\n\n<p>\u5728\u52a8\u6001\u7ebf\u7a0b\u6c60\u573a\u666f\u4e0b\uff0c\u914d\u7f6e\u53d8\u66f4\u901a\u5e38\u9891\u7387\u8f83\u4f4e\u3002\u4f46\u5728\u67d0\u4e9b\u6781\u7aef\u60c5\u51b5\u4e0b\uff08\u4f8b\u5982\u914d\u7f6e\u4e2d\u5fc3\u53d1\u751f\u6296\u52a8\uff0c\u6216\u8005\u8fd0\u7ef4\u4eba\u5458\u8bef\u64cd\u4f5c\u8fde\u7eed\u70b9\u51fb\u4fdd\u5b58\uff09\uff0c\u53ef\u80fd\u4f1a\u5728\u77ed\u65f6\u95f4\u5185\u89e6\u53d1\u5927\u91cf <code>ThreadPoolConfigUpdateEvent<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u540e\u679c<\/strong>\uff1a\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7ebf\u7a0b\u6c60\uff08\u867d\u7136\u901a\u5e38\u53ea\u662f\u6539\u53c2\u6570\uff09\uff0c\u6216\u8005\u9891\u7e41\u6253\u5370\u65e5\u5fd7\u3001\u53d1\u9001\u62a5\u8b66\uff0c\u4f1a\u7ed9\u7cfb\u7edf\u9020\u6210\u538b\u529b\u3002<\/p>\n\n\n\n<p><strong>\u9ad8\u9636\u5904\u7406<\/strong>\uff1a<br>\u53ef\u4ee5\u5728\u53d1\u5e03\u8005\uff08Refresher\uff09\u6216\u76d1\u542c\u5668\u5c42\u9762\u505a<strong>\u9632\u6296\uff08Debounce\uff09<\/strong>\u5904\u7406\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f8b\u5982\uff1a\u6536\u5230\u53d8\u66f4\u540e\uff0c\u7b49\u5f85 5\u79d2\uff0c\u5982\u679c\u671f\u95f4\u6ca1\u6709\u65b0\u53d8\u66f4\uff0c\u518d\u53d1\u5e03\u4e8b\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u8c03\u8bd5\u6280\u5de7\uff1a\u600e\u4e48\u77e5\u9053\u8c01\u76d1\u542c\u4e86\uff1f<\/h3>\n\n\n\n<p>\u4f7f\u7528 Spring \u4e8b\u4ef6\u7684\u4e00\u4e2a\u5c0f\u7f3a\u70b9\u662f<strong>\u4ee3\u7801\u8df3\u8f6c\u4e0d\u76f4\u89c2<\/strong>\u3002\u4f60\u70b9\u51fb <code>publishEvent<\/code>\uff0cIDE \u4e0d\u4f1a\u76f4\u63a5\u8df3\u5230 <code>onApplicationEvent<\/code>\u3002<br>IntelliJ IDEA \u7684 Ultimate \u7248\uff08\u65d7\u8230\u7248\uff09\u5bf9 Spring \u652f\u6301\u975e\u5e38\u597d\u3002\u5728 <code>publishEvent<\/code> \u4ee3\u7801\u884c\u53f7\u5de6\u4fa7\uff0c\u901a\u5e38\u4f1a\u6709\u4e00\u4e2a<strong>\u7eff\u8272\u7684\u8c46\u5b50\u56fe\u6807<\/strong>\u6216<strong>\u8033\u673a\u56fe\u6807<\/strong>\uff0c\u70b9\u51fb\u5b83\uff0cIDEA \u4f1a\u5217\u51fa\u6240\u6709\u8ba2\u9605\u4e86\u8fd9\u4e2a\u4e8b\u4ef6\u7684\u76d1\u542c\u5668\u7c7b\uff0c\u65b9\u4fbf\u5feb\u901f\u8df3\u8f6c\u3002<\/p>\n\n\n\n<p><strong>\u8fd0\u884c\u65f6\u76d1\u63a7<\/strong>\uff1a<br>\u53ef\u4ee5\u5728\u65e5\u5fd7\u4e2d\u6253\u5370\u76d1\u542c\u5668\u5217\u8868\uff0c\u6216\u8005\u5728\u542f\u52a8\u65f6\u9a8c\u8bc1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u83b7\u53d6\u6240\u6709\u76d1\u542c\u8be5\u4e8b\u4ef6\u7684 Bean\nString&#91;] listeners = applicationContext.getBeanNamesForType(\n    ResolvableType.forClassWithGenerics(ApplicationListener.class, ThreadPoolConfigUpdateEvent.class)\n);\nlog.info(\"Current Listeners: {}\", Arrays.toString(listeners));<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u6df1\u5165\u5256\u6790\u4e86 oneThread \u52a8\u6001\u7ebf\u7a0b\u6c60\u6846\u67b6\u4e2d\u5982\u4f55\u5229\u7528 \u89c2\u5bdf\u8005\u6a21\u5f0f\uff08Observer Pattern\uff09 \u6765\u89e3\u51b3\u67b6\u6784\u4e2d\u7684\u6a21 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3],"tags":[19,20],"class_list":["post-1808","post","type-post","status-publish","format-standard","hentry","category-3","tag-java","tag-20"],"_links":{"self":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1808"}],"version-history":[{"count":6,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1808\/revisions"}],"predecessor-version":[{"id":1816,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1808\/revisions\/1816"}],"wp:attachment":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}