{"id":15402,"date":"2024-05-28T10:51:27","date_gmt":"2024-05-28T02:51:27","guid":{"rendered":"https:\/\/92it.top\/?p=15402"},"modified":"2024-05-28T10:51:51","modified_gmt":"2024-05-28T02:51:51","slug":"%e6%8a%80%e5%b7%a76%ef%bc%9a%e4%bd%bf%e7%94%a8webflux%e5%ae%9e%e7%8e%b0%e9%9d%9e%e9%98%bb%e5%a1%9e%e5%bc%8fio%e6%8f%90%e5%8d%87%e5%8d%95%e4%bd%93springboot%e7%9a%84%e5%b9%b6%e5%8f%91%e6%95%b0%e7%9a%84","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=15402","title":{"rendered":"\u63d0\u5347\u5355\u4f53springboot\u7684\u5e76\u53d1\u6570\u76848\u4e2a\u5c0f\u6280\u5de7"},"content":{"rendered":"\n<p>\u563f\uff0c\u60f3\u8981\u8ba9\u4f60\u7684SpringBoot\u5e94\u7528\u5e76\u53d1\u6570\u66f4\u4e0a\u4e00\u5c42\u697c\u5417\uff1f<\/p>\n\n\n\n<p>\u90a3\u4f60\u6765\u5bf9\u5730\u65b9\u4e86\uff01<\/p>\n\n\n\n<p>\u4eca\u5929\u6211\u4eec\u5c31\u6765\u804a\u804a\u5982\u4f55\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355\u800c\u6709\u6548\u7684\u6280\u5de7\u6765\u63d0\u5347\u4f60\u7684SpringBoot\u5355\u4f53\u5e94\u7528\u7684\u5e76\u53d1\u80fd\u529b\u3002<\/p>\n\n\n\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u4e92\u8054\u7f51\u7684\u9ad8\u901f\u53d1\u5c55\u4e0b\uff0c\u7528\u6237\u5bf9\u7f51\u7ad9\u548c\u5e94\u7528\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u5904\u7406\u80fd\u529b\u8981\u6c42\u8d8a\u6765\u8d8a\u9ad8\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4f7f\u7528SpringBoot\u5f00\u53d1\u7684\u5355\u4f53\u5e94\u7528\u6765\u8bf4\uff0c\u63d0\u5347\u5e76\u53d1\u6570\u4e0d\u4ec5\u80fd\u6539\u5584\u7528\u6237\u4f53\u9a8c\uff0c\u8fd8\u80fd\u63d0\u9ad8\u7cfb\u7edf\u7684\u541e\u5410\u91cf\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\u5462\uff1f<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u7684\u5185\u5bb9\u5c06\u4e3a\u4f60\u63ed\u6653\u3002<\/p>\n\n\n\n<p><strong>\u6b63\u6587<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0b\u9762\u662f 8 \u4e2a\u63d0\u5347\u4f60\u7684SpringBoot\u5e94\u7528\u5e76\u53d1\u80fd\u529b\u7684\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n\n\n\n<p><strong>\u6280\u5de71\uff1a\u4f18\u5316\u6570\u636e\u5e93\u8fde\u63a5\u6c60<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u7684\u5927\u5c0f\u5bf9\u4e8e\u5e94\u7528\u7684\u6027\u80fd\u6709\u7740\u76f4\u63a5\u7684\u5f71\u54cd\u3002<br>\u5408\u7406\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n\n\n\n<p>application.properties\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u914d\u7f6eHikariCP\u4e3aSpringBoot\u7684\u6570\u636e\u5e93\u8fde\u63a5\u6c60\nspring.datasource.hikari.maximum-pool-size=20 # \u8bbe\u7f6e\u8fde\u63a5\u6c60\u7684\u6700\u5927\u5927\u5c0f\nspring.datasource.hikari.minimum-idle=10 # \u8bbe\u7f6e\u8fde\u63a5\u6c60\u7684\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5<\/pre>\n\n\n\n<p><strong>\u6280\u5de72\uff1a\u4f7f\u7528\u7f13\u5b58\u51cf\u5c11\u6570\u636e\u5e93\u8bbf\u95ee<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u9891\u7e41\u7684\u6570\u636e\u5e93\u8bbf\u95ee\u4f1a\u663e\u8457\u964d\u4f4e\u5e94\u7528\u7684\u54cd\u5e94\u901f\u5ea6\u3002<br>\u901a\u8fc7\u4f7f\u7528\u7f13\u5b58\uff0c\u6211\u4eec\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u7684\u8bbf\u95ee\u6b21\u6570\uff0c\u4ece\u800c\u63d0\u5347\u5e76\u53d1\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.springframework.cache.annotation.Cacheable;\n\n@Service\npublic class UserService {\n\n    @Autowired\n    private UserRepository userRepository;\n\n    \/\/ \u4f7f\u7528Spring\u7f13\u5b58\u6ce8\u89e3\u6765\u7f13\u5b58\u65b9\u6cd5\u7ed3\u679c\n    @Cacheable(\"users\")\n    public User findUserById(Long id) {\n        return userRepository.findById(id).orElse(null); \/\/ \u67e5\u8be2\u6570\u636e\u5e93\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de73\uff1a\u5f02\u6b65\u5904\u7406\u8017\u65f6\u4efb\u52a1<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5bf9\u4e8e\u8017\u65f6\u7684\u4efb\u52a1\uff0c\u5982\u53d1\u9001\u90ae\u4ef6\u3001\u8c03\u7528\u5916\u90e8API\u7b49\uff0c\u53ef\u4ee5\u5f02\u6b65\u6267\u884c\uff0c\u907f\u514d\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import org.springframework.scheduling.annotation.Async;\n\n@Service\npublic class AsyncTaskService {\n\n    @Async\n    public void executeAsyncTask() {\n        \/\/ \u6267\u884c\u8017\u65f6\u7684\u5f02\u6b65\u4efb\u52a1\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de74\uff1a\u5408\u7406\u914d\u7f6eTomcat\u7ebf\u7a0b\u6c60<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>SpringBoot\u5185\u7f6e\u7684Tomcat\u670d\u52a1\u5668\u7684\u7ebf\u7a0b\u6c60\u914d\u7f6e\u4e5f\u4f1a\u5f71\u54cd\u5e76\u53d1\u80fd\u529b\u3002<br>\u9002\u5f53\u589e\u52a0\u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\u53ef\u4ee5\u5904\u7406\u66f4\u591a\u7684\u5e76\u53d1\u8bf7\u6c42\u3002<\/p>\n\n\n\n<p>application.properties\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server.tomcat.max-threads=200 # \u8bbe\u7f6eTomcat\u7ebf\u7a0b\u6c60\u7684\u6700\u5927\u7ebf\u7a0b\u6570\nserver.tomcat.min-spare-threads=20 # \u8bbe\u7f6eTomcat\u7ebf\u7a0b\u6c60\u7684\u6700\u5c0f\u5907\u7528\u7ebf\u7a0b\u6570<\/pre>\n\n\n\n<p><strong>\u6280\u5de75\uff1a\u9650\u6d41\u4e0e\u964d\u7ea7<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\uff0c\u9650\u6d41\u548c\u964d\u7ea7\u662f\u4fdd\u62a4\u7cfb\u7edf\u7684\u91cd\u8981\u624b\u6bb5\uff0c\u53ef\u4ee5\u9632\u6b62\u7cfb\u7edf\u56e0\u8fc7\u8f7d\u800c\u5d29\u6e83\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u5047\u8bbe\u6211\u4eec\u4f7f\u7528\u4e86Google Guava\u7684RateLimiter\u8fdb\u884c\u9650\u6d41\nimport com.google.common.util.concurrent.RateLimiter;\n\n@Service\npublic class RateLimitService {\n\n    private RateLimiter rateLimiter = RateLimiter.create(10.0); \/\/ \u6bcf\u79d2\u4e0d\u8d85\u8fc710\u4e2a\u4efb\u52a1\n\n    public void rateLimitedMethod() {\n        if (rateLimiter.tryAcquire()) {\n            \/\/ \u6267\u884c\u4efb\u52a1\n        } else {\n            \/\/ \u8fbe\u5230\u9650\u6d41\u6761\u4ef6\uff0c\u964d\u7ea7\u5904\u7406\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de76\uff1a\u4f7f\u7528WebFlux\u5b9e\u73b0\u975e\u963b\u585e\u5f0fIO<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Spring WebFlux\u662fSpring 5\u5f15\u5165\u7684\u53cd\u5e94\u5f0f\u7f16\u7a0b\u6846\u67b6\uff0c\u5b83\u652f\u6301\u975e\u963b\u585e\u5f0fIO\u64cd\u4f5c\uff0c\u9002\u7528\u4e8e\u9ad8\u5e76\u53d1\u573a\u666f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RestController\n@RequestMapping(\"\/api\")\npublic class WebFluxController {\n\n    @GetMapping(\"\/webflux\")\n    public Mono&lt;String> helloWebFlux() {\n        return Mono.just(\"Hello, WebFlux!\"); \/\/ \u8fd4\u56de\u4e00\u4e2a\u975e\u963b\u585e\u5f0f\u7684Mono\u5bf9\u8c61\n    }\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de77\uff1a\u4f18\u5316JPA\/Hibernate\u67e5\u8be2<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u4f7f\u7528JPA\u6216Hibernate\u65f6\uff0c\u4f18\u5316\u67e5\u8be2\u8bed\u53e5\u548c\u4f7f\u7528\u61d2\u52a0\u8f7d\u7279\u6027\u53ef\u4ee5\u51cf\u5c11\u6570\u636e\u5e93\u7684\u8d1f\u62c5\uff0c\u63d0\u5347\u6027\u80fd\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Entity\npublic class User {\n\n    @Id\n    @GeneratedValue(strategy = GenerationType.AUTO)\n    private Long id;\n\n    private String name;\n\n    \/\/ \u4f7f\u7528FetchType.LAZY\u5b9e\u73b0\u61d2\u52a0\u8f7d\n    @OneToMany(fetch = FetchType.LAZY)\n    private Set&lt;Order> orders;\n}<\/pre>\n\n\n\n<p><strong>\u6280\u5de78\uff1a\u4f7f\u7528\u6d88\u606f\u961f\u5217\u89e3\u8026\u548c\u5f02\u6b65\u5904\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6d88\u606f\u961f\u5217\u53ef\u4ee5\u5728\u7cfb\u7edf\u95f4\u8fdb\u884c\u89e3\u8026\u548c\u5f02\u6b65\u901a\u4fe1\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u7cfb\u7edf\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Service\npublic class MessageQueueService {\n\n    @Autowired\n    private JmsTemplate jmsTemplate;\n\n    public void sendMessage(String message) {\n        jmsTemplate.convertAndSend(\"queueName\", message); \/\/ \u53d1\u9001\u6d88\u606f\u5230\u961f\u5217\n    }\n}<\/pre>\n\n\n\n<p><strong>\u672c\u6587\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u4e0a\u8ff08\u4e2a\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u63d0\u5347SpringBoot\u5355\u4f53\u5e94\u7528\u7684\u5e76\u53d1\u5904\u7406\u80fd\u529b\u3002<br>\u8bb0\u5f97\uff0c\u5e76\u53d1\u4f18\u5316\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8c03\u6574\u548c\u4f18\u5316\u3002<br>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u7ed9\u4f60\u5e26\u6765\u5e2e\u52a9\uff0c\u8ba9\u4f60\u7684SpringBoot\u5e94\u7528\u5728\u9762\u5bf9\u9ad8\u5e76\u53d1\u6311\u6218\u65f6\u66f4\u52a0\u4ece\u5bb9\u4e0d\u8feb\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u563f\uff0c\u60f3\u8981\u8ba9\u4f60\u7684SpringBoot\u5e94\u7528\u5e76\u53d1\u6570\u66f4\u4e0a\u4e00\u5c42\u697c\u5417\uff1f \u90a3\u4f60\u6765\u5bf9\u5730\u65b9\u4e86\uff01 \u4eca\u5929\u6211\u4eec\u5c31\u6765\u804a\u804a\u5982\u4f55\u901a\u8fc7\u4e00\u4e9b\u7b80\u5355 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15402"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15402"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15402\/revisions"}],"predecessor-version":[{"id":15405,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/15402\/revisions\/15405"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}