分享

RPC相关

 燮羽 2010-11-08
RPC 工作原理

每项 RPC 服务都会在注册表中为自己配置一个通用的唯一标识符 (UUID)(类似于全局唯一标识符 GUID)。UUID 是一种常用标识符,各项服务都有一个唯一的 UUID,且在所有平台上通用。当一项 RPC 服务启动时,它会获得一个可用的高端口,并以其 UUID 在该端口注册。有些服务随机使用高端口;而有些服务却每次都尽量使用相同的高端口(如果这些高端口可用)。在服务的生存期内,端口的分配是静态的。 

当一个客户端要与某特定 RPC 服务通讯时,它无法事先确定该服务在哪个端口上运行。该客户端会先建立一个到服务器端口映射器服务(在 135 上)的连接,并利用该服务的 UUID 请求所需服务。端口映射器会将相应的端口号返回给客户端,然后关闭连接。最后,客户端利用端口映射器提供的端口号,新建一个到该服务器的连接。 

由于事先无法获悉 RPC 服务将使用哪个端口,因此防火墙不得不允许所有高端口通过。 

有限的 RPC


优点:仅开放一个高端口,比动态 RPC 更安全

缺点:修改所有服务器的注册表

这种方法更安全,但却需要修改所有域控制器的注册表。可以通过用 Microsoft ® Windows ® 2000 资源工具包中的工具编写脚本这种方式来修改注册表,这样可避免出现配置错误。 

必须为 RPC 复制确定一个固定的端口号。Internet Assigned Numbers Authority (IANA) 规定将 49152 - 65535 之间的数字用于专用和动态分配。
 

什么是 RPC ? Remote Procedure Calls 远程过程调用 (RPC) 是一种协议,程序可使用这种协议向网络中的另一台计算机上的程序请求服务。由于使用 RPC 的程序不必了解支持通信的网络协议的情况,因此 RPC 提高了程序的互操作性。在 RPC 中,发出请求的程序是客户程序,而提供服务的程序是服务器。 RPC(远程过程调用)是一项广泛用于支持分布式应用程序(不同组件分布在不同计算机上的应用程序)的技术。RPC 的主要目的是为组件提供一种相互通信的方式,使这些组件之间能够相互发出请求并传递这些请求的结果。 win 2000以后的版本 RPC服务都是不可少的

 

I finally get REST. Wow.

Yeah, I'm alive. And I remember the password to my blog. I've been away for a bit, working on something very cool involving the TV. If all goes well, you'll hear about it in a big way. Anyway, I'm still having a ball out here in reality. Building something real has a way of focusing your decisions about technology. My app is a distributed system, some of which runs in a cable plant head-end or telco office (whatever's on the other end of the wire in your living room), and some of which runs elsewhere. We also connect to some things on the Web. These connections have to be extremely flexible and the bar to adoption has to be low. The thing I finally realized (some of you will say “Duh!”) is that Web services are not a good way to do this.

It's depressing to think that SOAP started just about 10 years ago and that now that everything is said and done, we built RPC again. I know SOAP is really an XML messaging protocol, you can do oneway async stuff, etc, etc, but let's face it. The tools make the technology and the tools (and the examples and the advice you get) point at RPC. And we know what the problems with RPC are. If you want to build something that is genuinely loosely-coupled, RPC is a pretty hard path to take.

That realization would have gotten me down if not for the fact that something else jazzed me up an hour or so later. I was in the process of considering the alternatives when I finally understood REST. And wow, it was eye-opening. REST is often positioned as CRUD operations against entities identified by URIs. Then it is dismissed as to simplistic to be useful. You can't build with just CRUD, the reasoning goes, just think about why we write sprocs. I've been down that path any number of times and always ended up in the same place. But I had it all wrong.

I skimmed Fielding's thesis a while back, but it wasn't until I read Sam Ruby's recent posts that it really sank in. Here's what I came to understand. Every communication protocol has a state machine. For some protocols they are very simple, for others they are more complex. When you implement a protocol via RPC, you build methods that modify the state of the communication. That state is maintained as a black box at the endpoint. Because the protocol state is hidden, it is easy to get things wrong. For instance, you might call Process before calling Init. People have been looking for ways to avoid these problems by annotating interface type information for a long time, but I'm not aware of any mainstream solutions. The fact that the state of the protocol is encapsulated behind method invocations that modify that state in non-obvious ways also makes versioning interesting.

The essence of REST is to make the states of the protocol explicit and addressible by URIs. The current state of the protocol state machine is represented by the URI you just operated on and the state representation you retrieved. You change state by operating on the URI of the state you're moving to, making that your new state. A state's representation includes the links (arcs in the graph) to the other states that you can move to from the current state. This is exactly how browser based apps work, and there is no reason that your app's protocol can't work that way too. (TheATOM Publishing protocol is the canonical example, though its easy to think that its about entities, not a state machine.)

The “state machine as node graph traversed via URI” view of the world has really interesting implications for being able to suspend and resume a protocol. Because links to other states are embedded in a state's representation there are interesting ways to solve dynamic load-balancing, data-directed-routing, versioning and other problems using normal Web infrastructure. And because it's HTTP based, you get all the features that protocol supplies, including streaming and support for non-XML MIME types (a huge concern when you're doing TV stuff). The one thing that's really missing here is a simple framework for implementing a URI graph on top of an HTTP handler (similar to what Marc's been working on for Java). I'm building my own now.

The thing I love about this model is that, as Sam says, it is of the Web, not over the Web. That doesn't mean I'll use it for everything. I use TDS to get to SQL Server. I use WCF for RPC-style communication between distributed components within major systems. I'll use this model when I cross major system boundaries, especially when I don't own both sides. I'll let you know how it turns out.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多