zoukankan      html  css  js  c++  java
  • 精选 Dubbo RPC 面试题,比较全面,含答案

                <h2 class="rich_media_title" id="activity-name">
                    
                    
                    
            精选 Dubbo RPC 面试题,比较全面,含答案
                      </h2>
                <div id="meta_content" class="rich_media_meta_list">
                                                                                                            <span class="rich_media_meta rich_media_meta_text">
                                                        hu1991die
                                                    </span>
                                                                
                                        <span class="rich_media_meta rich_media_meta_nickname" id="profileBt">
                      <a id="js_name">
                        搜云库技术团队                      </a>
                      <div id="js_profile_qrcode" class="profile_container" style="display:none;">
                          <div class="profile_inner">
                              <strong class="profile_nickname">搜云库技术团队</strong>
                              <img class="profile_avatar" id="js_profile_qrcode_img" src="" alt="">
    
                              <p class="profile_meta">
                              <label class="profile_meta_label">微信号</label>
                              <span class="profile_meta_value">souyunku</span>
                              </p>
    
                              <p class="profile_meta">
                              <label class="profile_meta_label">功能介绍</label>
                              <span class="profile_meta_value">专注于分享最有价值的互联网技术干货文章,内容覆盖,Java后端、微服务、分布式、大数据、区块链、容器化编程、等技术栈。网站:https://tech.souyunku.com</span>
                              </p>
                              
                          </div>
                          <span class="profile_arrow_wrp" id="js_profile_arrow_wrp">
                              <i class="profile_arrow arrow_out"></i>
                              <i class="profile_arrow arrow_in"></i>
                          </span>
                      </div>
                    </span>
    
    
                    <em id="publish_time" class="rich_media_meta rich_media_meta_text">昨天</em>
    
    
    
    
    
                </div>
    
                
                
                                
                
                
                
                                                
                                                                
                                
                
                <div class="rich_media_content " id="js_content">
                    
    
                    
    
                    
                    
                    <p style="max- 100%;min-height: 1em;white-space: normal;text-align: center;font-size: 16px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"><br style="max- 100%;overflow-wrap: break-word !important;box-sizing: border-box !important;"></p><p style="margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">RPC非常重要,很多人面试的时候都挂在了这个地方!你要是还不懂RPC是什么?他的基本原理是什么?你一定要把下边的内容记起来!好好研究一下!特别是文中给出的一张关于RPC的基本流程图,重点中的重点,Dubbo RPC的基本执行流程就是他,RPC框架的基本原理也是他,别说我没告诉你!看了下边的内容你要掌握的内容如下,当然还有很多:</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">1、</strong>RPC的由来,是怎样一步步演进出来的;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2、</strong>RPC的基本架构是什么;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">3、</strong>RPC的基本实现原理,就是下边的这张图,重点中的重点;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">4、</strong>REST 和 SOAP、RPC 有何区别呢?</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">5、</strong>整个调用的过程经历了哪几步和Spring MVC的执行流程一样,相当重要;</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">一、为什么要有RPC</h2><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><img class="" data-ratio="0.3" data-src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125145964-2000793398.png" data-type="jpeg" data-w="700" style="border- 0px; border-style: initial; border-color: initial; border-radius: 6px;  677px !important; height: auto !important; visibility: visible !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125340747-732605036.png" crossorigin="anonymous" data-fail="0"></p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">1、单一应用架构</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本。此时,用于简化增删改查工作量的数据访问框架(ORM) 是关键<span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 14px;letter-spacing: 0.544px;text-align: start;white-space: pre-line;background-color: rgb(255, 255, 255);">。整编:微信公众号,搜云库技术团队,ID:souyunku</span></p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2、垂直应用架构</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">当访问量逐渐增大,单一应用增加机器带来的加速度越来越小,将应用拆成互不相干的几个应用,以提升效率。此时,用于加速前端页面开发的 Web框架(MVC) 是关键。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><mpcpc js_editor_cpcad="" class="" src="/cgi-bin/readtemplate?t=tmpl/cpc_tmpl#1567334079339" data-category_id_list="1|11|16|17|22|24|26|27|28|29|3|31|32|35|36|37|39|41|42|43|45|46|47|48|49|5|50|51|52|53|54|55|6|7|8" data-id="1567334079339" style="display: none;"></mpcpc></p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3、分布式服务架构</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,使前端应用能更快速的响应多变的市场需求。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">此时,用于提高业务复用及整合的分布式服务框架(RPC),提供统一的服务是关键。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">例如:各个团队的服务提供方就不要各自实现一套序列化、反序列化、网络框架、连接池、收发线程、超时处理、状态机等“业务之外”的重复技术劳动,造成整体的低效。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><strong style="color: rgb(0, 0, 0);">PS:</strong>其实上述三个原因也是为什么要有Dubbo的原因!不信你去Dubbo官网去看!</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><strong style="color: rgb(0, 0, 0);">流动计算架构</strong></p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><strong style="color: rgb(0, 0, 0);">PS:这个属于扩展内容,摘自Dubbo官网,属于架构演进的一个过程</strong></p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">当服务越来越多,容量的评估,小服务资源的浪费等问题逐渐显现,此时需增加一个调度中心基于访问压力实时管理集群容量,提高集群利用率。此时,用于提高机器利用率的资源调度和治理中心(SOA)是关键<span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 14px;letter-spacing: 0.544px;text-align: start;white-space: pre-line;background-color: rgb(255, 255, 255);">。整编:微信公众号,搜云库技术团队,ID:souyunku</span></p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">4、另外一个原因</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">就是因为在几个进程内(应用分布在不同的机器上),无法共用内存空间,或者在一台机器内通过本地调用无法完成相关的需求,比如不同的系统之间的通讯,甚至不同组织之间的通讯。此外由于机器的横向扩展,需要在多台机器组成的集群上部署应用等等。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">所以,统一RPC框架来解决提供统一的服务。</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">二、什么是RPC</h2><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。简言之,RPC使得程序能够像访问本地系统资源一样,去访问远端系统资源。比较关键的一些方面包括:通讯协议、序列化、资源(接口)描述、服务框架、性能、语言支持等。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><img class="img_loading" data-ratio="0.49282296650717705" data-src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125421474-919954314.png" data-type="jpeg" data-w="418" style="border- 0px; border-style: initial; border-color: initial; border-radius: 6px;  418px !important; height: 206px !important;" _width="418px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125421474-919954314.png" crossorigin="anonymous"></p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">简单的说,RPC就是从一台机器(客户端)上通过参数传递的方式调用另一台机器(服务器)上的一个函数或方法(可以统称为服务)并得到返回的结果。</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">三、PRC架构组件</h2><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">一个基本的RPC架构里面应该至少包含以下4个组件:</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">1、客户端(Client):</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">服务调用方(服务消费者)</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2、客户端存根(Client Stub):</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">存放服务端地址信息,将客户端的请求参数数据信息打包成网络消息,再通过网络传输发送给服务端</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3、服务端存根(Server Stub):</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">接收客户端发送过来的请求消息并进行解包,然后再调用本地服务进行处理</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">4、服务端(Server):</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">服务的真正提供者</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><img  data-w="889" style="border- 0px; border-style: initial; border-color: initial; border-radius: 6px;  677px !important; height: 492.71px !important;" _width="677px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125227397-377281720.png" ></p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><strong style="color: rgb(0, 0, 0);">具体调用过程:</strong></p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">1、</strong>服务消费者(client客户端)通过调用本地服务的方式调用需要消费的服务;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">2、</strong>客户端存根(client stub)接收到调用请求后负责将方法、入参等信息序列化(组装)成能够进行网络传输的消息体;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">3、</strong>客户端存根(client stub)找到远程的服务地址,并且将消息通过网络发送给服务端;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">4、</strong>服务端存根(server stub)收到消息后进行解码(反序列化操作);</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">5、</strong>服务端存根(server stub)根据解码结果调用本地的服务进行相关处理;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">6、</strong>本地服务执行具体业务逻辑并将处理结果返回给服务端存根(server stub);</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">7、</strong>服务端存根(server stub)将返回结果重新打包成消息(序列化)并通过网络发送至消费方;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">8、</strong>客户端存根(client stub)接收到消息,并进行解码(反序列化);</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-bottom: 5px;margin-top: 5px;"><strong style="color: rgb(0, 0, 0);">9、</strong>服务消费方得到最终结果;</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">而RPC框架的实现目标则是将上面的第2-10步完好地封装起来,也就是把调用、编码/解码的过程给封装起来,让用户感觉上像调用本地服务一样的调用远程服务。</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">四、RPC和SOA、SOAP、REST的区别</h2><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">1、REST</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">可以看着是HTTP协议的一种直接应用,默认基于JSON作为传输格式,使用简单,学习成本低效率高,但是安全性较低。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2、SOAP</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">SOAP是一种数据交换协议规范,是一种轻量的、简单的、基于XML的协议的规范。而SOAP可以看着是一个重量级的协议,基于XML、SOAP在安全方面是通过使用XML-Security和XML-Signature两个规范组成了WS-Security来实现安全控制的,当前已经得到了各个厂商的支持 <span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 14px;letter-spacing: 0.544px;text-align: start;white-space: pre-line;background-color: rgb(255, 255, 255);">。整编:微信公众号,搜云库技术团队,ID:souyunku</span></p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">它有什么优点?简单总结为:易用、灵活、跨语言、跨平台。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3、SOA</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">面向服务架构,它可以根据需求通过网络对松散耦合的粗粒度应用组件进行分布式部署、组合和使用。服务层是SOA的基础,可以直接被应用调用,从而有效控制系统中与软件代理交互的人为依赖性。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">SOA是一种粗粒度、松耦合服务架构,服务之间通过简单、精确定义接口进行通讯,不涉及底层编程接口和通讯模型。SOA可以看作是B/S模型、XML(标准通用标记语言的子集)/Web Service技术之后的自然延伸。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">4、REST 和 SOAP、RPC 有何区别呢?</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">没什么太大区别,他们的本质都是提供可支持分布式的基础服务,最大的区别在于他们各自的的特点所带来的不同应用场景 。</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">五、RPC框架需要解决的问题?</h2><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">1、</strong>如何确定客户端和服务端之间的通信协议?</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2、</strong>如何更高效地进行网络通信?</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">3、</strong>服务端提供的服务如何暴露给客户端?</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">4、</strong>客户端如何发现这些暴露的服务?</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">5、</strong>如何更高效地对请求对象和响应结果进行序列化和反序列化操作?</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">六、RPC的实现基础?</h2><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">1、</strong>需要有非常高效的网络通信,比如一般选择Netty作为网络通信框架;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2、</strong>需要有比较高效的序列化框架,比如谷歌的Protobuf序列化框架;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">3、</strong>可靠的寻址方式(主要是提供服务的发现),比如可以使用Zookeeper来注册服务等等;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">4、</strong>如果是带会话(状态)的RPC调用,还需要有会话和状态保持的功能;</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">七、RPC使用了哪些关键技术?</h2><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">1、动态代理</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">生成Client Stub(客户端存根)和Server Stub(服务端存根)的时候需要用到Java动态代理技术,可以使用JDK提供的原生的动态代理机制,也可以使用开源的:CGLib代理,Javassist字节码生成技术。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2、序列化和反序列化</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">在网络中,所有的数据都将会被转化为字节进行传送,所以为了能够使参数对象在网络中进行传输,需要对这些参数进行序列化和反序列化操作。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">序列化:把对象转换为字节序列的过程称为对象的序列化,也就是编码的过程。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">反序列化:把字节序列恢复为对象的过程称为对象的反序列化,也就是解码的过程。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">目前比较高效的开源序列化框架:如Kryo、FastJson和Protobuf等。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3、NIO通信</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">出于并发性能的考虑,传统的阻塞式 IO 显然不太合适,因此我们需要异步的 IO,即 NIO。Java 提供了 NIO 的解决方案,Java 7 也提供了更优秀的 NIO.2 支持。可以选择Netty或者MINA来解决NIO数据传输的问题。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">4、服务注册中心</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">可选:Redis、Zookeeper、Consul 、Etcd。一般使用ZooKeeper提供服务注册与发现功能,解决单点故障以及分布式部署的问题(注册中心)。</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">八、主流RPC框架有哪些</h2><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">1、RMI</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">利用java.rmi包实现,基于Java远程方法协议(Java Remote Method Protocol) 和java的原生序列化。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2、Hessian</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。基于HTTP协议,采用二进制编解码。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3、protobuf-rpc-pro</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">是一个Java类库,提供了基于 Google 的 Protocol Buffers 协议的远程方法调用的框架。基于 Netty 底层的 NIO 技术。支持 TCP 重用/ keep-alive、SSL加密、RPC 调用取消操作、嵌入式日志等功能。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">4、Thrift</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">是一种可伸缩的跨语言服务的软件框架。它拥有功能强大的代码生成引擎,无缝地支持C + +,C#,Java,Python和PHP和Ruby。thrift允许你定义一个描述文件,描述数据类型和服务接口。依据该文件,编译器方便地生成RPC客户端和服务器通信代码<span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 14px;letter-spacing: 0.544px;text-align: start;white-space: pre-line;background-color: rgb(255, 255, 255);">。整编:微信公众号,搜云库技术团队,ID:souyunku</span></p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">最初由facebook开发用做系统内个语言之间的RPC通信,2007年由facebook贡献到apache基金 ,现在是apache下的opensource之一 。支持多种语言之间的RPC方式的通信:php语言client可以构造一个对象,调用相应的服务方法来调用java语言的服务,跨越语言的C/S RPC调用。底层通讯基于SOCKET。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">5、Avro</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">出自Hadoop之父Doug Cutting, 在Thrift已经相当流行的情况下推出Avro的目标不仅是提供一套类似Thrift的通讯中间件,更是要建立一个新的,标准性的云计算的数据交换和存储的Protocol。支持HTTP,TCP两种协议。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">6、Dubbo</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">Dubbo是 阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。</p><h2 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;font-size: 17px;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">九、RPC的实现原理架构图</h2><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><img class="img_loading" data-ratio="0.6298568507157464" data-src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125704874-972732220.png" data-type="jpeg" data-w="489" style="border- 0px; border-style: initial; border-color: initial; border-radius: 6px;  489px !important; height: 308px !important;" _width="489px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125704874-972732220.png" crossorigin="anonymous"></p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><img class="img_loading" data-ratio="0.7269439421338155" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/tO7NEN7wjr5icBHfLv2ybw55GjSwwiaas3kbAQ5JibiaEWDqpBUxubutB4OcYYibGic1Wib1ibsEuzIMSdQQFR0GDsZCZA/640?wx_fmt=jpeg" data-type="jpeg" data-w="553" style="border- 0px; border-style: initial; border-color: initial; border-radius: 6px;  553px !important; height: 402px !important;" _width="553px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125800025-1033181348.png" crossorigin="anonymous">PS:这张图非常重点,是PRC的基本原理,请大家一定记住!</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">也就是说两台服务器A,B,一个应用部署在A服务器上,想要调用B服务器上应用提供的函数/方法,由于不在一个内存空间,不能直接调用,需要通过网络来表达调用的语义和传达调用的数据。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">比如说,A服务器想调用B服务器上的一个方法:</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">User getUserByName(String userName)</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">1、建立通信</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">首先要解决通讯的问题:即A机器想要调用B机器,首先得建立起通信连接。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">主要是通过在客户端和服务器之间建立TCP连接,远程过程调用的所有交换的数据都在这个连接里传输。连接可以是按需连接,调用结束后就断掉,也可以是长连接,多个远程过程调用共享同一个连接。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">通常这个连接可以是按需连接(需要调用的时候就先建立连接,调用结束后就立马断掉),也可以是长连接(客户端和服务器建立起连接之后保持长期持有,不管此时有无数据包的发送,可以配合心跳检测机制定期检测建立的连接是否存活有效),多个远程过程调用共享同一个连接<span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 14px;letter-spacing: 0.544px;text-align: start;white-space: pre-line;background-color: rgb(255, 255, 255);">。整编:微信公众号,搜云库技术团队,ID:souyunku</span></p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2、服务寻址</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">要解决寻址的问题,也就是说,A服务器上的应用怎么告诉底层的RPC框架,如何连接到B服务器(如主机或IP地址)以及特定的端口,方法的名称名称是什么。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">通常情况下我们需要提供B机器(主机名或IP地址)以及特定的端口,然后指定调用的方法或者函数的名称以及入参出参等信息,这样才能完成服务的一个调用。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">可靠的寻址方式(主要是提供服务的发现)是RPC的实现基石,比如可以采用Redis或者Zookeeper来注册服务等等。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;"><img class="" data-ratio="0.49282296650717705" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/tO7NEN7wjr5icBHfLv2ybw55GjSwwiaas328s2GDhA0pjgMhUZoqDrA25qUqB8sY8qla0ULFicuwNNlk3YmO3Lx2A/640?wx_fmt=jpeg" data-type="jpeg" data-w="418" style="border- 0px; border-style: initial; border-color: initial; border-radius: 6px;  418px !important; height: auto !important; visibility: visible !important;" _width="418px" src="https://img2018.cnblogs.com/blog/1112483/201909/1112483-20190902125839284-1056710814.png" crossorigin="anonymous" data-fail="0"></p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2.1、从服务提供者的角度看:</h3><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2.1.1、</strong>当服务提供者启动的时候,需要将自己提供的服务注册到指定的注册中心,以便服务消费者能够通过服务注册中心进行查找;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2.1.2、</strong>当服务提供者由于各种原因致使提供的服务停止时,需要向注册中心注销停止的服务;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2.1.3、</strong>服务的提供者需要定期向服务注册中心发送心跳检测,服务注册中心如果一段时间未收到来自服务提供者的心跳后,认为该服务提供者已经停止服务,则将该服务从注册中心上去掉</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">2.2、从调用者的角度看:</h3><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2.2.1、</strong>服务的调用者启动的时候根据自己订阅的服务向服务注册中心查找服务提供者的地址等信息;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2.2.2、</strong>当服务调用者消费的服务上线或者下线的时候,注册中心会告知该服务的调用者;</p><p style="font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;margin-top: 5px;margin-bottom: 5px;"><strong style="color: rgb(0, 0, 0);">2.2.3、</strong>服务调用者下线的时候,则取消订阅。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3、网络传输</h3><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3.1、序列化</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">当A机器上的应用发起一个RPC调用时,调用方法和其入参等信息需要通过底层的网络协议如TCP传输到B机器,由于网络协议是基于二进制的,所有我们传输的参数数据都需要先进行序列化(Serialize)或者编组(marshal)成二进制的形式才能在网络中进行传输。然后通过寻址操作和网络传输将序列化或者编组之后的二进制数据发送给B机器<span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 14px;letter-spacing: 0.544px;text-align: start;white-space: pre-line;background-color: rgb(255, 255, 255);">。整编:微信公众号,搜云库技术团队,ID:souyunku</span></p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">3.2、反序列化</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">当B机器接收到A机器的应用发来的请求之后,又需要对接收到的参数等信息进行反序列化操作(序列化的逆操作),即将二进制信息恢复为内存中的表达方式,然后再找到对应的方法(寻址的一部分)进行本地调用(一般是通过生成代理Proxy去调用,<br>通常会有JDK动态代理、CGLIB动态代理、Javassist生成字节码技术等),之后得到调用的返回值。</p><h3 style="margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(255, 41, 65);line-height: 1.35;text-align: start;white-space: normal;font-family: Menlo, Monaco, &quot;Source Code Pro&quot;, Consolas, Inconsolata, &quot;Ubuntu Mono&quot;, &quot;DejaVu Sans Mono&quot;, &quot;Courier New&quot;, &quot;Droid Sans Mono&quot;, &quot;Hiragino Sans GB&quot;, 微软雅黑, monospace !important;">4、服务调用</h3><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">B机器进行本地调用(通过代理Proxy和反射调用)之后得到了返回值,此时还需要再把返回值发送回A机器,同样也需要经过序列化操作,然后再经过网络传输将二进制数据发送回A机器,而当A机器接收到这些返回值之后,则再次进行反序列化操作,恢复为内存中的表达方式,最后再交给A机器上的应用进行相关处理,一般是业务逻辑处理操作。</p><p style="margin-top: 15px;margin-bottom: 15px;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;text-align: start;">通常,经过以上四个步骤之后,一次完整的RPC调用算是完成了,另外可能因为网络抖动等原因需要重试等。</p><section class="" data-role="paragraph" style="max- 100%;white-space: normal;font-size: 16px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"><p style="margin-top: 15px;margin-bottom: 15px;max- 100%;min-height: 1em;text-align: center;font-size: 14px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"><strong style="max- 100%;color: rgb(0, 0, 0);overflow-wrap: break-word !important;box-sizing: border-box !important;">敬请关注「搜云库技术团队」微信公众号,获取最新文章</strong></p></section><section class="" data-tools="135编辑器" data-id="92737" style="max- 100%;white-space: normal;font-size: 16px;font-family: mp-quote, -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"><section data-width="100%" style="max- 100%; 2022px;border-left: 2px dashed rgb(244, 244, 244);background: rgb(245, 245, 245);overflow-wrap: break-word !important;box-sizing: border-box !important;"><section class="" style="padding: 0.5em;max- 100%;font-size: 14px;letter-spacing: 1.5px;line-height: 1.75em;color: rgb(63, 62, 63);overflow-wrap: break-word !important;box-sizing: border-box !important;"><p style="max- 100%;min-height: 1em;letter-spacing: 0.5px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max- 100%;color: rgb(136, 136, 136);font-size: 13px;caret-color: red;font-family: Helvetica, Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"></span><span style="color: rgb(136, 136, 136);font-family: Helvetica, Arial, sans-serif;font-size: 13px;caret-color: red;">来源:</span><span style="color: rgb(136, 136, 136);font-family: Helvetica, Arial, sans-serif;font-size: 13px;caret-color: red;">http://t.cn/Ai8fdqpP</span></p><p style="max- 100%;min-height: 1em;letter-spacing: 0.5px;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max- 100%;color: rgb(136, 136, 136);font-size: 13px;font-family: Helvetica, Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">整编:搜云库技术团队,欢迎广大技术人员投稿</span></p></section></section></section><p style="margin-top: 5px;max- 100%;min-height: 1em;white-space: normal;font-size: 16px;color: rgb(62, 62, 62);letter-spacing: 2px;line-height: 2em;font-family: -apple-system-font, BlinkMacSystemFont, Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;"><span style="max- 100%;font-size: 14px;color: rgb(0, 0, 0);letter-spacing: 0.5px;font-family: Helvetica, Arial, sans-serif;overflow-wrap: break-word !important;box-sizing: border-box !important;">如果对本文的内容有疑问,请在文章留言区留言,谢谢。</span></p>
                </div>
    
                <div class="read-more__area" id="js_more_read_area" style="display:none;">
                    
                </div>
    
                
                                <div class="reward_area tc reward_area_primary" id="js_preview_reward_author" style="display:none;">
                    <div class="reward-avatar" style="display: none;" id="js_preview_reward_author_avatar">
                        <img src="" alt="" id="js_preview_reward_author_head">
                    </div>
                    
                                        <div class="reward-author" id="js_preview_reward_author_name">hu1991die</div>
                                        <p class="reward_tips" id="js_preview_reward_author_wording" style="display:none;"></p>
                    <p class="reward_button_wrp">
                    
                      <span class="reward_pop_panel">
                        <img src="https://res.wx.qq.com/mpres/zh_CN/htmledition/pages/home/index/pic_mp_app4290ba.png" alt="">
                        <strong>扫一扫下载订阅号助手,用手机发文章</strong>
                      </span>
                        <a class="reward_button" id="js_preview_reward_author_link" href="##"><span id="js_preview_reward_link_text">赞赏</span></a>
                    </p>
                </div>
    
                <div class="reward_qrcode_area reward_area tc" id="js_preview_reward_qrcode" style="display:none;">
                    <p class="tips_global">长按二维码向我转账</p>
                    <p id="js_preview_reward_ios_wording" class="reward_tips" style="display:none;"></p>
                    <span class="reward_qrcode_img_wrp"><img class="reward_qrcode_img" src="//res.wx.qq.com/mmbizwap/zh_CN/htmledition/images/pic/appmsg/pic_reward_qrcode.2x42f400.png"></span>
                    <p class="tips_global">受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。</p>
                </div>
    

    原文地址:https://mp.weixin.qq.com/s/06Iumf2jy4CPtTazLzxjjg

  • 相关阅读:
    djangoadmin实现文件上传下载
    Apscheduler详解(转)
    django集成Apscheduler3
    springboot实现token鉴权
    xss攻击入门
    回顾2012——运维工作周年祭
    9个常用iptables配置实例
    ardunio 实验:超声波测距、声光报警模拟倒车雷达
    一款我用了好多年的多线程FTP软件
    线程Thread基础学习(2)
  • 原文地址:https://www.cnblogs.com/jpfss/p/11445873.html
Copyright © 2011-2022 走看看