text: "use strict";(self.webpackChunkvblog=self.webpackChunkvblog||[]).push([[237],{3901:(n,s,a)=>{a.r(s),a.d(s,{data:()=>p});const p={key:"v-d489b2be",path:"/backend/base/netty.html",title:"Netty",lang:"zh-CN",frontmatter:{},excerpt:"",headers:[{level:2,title:"Handler&编解码器",slug:"handler-编解码器",children:[]},{level:2,title:"Protocal Buffers",slug:"protocal-buffers",children:[]},{level:2,title:"Grpc",slug:"grpc",children:[]}],filePathRelative:"backend/base/netty.md",git:{updatedTime:1639984814e3,contributors:[{name:"bootvue",email:"bootvue@gmail.com",commits:1}]}}},7883:(n,s,a)=>{a.r(s),a.d(s,{default:()=>t});const p=(0,a(6252).uE)('<h1 id="netty" tabindex="-1"><a class="header-anchor" href="#netty" aria-hidden="true">#</a> Netty</h1><h2 id="handler-编解码器" tabindex="-1"><a class="header-anchor" href="#handler-编解码器" aria-hidden="true">#</a> Handler&amp;编解码器</h2><p><code>IdleStateHandler: 读写空闲检测</code><code>HttpObjectAggregator</code><code>HttpServerCodec</code><code>ChunkedWriteHandler</code><code>WebSocketServerProtocalHandler</code><code>ProtobufVarint32FrameDecoder</code><code>ProtobufDecoder</code><code>ProtobufVarint32LengthFieldPrepender</code><code>protobufEncoder</code></p><blockquote><p>ByteToMessageDecoder MessageToByteEncoder MessageToMessageDecoder MessageToMessageEncoder ReplayingDecoder FiexedLengthFrameDecoder DelimiterBasedFramedDecoder LengthFieldBasedFrameDecoder</p></blockquote><h2 id="protocal-buffers" tabindex="-1"><a class="header-anchor" href="#protocal-buffers" aria-hidden="true">#</a> Protocal Buffers</h2><div class="language-protobuf ext-protobuf line-numbers-mode"><pre class="language-protobuf"><code><span class="token keyword">syntax</span><span class="token operator">=</span><span class="token string">&quot;proto2&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">package</span>  xxx<span class="token punctuation">;</span>\n<span class="token keyword">option</span> java_package<span class="token operator">=</span><span class="token string">&quot;xxxx&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">option</span> java_outer_classname<span class="token operator">=</span><span class="token string">&quot;xxxx&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">option</span> optimize_for <span class="token operator">=</span> SPEED<span class="token punctuation">;</span><span class="token comment">// CODE_SIZE LITE_TUNTIME</span>\n\n<span class="token keyword">message</span> <span class="token class-name">Person</span><span class="token punctuation">{</span>\n    <span class="token keyword">required</span> <span class="token positional-class-name class-name">String</span> name<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>\n    <span class="token keyword">required</span> <span class="token builtin">int32</span> age<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>\n    <span class="token keyword">optional</span> <span class="token positional-class-name class-name">String</span> address<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>\n    <span class="token keyword">repeated</span> phones<span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">;</span>\n    <span class="token map class-name">map<span class="token punctuation">&lt;</span><span class="token builtin">string</span><span class="token punctuation">,</span> Project<span class="token punctuation">&gt;</span></span> projects <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">;</span>\n\n\n    <span class="token keyword">enum</span> <span class="token class-name">xx_type</span> <span class="token punctuation">{</span>\n        IPHONE<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>\n        HUAWEI<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">oneof</span> data_bofy <span class="token punctuation">{</span> <span class="token comment">// oneof 同时只有一个字段生效</span>\n        <span class="token positional-class-name class-name">XX</span> xx <span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">;</span>\n        <span class="token positional-class-name class-name">YY</span> yy<span class="token operator">=</span><span class="token number">6</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">message</span> <span class="token class-name">xx</span><span class="token punctuation">{</span>\n        <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><blockquote><p>protobuf-java protobuf-lite protobuf-util</p></blockquote><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>protoc --java_out<span class="token operator">=</span>src/main/java src/xxxxx.ptoto\n</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><h2 id="grpc" tabindex="-1"><a class="header-anchor" href="#grpc" aria-hidden="true">#</a> Grpc</h2><blockquote><p>proto3 request/stream request &lt;----&gt; response/stream response</p></blockquote><div class="language-protobuf ext-protobuf line-numbers-mode"><pre class="language-protobuf"><code><span class="token keyword">syntax</span> <span class="token operator">=</span> <span class="token string">&quot;proto3&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">import</span> <span class="token string">&quot;google/protobuf/empty.proto&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">package</span> xxxx<span class="token punctuation">;</span>\n<span class="token keyword">option</span> java_package <span class="token operator">=</span> <span class="token string">&quot;com.xxxxxx.proto&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">option</span> java_outer_classname <span class="token operator">=</span> <span class="token string">&quot;HHH&quot;</span><span class="token punctuation">;</span>\n<span class="token keyword">option</span> optimize_for <span class="token operator">=</span> SPEED<span class="token punctuation">;</span>\n<span class="token keyword">option</span> java_multiple_files <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>\n\n<span class="token keyword">service</span> <span class="token class-name">xx</span> <span class="token punctuation">{</span>\n    <span class="token keyword">rpc</span> <span class="token function">Test</span><span class="token punctuation">(</span>Request xx<span class="token punctuation">)</span> <span class="token keyword">returns</span> <span class="token punctuation">(</span><span class="token class-name">Response</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token keyword">message</span> <span class="token class-name">Request</span><span class="token punctuation">{</span>\n    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>\n<span class="token punctuation">}</span>\n\n<span class="token keyword">message</span> <span class="token class-name">Response</span><span class="token punctuation">{</span>\n    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>\n<span class="token punctuation">}</span>\n</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><div class="custom-container tip"><p class="custom-container-title">TIP</p><p>类型</p></div><div class="language-bash ext-sh line-numbers-mode"><pre class="language-bash"><code>float double int32 bool string bytes sint64 map<span class="token punctuation">..</span>.\n</code></pre><div class="line-numbers"><span class="line-number">1</span><br></div></div><p>java:</p><div class="language-java ext-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">extends</span> <span class="token class-name">XxxServiceGrpc<span class="token punctuation">.</span>XxxServiceImplBase</span><span class="token punctuation">{</span>\n<span class="token comment">// 重写 ptoto中定义的rpc方法</span>\n<span class="token punctuation">}</span>\n\n<span class="token class-name">StreamObserver</span>\n\n<span class="token comment">// Server</span>\n\n<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">GrpcServer</span> <span class="token punctuation">{</span>\n\n    <span class="token keyword">private</span> <span class="token class-name">Server</span> server<span class="token punctuation">;</span>\n\n    <span class="token annotation punctuation">@PostConstruct</span>\n    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>\n        server <span class="token operator">=</span> <span class="token class-name">ServerBuilder</span><span class="token punctuation">.</span><span class="token function">forPort</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addService</span><span class="token punctuation">(</span>xxxxxService<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n        <span class="token class-name">Runtime</span><span class="token punctuation">.</span><span class="token function">getRuntime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addShutdownHook</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token class-name">GrpcServer</span><span class="token punctuation">.</span><span class="token keyword">this</span><span class="token operator">::</span><span class="token function">stop</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n        <span class="token function">block</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token keyword">if</span> <span class="token punctuation">(</span>server <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n            server<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n        <span class="token punctuation">}</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">private</span> <span class="token keyword">void</span> <span class="token function">block</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">InterruptedException</span> <span class="token punctuation">{</span>\n        <span class="token keyword">if</span> <span class="token punctuation">(</span>server <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n            server<span class="token punctuation">.</span><span class="token function">awaitTermination</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n        <span class="token punctuation">}</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token comment">// client</span>\n<span class="token class-name">ManagedChannel</span> channel <span class="token operator">=</span> <span class="token class-name">ManagedChannelBuilder</span><span class="token punctuation">.</span><span class="token function">forAddress</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">)</span>\n                <span class="token punctuation">.</span><span class="token function">enableRetry</span><span class="token punctuation">(</span><span class="token punctuation">)</span>\n                <span class="token punctuation">.</span><span class="token function">usePlaintext</span><span class="token punctuation">(</span><span class="token punctuation">)</span>\n                <span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n        <span class="token class-name">Runtime</span><span class="token punctuation">.</span><span class="token function">getRuntime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addShutdownHook</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-&gt;</span> <span class="token punctuation">{</span>\n            <span class="token keyword">if</span> <span class="token punctuation">(</span>channel <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n                channel<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n            <span class="token punctuation">}</span>\n        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n        xxxxGrpc<span class="token punctuation">.</span>xxxBlockingStub blockingStub <span class="token operator">=</span> xxxxGrpc<span class="token punctuation">.</span><span class="token function">newBlockingStub</span><span class="token punctuation">(</span>channel<span class="token punctuation">)</span><span class="token punctuation">;</span>\n</code></pre><div class="line-numbers"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br></div></div>',15),e={},t=(0,a(3744).Z)(e,[["render",function(n,s){return p}]])},3744:(n,s)=>{s.Z=(n,s)=>{for(const[a,p]of s)n[a]=p;return n}}}]);