{"id":10008196,"date":"2023-08-30T12:01:12","date_gmt":"2023-08-30T04:01:12","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008196"},"modified":"2023-08-30T12:01:12","modified_gmt":"2023-08-30T04:01:12","slug":"java-nio-%e5%9b%be%e8%a7%a3-netty-%e6%9c%8d%e5%8a%a1%e7%ab%af%e5%90%af%e5%8a%a8%e7%9a%84%e8%bf%87%e7%a8%8b-%e4%ba%ac%e4%b8%9c%e4%ba%91%e6%8a%80%e6%9c%af%e5%9b%a2%e9%98%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008196","title":{"rendered":"Java NIO \u56fe\u89e3 Netty \u670d\u52a1\u7aef\u542f\u52a8\u7684\u8fc7\u7a0b | \u4eac\u4e1c\u4e91\u6280\u672f\u56e2\u961f"},"content":{"rendered":"<h2>\u4e00.\u542f\u52a8\u6982\u8ff0<\/h2>\n<p>\u4e86\u89e3\u6574\u4f53Netty\u5e38\u7528\u7684\u6838\u5fc3\u7ec4\u4ef6\u540e\uff0c\u5e76\u4e14\u5bf9\u6bd4\u4e86\u4f20\u7edfIO\u6a21\u5f0f\u3002\u5728\u5bf9\u6bd4\u8fc7\u7a0b\u4e2d\uff0c\u627e\u5230\u4e86\u4f20\u7edfIO\u5bf9\u5e94Netty\u4e2d\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002\u6700\u540e\u6211\u4eec\u4e86\u89e3\u5230\u5728netty\u4e2d\u5e38\u7528\u7684\u90a3\u4e9b\u7ec4\u4ef6\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164516\" alt=\"\" title=\"\">  <br \/>\u672c\u6587\u5728\u4e86\u89e3\u4e0b\u8fd9\u4e9b\u6838\u5fc3\u7ec4\u4ef6\u7684\u524d\u63d0\u4e0b\uff0c\u8fdb\u4e00\u6b65\u4e86\u89e3\u7ec4\u4ef6\u5982\u4f55\u5728\u6574\u4e2a\u670d\u52a1\u5668\u542f\u52a8\u8fc7\u7a0b\u5982\u4f55\u88ab\u521b\u5efa\uff0c\u5982\u4f55\u7ec4\u4ef6\u4e4b\u95f4\u914d\u5408\u6765\u4f7f\u7528\u3002\u9996\u5148\u4e5f\u662f\u5148\u4e86\u89e3\u4e0b\u5927\u6982\u670d\u52a1\u7aef\u7684\u542f\u52a8\u8fc7\u7a0b\uff0c\u5e76\u4e14\u5728\u4e86\u89e3\u8fc7\u7a0b\u4e2d\u6211\u4eec\u5e26\u7740\u81ea\u5df1\u7684\u95ee\u9898\u53bb\u5728\u5b66\u4e60\u8fc7\u7a0b\u4e2d\u63a2\u5bfb\u7b54\u6848\u3002<\/p>\n<h4>1.1 \u542f\u52a8\u6982\u8ff0<\/h4>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164517\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>1.2 \u542f\u52a8\u95ee\u9898<\/h4>\n<ol>\n<li>netty\u670d\u52a1\u7aef\u542f\u52a8\u662f\u5982\u4f55\u8bbe\u7f6e\u975e\u963b\u585e\u6a21\u5f0f\u7684\uff1f<\/li>\n<li>\u670d\u52a1\u7aef\u542f\u52a8\u540e\u4e8b\u4ef6\u662f\u5982\u4f55\u6ce8\u518c\u5230selector\u4e0a\uff1f<\/li>\n<\/ol>\n<h2>\u4e8c.\u542f\u52a8\u8be6\u8ff0<\/h2>\n<h4>2.1 channel \u521b\u5efa<\/h4>\n<p>\u8fd8\u662f\u4e00\u6837\u9996\u5148\u5728channel\u521b\u5efa\u8fc7\u7a0b\u5927\u6982\u6709\u54ea\u4e9b\u8fc7\u7a0b<\/p>\n<ul>\n<li>bind<\/li>\n<li>initAndRegister<\/li>\n<li>\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521b\u5efachannel<\/li>\n<\/ul>\n<p>\u5177\u4f53\u8c03\u7528\u5173\u7cfb  <br \/><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164518\" alt=\"\" title=\"\" loading=\"lazy\">  <br \/>\u65f6\u5e8f\u56fe\u4e2d\u4ece1,2,3\u6b65\u90fd\u597d\u7406\u89e3\u3002<\/p>\n<h5>2.1.1 \u521b\u5efachannelFactory<\/h5>\n<p>\u4ece\u7c7b\u7684\u53cd\u5c04\u5f97\u5230channel\u8fd9\u91cc\u662f\u4e00\u4e2a\u5173\u952e\u70b9\u9700\u8981\u8bf4\u660e\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164519\" alt=\"\" title=\"\" loading=\"lazy\">  <br \/>\u56fe\u4e2d\u76f4\u63a5\u4f7f\u7528channelFactory\u6765\u5b9e\u73b0\u4e86channel\u7684\u5b9e\u4f8b\u5316.\u90a3\u4e48\u5c31\u6309\u56fe\u7d22\u9aa5\u8fd9\u4e2achannelFactory\u662f\u4ec0\u4e48\u65f6\u5019\u8d4b\u503c\u7684\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164520\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u56fe\u4e2d\u6211\u4eec\u4e00\u6b65\u6b65\u627e\u5230channelFactory\u7684\u8def\u5f84\u3002\u6211\u4eec\u518d\u770b\u7b2c\u4e09\u6b65\u662f\u8c01\u8c03\u7528\u4e86  <br \/>channel(Class&lt;? extends C&gt; channelClass) \u521b\u5efa\u4e86channelFactory\u3002\u6700\u540e\u53d1\u73b0\u6211\u4eec\u5728bootstrap\u8bbe\u7f6echannel\u5c5e\u6027\u7684\u65f6\u5019\u8bbe\u7f6e\u4e86channelFactory\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164521\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8d70\u5230\u8fd9\u91cc\u6211\u4eec\u624d\u771f\u6b63\u8fdb\u5165\u77e5\u9053\u5728bootstrap\u8bbe\u7f6eNioServerSocketChannel\u3002\u5e76\u5229\u7528NioServerSocketChannel\u521b\u5efa\u3002  <br \/><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164522\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0a\u8ff0\u6b65\u9aa4\u6211\u4eec\u5c0f\u7ed3\u4e00\u4e0b\u3002\u901a\u8fc7\u8bbe\u7f6ebootstrap\u7684Channel\u5c5e\u6027\u6765\u8bbe\u7f6e\u670d\u52a1\u7aefNioServerSocketChannel\u751f\u6210\u4e86channelFactory\u3002channelFactory \u6765\u4e3b\u52a8\u8c03\u7528\u4f20\u5165\u7684class\u6765\u6784\u9020channel\u3002<\/p>\n<h5>2.2.2 NioServerSocketChannel\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521b\u5efaChannel<\/h5>\n<p>\u4ece\u4e0a\u56fe\u4e2d\u6211\u4eec\u77e5\u9053\u8be5\u6784\u9020\u5de5\u5382\u662f\u8c03\u7528\u7c7b\u7684\u6784\u9020\u51fd\u6570\u6765\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u901a\u8fc7\u4ee3\u7801\u4e2d\u6211\u4eec\u77e5\u9053\u6784\u9020\u7c7b\u4e3aNioServerSocketChannel\u3002\u90a3\u4e48\u5c31\u770b\u4e0bNioServerSocketChannel\u6784\u9020\u8fc7\u7a0b\u3002<\/p>\n<p>\u5148\u4e0a\u6765\u8fd8\u662f\u770b\u4e0b\u6574\u4f53\u6b65\u9aa4\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164523\" alt=\"\" title=\"\" loading=\"lazy\">  <br \/>\u901a\u8fc7\u9ed8\u8ba4\u7684newSocket\u65b9\u6cd5\u521b\u5efa\u4e86jdk\u5e95\u5c42\u7684channel\uff0c\u7136\u540e\u901a\u8fc7Channel\u7684\u914d\u7f6e\u4e86channel\u7684id\uff0c\u5bf9\u5e94channel\u5e95\u5c42\u7684\u8bfb\u5199unsafe\u7ec4\u4ef6\uff0cchannel\u5bf9\u5e94\u7684\u903b\u8f91\u5904\u7406pipeline\uff0c  <br \/>\u6700\u540e\u901a\u8fc7configureBlocking\u8bbe\u7f6echannel\u4e3a\u975e\u963b\u585e\u6a21\u5f0f\u3010\u56de\u7b54\u4e86\u6211\u4eec\u7b2c\u4e00\u4e2a\u95ee\u9898\u3011\u3002  <br \/>\u7b2c\u4e09\u6b65\uff1a\u901a\u8fc7channelConfig\u8bbe\u7f6e\u4e00\u4e9btcp\u5c42\u9762\u7684\u8bbe\u7f6e<\/p>\n<p>\u81f3\u6b64channel\u521b\u5efa\u5b8c\u6210<\/p>\n<h4>2.2 channel \u521d\u59cb\u5316<\/h4>\n<p>\u901a\u8fc7\u4e0a\u97622.1 \u6574\u4e2achannel\u5df2\u7ecf\u521b\u5efa\u5b8c\u6210\u3002\u7b2c\u4e8c\u5927\u6b65\u5728\u521b\u5efachannel\u7684\u57fa\u7840\u4e0a\uff0c\u7ed9channel\u505a\u4e00\u4e9b\u5c5e\u6027\u914d\u7f6e\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164524\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164525\" alt=\"\" title=\"\" loading=\"lazy\">  <br \/>\u4e0a\u56fe\u8bbe\u7f6e\u7528\u7684\u5c5e\u6027 \u5bf9\u5e94\u5230\u4ee3\u7801\u4e2d \u4e3a\u5982\u4e0b\u7684\u4ee3\u7801<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164526\" alt=\"\" title=\"\" loading=\"lazy\">  <br \/>\u8fd9\u91cc\u5747\u4e3a\u4e00\u4e9b\u81ea\u5df1\u7684\u5c5e\u6027\u548c\u914d\u7f6e\u7684\u8bbe\u7f6e\uff0c\u81f3\u6b64channel\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\u5b8c\u6210\u3002\u8fd9\u91cc\u7684\u914d\u7f6e\u7528\u4ee5\u5ba2\u6237\u7aef\u65b0\u5efa\u8fde\u63a5\u65f6\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<h4>2.3 selector \u6ce8\u518c<\/h4>\n<p>\u8fd8\u662f\u5148\u6574\u4f53\u770b\u4e0b \u6574\u4e2a selector \u6d41\u7a0b\uff0c\u662f\u5982\u4f55\u5c06channel\u6ce8\u518c\u5230selector\u4e0a\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164527\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>ps:\u4ee5\u4e0a\u4ee3\u7801\u6d41\u7a0b\u9700\u8981\u4e00\u5c42\u5c42\u901a\u8fc7\u65ad\u70b9\u65b9\u5f0f\u8fdb\u884c\u8ddf\u8e2a\u3002<\/p>\n<p>\u7b2c\u4e00\u6b65\uff1aAbstractBootStrap register \u5165\u53e3<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164528\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4ee5\u4e0a\u7684\u6b65\u9aa4\u5b8c\u6210channel\u7684\u521b\u5efa\u548c\u521d\u59cb\u5316\uff0c\u901a\u8fc7initAndRegister\u5185\u90e8\u65b9\u6cd5\u6765\u5b9e\u73b0selector\u6302\u8f7d.<\/p>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u8c03\u7528<\/p>\n<p>io.netty.channel.MultithreadEventLoopGroup#register(io.netty.channel.Channel)<\/p>\n<p>register\u65b9\u6cd5\u4e2d\u901a\u8fc7next\u65b9\u6cd5\u83b7\u53d6\u4e0b\u4e00\u4e2a\u53ef\u4ee5\u8c03\u7528EventLoop\u6765\u8c03\u7528\u5b83\u7684register\u65b9\u6cd5<\/p>\n<p>\u7b2c\u4e09\u6b65\uff1a\u8c03\u7528<\/p>\n<p>io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.Channel)<\/p>\n<p>\u8c03\u52a8channel.unsafe.register \u65b9\u6cd5\u83b7\u53d6channel\u5e95\u5c42\u64cd\u4f5c\uff0c\u5e76\u8c03\u7528unsafe\u7684register\u65b9\u6cd5<\/p>\n<p>\u7b2c\u56db\u6b65:\u901a\u8fc7\u8c03\u7528abstract Nio Channel \u5c06channel\u6ce8\u518c\u5230eventLoop\u7684selector\u4e2d\u3010\u7b2c\u4e8c\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u3011\uff0c\u5e76\u5c06\u5f53\u524dchannel\u4f5c\u4e3aattache\u4e0esocket channel\u5173\u8054\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164529\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5b8c\u6210\u5bf9\u5e94\u7684selector \u548c channel \u7ed1\u5b9a\u4e4b\u540e\u5982\u679c\u6709\u76f8\u5e94\u7684\u4e8b\u4ef6\u56de\u8c03\u4f1a\u8fdb\u884c\u4e8b\u4ef6\u56de\u8c03\u64cd\u4f5c\uff0c\u8fd9\u91cc\u9700\u8981\u7684\u8bdd\u9700\u8981\u7ee7\u627fChannelInboundHandlerAdapter\u4e2d\u5bf9\u5e94\u7684\u65b9\u6cd5channelRegistered\uff0c channelActive\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164530\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u81f3\u6b64nio channel \u901a\u8fc7java \u5e95\u5c42\u7684socketchannel\u7ed1\u5b9a \u5230\u6307\u5b9a\u7684selector \uff0c\u5b8c\u6210selector\u4e0eniochannel \u7684\u6ce8\u518c\u8fc7\u7a0b\u3002<\/p>\n<h4>2.4 \u7aef\u53e3\u7ed1\u5b9a<\/h4>\n<p>\u57fa\u672c\u6b65\u9aa4<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164531\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164532\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7b2c\u4e00\u6b65\uff1a\u5e95\u5c42\u7aef\u53e3channel\u7ed1\u5b9a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164533\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u6700\u7ec8\u8c03\u7528AbstractNioChannel doBeginRead\u65b9\u6cd5\u3002<\/p>\n<p>\u6fc0\u6d3bchannel \u5e76\u5411selector \u6ce8\u518c read \u4e8b\u4ef6\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044164534\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2>\u4e09 .\u603b\u7ed3<\/h2>\n<p>\u6574\u4f53\u4e86\u89e3\u5230netty\u670d\u52a1\u7aef\u7684\u542f\u52a8\u8fc7\u7a0b\u3002<\/p>\n<ol>\n<li>\u901a\u8fc7NioServerSocketChannel\u7684channelFactory\u521b\u5efachannelFactory\u65b9\u6cd5<\/li>\n<li>channelFactory \u901a\u8fc7\u53cd\u5c04\u8c03\u7528nioServerSocketChannel\u6784\u9020\u51fd\u6570\u3002\u521b\u5efachannel\u5bf9\u8c61<\/li>\n<li>channel\u901a\u8fc7NioServerSocketChannelConfig\u5c06\u81ea\u5b9a\u4e49\u7684option\uff0cattr\uff0chandler\u8bbe\u7f6e\u5b8c\u6210\u3002<\/li>\n<li>abstract Nio Channel\u8c03\u7528register\u65b9\u6cd5\u5b9e\u73b0selector\u548cchannel\u7684\u7ed1\u5b9a<\/li>\n<li>\u6700\u7ec8\u8c03\u7528java channel \u8fdb\u884c\u7aef\u53e3\u7ed1\u5b9a\u5e76\u5411selector \u6ce8\u518cread\u4e8b\u4ef6<\/li>\n<\/ol>\n<p>\u6574\u4f53netty\u670d\u52a1\u7aef\u542f\u52a8\u5b8c\u6210\u3002<\/p>\n<blockquote>\n<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u79d1\u6280 \u9648\u65b9\u6797<\/p>\n<p>\u6765\u6e90\uff1a\u4eac\u4e1c\u4e91\u5f00\u53d1\u8005\u793e\u533a \u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u6e90<\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044164514\">https:\/\/segmentfault.com\/a\/1190000044164514<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00.\u542f\u52a8\u6982\u8ff0 \u4e86\u89e3\u6574\u4f53Netty\u5e38\u7528\u7684\u6838\u5fc3\u7ec4\u4ef6\u540e\uff0c\u5e76\u4e14\u5bf9\u6bd4\u4e86\u4f20\u7edfIO\u6a21\u5f0f\u3002\u5728\u5bf9\u6bd4\u8fc7\u7a0b\u4e2d\uff0c\u627e\u5230\u4e86\u4f20\u7edfIO\u5bf9\u5e94Ne [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008196","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008196","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"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008196"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008196\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}