{"id":28671,"date":"2025-04-20T02:26:26","date_gmt":"2025-04-19T18:26:26","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/28671.html"},"modified":"2025-04-20T02:26:26","modified_gmt":"2025-04-19T18:26:26","slug":"springboot%e9%9b%86%e6%88%90netty%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%8e%a5%e6%94%b6%e5%a4%a7%e9%87%8f%e6%95%b0%e6%8d%ae%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/28671.html","title":{"rendered":"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b"},"content":{"rendered":"<h3>\u4f9d\u8d56<\/h3>\n<p>&lt;dependency&gt; \u00a0 \u00a0 &lt;groupId&gt;io.netty&lt;\/groupId&gt; \u00a0 \u00a0 &lt;artifactId&gt;netty-all&lt;\/artifactId&gt; \u00a0 \u00a0 &lt;version&gt;4.1.68.Final&lt;\/version&gt; &lt;\/dependency&gt; \u00a0<\/p>\n<h3>\u521b\u5efaNetty\u670d\u52a1\u7c7b<\/h3>\n<p>&#064;Component<br \/>\n&#064;RequiredArgsConstructor<br \/>\npublic class NettyServerConfig {<\/p>\n<p>    \/\/ \u5b9a\u4e49\u670d\u52a1\u5668\u7aef\u53e3\u53f7<br \/>\n    private final int port &#061; 13030;<\/p>\n<p>    \/\/ \u6ce8\u5165JsonServerHandler\u5904\u7406\u5668<br \/>\n    private final JsonServerHandler jsonServerHandler;<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u65b9\u6cd5&#xff0c;\u5728Bean\u521b\u5efa\u65f6\u542f\u52a8Netty\u670d\u52a1\u5668<br \/>\n    &#064;PostConstruct<br \/>\n    public void startNetty() {<br \/>\n        new Thread(() -&gt; {<br \/>\n            \/\/ \u521b\u5efaboss\u548cworker\u4e24\u4e2a\u7ebf\u7a0b\u7ec4<br \/>\n            EventLoopGroup bossGroup &#061; new NioEventLoopGroup();<br \/>\n            EventLoopGroup workerGroup &#061; new NioEventLoopGroup();<br \/>\n            try {<br \/>\n                \/\/ \u521b\u5efaServerBootstrap\u5b9e\u4f8b\u4ee5\u914d\u7f6e\u670d\u52a1\u5668<br \/>\n                ServerBootstrap bootstrap &#061; new ServerBootstrap();<br \/>\n                bootstrap.group(bossGroup, workerGroup)<br \/>\n                        .channel(NioServerSocketChannel.class)  \/\/ \u6307\u5b9a\u4f7f\u7528NIO\u4f20\u8f93Channel<br \/>\n                        .childHandler(new ChannelInitializer&lt;SocketChannel&gt;() {<br \/>\n                            &#064;Override<br \/>\n                            protected void initChannel(SocketChannel ch) throws Exception {<br \/>\n                                \/\/ \u914d\u7f6e\u901a\u9053\u5904\u7406\u5668&#xff0c;\u5305\u62ec\u89e3\u7801\u5668\u3001\u7f16\u7801\u5668\u548c\u4e1a\u52a1\u5904\u7406\u5668<br \/>\n                                ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));<br \/>\n                                ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));<br \/>\n                                ch.pipeline().addLast(jsonServerHandler);  \/\/ \u4f7f\u7528\u6ce8\u5165\u7684JsonServerHandler<br \/>\n                            }<br \/>\n                        })<br \/>\n                        .option(ChannelOption.SO_BACKLOG, 128)  \/\/ \u8bbe\u7f6e\u961f\u5217\u5927\u5c0f<br \/>\n                        .childOption(ChannelOption.SO_KEEPALIVE, true);  \/\/ \u4fdd\u6301\u8fde\u63a5<\/p>\n<p>                \/\/ \u7ed1\u5b9a\u7aef\u53e3\u5e76\u542f\u52a8\u670d\u52a1\u5668<br \/>\n                ChannelFuture f &#061; bootstrap.bind(port).sync();<br \/>\n                \/\/ \u7b49\u5f85\u670d\u52a1\u5668\u901a\u9053\u5173\u95ed<br \/>\n                f.channel().closeFuture().sync();<br \/>\n            } catch (InterruptedException e) {<br \/>\n                e.printStackTrace();<br \/>\n            } finally {<br \/>\n                \/\/ \u4f18\u96c5\u5730\u5173\u95ed\u7ebf\u7a0b\u7ec4<br \/>\n                workerGroup.shutdownGracefully();<br \/>\n                bossGroup.shutdownGracefully();<br \/>\n            }<br \/>\n        }).start();<br \/>\n    }<br \/>\n} <\/p>\n<h3>\u521b\u5efaEchoServerHandler \u7c7b\u63a5\u6536\u5e76\u5b58\u50a8\u6570\u636e<\/h3>\n<p>&#064;Slf4j<br \/>\n&#064;ChannelHandler.Sharable<br \/>\n&#064;Component<br \/>\npublic class JsonServerHandler extends SimpleChannelInboundHandler&lt;String&gt; {<br \/>\n    &#064;Autowired<br \/>\n    private ReceptionMapper receptionMapper;<\/p>\n<p>    private final ObjectMapper objectMapper &#061; new ObjectMapper();<\/p>\n<p>    \/\/ \u5904\u7406\u63a5\u6536\u5230\u7684\u6d88\u606f<br \/>\n    &#064;Override<br \/>\n    &#064;Transactional<br \/>\n    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {<br \/>\n        try {<br \/>\n            \/\/ \u53cd\u5e8f\u5217\u5316\u63a5\u6536\u5230\u7684JSON\u6d88\u606f<br \/>\n            ReceptionData receptionData &#061; null;<br \/>\n            try {<br \/>\n                receptionData &#061; objectMapper.readValue(msg, ReceptionData.class);<br \/>\n            } catch (JsonProcessingException e) {<br \/>\n                log.error(&#034;\u89e3\u7801\u6570\u636e\u9519\u8bef&#034;, e);<br \/>\n            }<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u5b58\u5165\u6570\u636e\u5e93<br \/>\n            receptionMapper.saveData(receptionData );<\/p>\n<p>        } catch (Exception e) {<br \/>\n            log.error(&#034;\u63a5\u6536\u6570\u636e\u9519\u8bef&#034;, e);<br \/>\n        }<br \/>\n    }<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb881\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf6\u6b21\u3002\/ \u4f7f\u7528\u6ce8\u5165\u7684JsonServerHandler\u3002.channel(NioServerSocketChannel.class)  \/\/ \u6307\u5b9a\u4f7f\u7528NIO\u4f20\u8f93Channel\u3002.option(ChannelOption.SO_BACKLOG, 128)  \/\/ \u8bbe\u7f6e\u961f\u5217\u5927\u5c0f\u3002\/\/ \u6ce8\u5165JsonServerHandler\u5904\u7406\u5668\u3002\/\/ \u7ed1\u5b9a\u7aef\u53e3\u5e76\u542f\u52a8\u670d\u52a1\u5668\u3002\/\/ \u53cd\u5e8f\u5217\u5316\u63a5\u6536\u5230\u7684JSON\u6d88\u606f\u3002\/\/ \u7b49\u5f85\u670d\u52a1\u5668\u901a\u9053\u5173\u95ed\u3002\/\/ \u4f18\u96c5\u5730\u5173\u95ed\u7ebf\u7a0b\u7ec4\u3002\/\/ \u5b9a\u4e49\u670d\u52a1\u5668\u7aef\u53e3\u53f7\u3002\/\/ \u5904\u7406\u63a5\u6536\u5230\u7684\u6d88\u606f\u3002_springboot+netty\u63a5\u6536\u8bbe\u5907\u4fe1\u606f<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[386,99,1133,98,2312,43],"topic":[],"class_list":["post-28671","post","type-post","status-publish","format-standard","hentry","category-server","tag-intellij-idea","tag-java","tag-servlet","tag-spring-boot","tag-2312","tag-43"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/28671.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb881\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf6\u6b21\u3002\/ \u4f7f\u7528\u6ce8\u5165\u7684JsonServerHandler\u3002.channel(NioServerSocketChannel.class) \/\/ \u6307\u5b9a\u4f7f\u7528NIO\u4f20\u8f93Channel\u3002.option(ChannelOption.SO_BACKLOG, 128) \/\/ \u8bbe\u7f6e\u961f\u5217\u5927\u5c0f\u3002\/\/ \u6ce8\u5165JsonServerHandler\u5904\u7406\u5668\u3002\/\/ \u7ed1\u5b9a\u7aef\u53e3\u5e76\u542f\u52a8\u670d\u52a1\u5668\u3002\/\/ \u53cd\u5e8f\u5217\u5316\u63a5\u6536\u5230\u7684JSON\u6d88\u606f\u3002\/\/ \u7b49\u5f85\u670d\u52a1\u5668\u901a\u9053\u5173\u95ed\u3002\/\/ \u4f18\u96c5\u5730\u5173\u95ed\u7ebf\u7a0b\u7ec4\u3002\/\/ \u5b9a\u4e49\u670d\u52a1\u5668\u7aef\u53e3\u53f7\u3002\/\/ \u5904\u7406\u63a5\u6536\u5230\u7684\u6d88\u606f\u3002_springboot+netty\u63a5\u6536\u8bbe\u5907\u4fe1\u606f\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/28671.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-19T18:26:26+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/28671.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/28671.html\",\"name\":\"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-19T18:26:26+00:00\",\"dateModified\":\"2025-04-19T18:26:26+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/28671.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/28671.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/28671.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/28671.html","og_locale":"zh_CN","og_type":"article","og_title":"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb881\u6b21\uff0c\u70b9\u8d5e12\u6b21\uff0c\u6536\u85cf6\u6b21\u3002\/ \u4f7f\u7528\u6ce8\u5165\u7684JsonServerHandler\u3002.channel(NioServerSocketChannel.class) \/\/ \u6307\u5b9a\u4f7f\u7528NIO\u4f20\u8f93Channel\u3002.option(ChannelOption.SO_BACKLOG, 128) \/\/ \u8bbe\u7f6e\u961f\u5217\u5927\u5c0f\u3002\/\/ \u6ce8\u5165JsonServerHandler\u5904\u7406\u5668\u3002\/\/ \u7ed1\u5b9a\u7aef\u53e3\u5e76\u542f\u52a8\u670d\u52a1\u5668\u3002\/\/ \u53cd\u5e8f\u5217\u5316\u63a5\u6536\u5230\u7684JSON\u6d88\u606f\u3002\/\/ \u7b49\u5f85\u670d\u52a1\u5668\u901a\u9053\u5173\u95ed\u3002\/\/ \u4f18\u96c5\u5730\u5173\u95ed\u7ebf\u7a0b\u7ec4\u3002\/\/ \u5b9a\u4e49\u670d\u52a1\u5668\u7aef\u53e3\u53f7\u3002\/\/ \u5904\u7406\u63a5\u6536\u5230\u7684\u6d88\u606f\u3002_springboot+netty\u63a5\u6536\u8bbe\u5907\u4fe1\u606f","og_url":"https:\/\/www.wsisp.com\/helps\/28671.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-19T18:26:26+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/28671.html","url":"https:\/\/www.wsisp.com\/helps\/28671.html","name":"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-19T18:26:26+00:00","dateModified":"2025-04-19T18:26:26+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/28671.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/28671.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/28671.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"SpringBoot\u96c6\u6210Netty\u670d\u52a1\u5668\u63a5\u6536\u5927\u91cf\u6570\u636e\u5b9e\u4f8b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/28671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=28671"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/28671\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=28671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=28671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=28671"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=28671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}