RPC remote call popular understanding

Start with a case in terms of RPC (articles from Life program Micro Signal)

In order to enhance the service capabilities of the hotel, the hotel is only a start from a chef responsible for all things have developed into a chef, vegetable division, division, and other dishes prepared by more than one role. Only a chef in a restaurant, when you want to make time to cook a delicious tomato scrambled eggs, he needs to wash your own tomatoes, cut tomato, beat eggs, cooking. The whole process does not require others to participate and that they can complete. This is the old centralized applications, a single computer can handle all the things

制作番茄炒蛋{
    厨师->洗菜->切菜->炒菜
}

 

With the hotel development requires a clear division of labor, professional people responsible for the professional thing. So, the whole process will not only cook cooking involved. Need to have multiple roles, the master is responsible for preparing dishes prepared tomato and egg, vegetable master responsible for chopping vegetables, as long as the chef in charge of cooking on the line.

However, with clear division of labor, the process of making tomato scrambled eggs is no longer only one person involved in the process of the. This process requires multilateral collaboration. Before cooking the chef prepare, you need to inform and vegetable dishes prepared master chef, the preamble work to be ready after cooking.
制作番茄炒蛋{
    备菜师->洗菜
    切菜师->切菜
    厨师->炒菜
}

In this case, we must rely on a lot of personal chefs to participate in the work of cooking. And his teacher helped him notice prepared dish of vegetables, vegetable notice to help him master the cooking time, this process is a remote procedure call. In most cases, is generally orders directly to the kitchen staff, then there is a Houchu personnel were distributed to the menu dishes prepared teachers, vegetable division and cooks. This process is much like a computer system and. Today, large sites are distributed deployments. After take a single process, it may involve multiple systems logistics, payment, inventory, red envelope, etc., and more than one system is deployed separately on different machines, each responsible for a different team. And in order to achieve the order process, you need to use remote call

What is the Remote Procedure Call

RPC is a computer on which a process A, a process calling another computer on B, wherein A of the calling process is suspended, the calling process to be started on the B, when returned to the value A, the process continues A . The caller can be transmitted by using the parameter information to the called party, then you can get the information returned by the results. And this process is transparent to the developer.

As in the example Houchu, like the waiter to pass Houchu menu, chefs prepare dishes to tell teachers and vegetables division began work, and then he waits for them to complete the work. After completion of the work of teachers and vegetables prepared dish cooks and start cooking. This process is transparent to the waiter is in fact, he does not need to be concerned about in the end is how Houchu cooking.

 

 

Since each service deployed on different machines, in order to make remote calls inevitably network communication process between services, each service consumer invokes a service should write a lump-related network communications code is complicated and error-prone. If there was a way to make the service as we like to call a local call to the remote service, and allow the caller to communicate these details transparent to the network, it will greatly enhance productivity, such as when the service consumer in the implementation of orderService.buy ( "HHKB keyboard"), essentially calling a remote service. In this way it is actually RPC. The tool provides this functionality we call RPC framework.

In RPC framework has three main roles: Provider, Consumer and Registry. As shown below:

 

  • Server: expose service provider service

  • Client: call the remote service consumer services

  • Registry: service registration and discovery registries

Service providers and service consumers are better understood, the division is Houchu vegetables and cook it. Chef is the service consumer, vegetables division is the service provider. Cook vegetables dependent services division provides. A service registry is a what is it?

In fact, this is relatively easy to understand. For the kind of great restaurants, the chef may have many (cluster deployment), there are many (cluster deployment) vegetables division. And you want to cook the vegetables when vegetables division to help, he will not find a direct vegetables division, but notice a middleman, this person may be leading vegetables division team, and might also be a special coordination Houchu personnel. He knows how many vegetables whole kitchen division, also know which vegetables division today come to work (you need to be registered service). Moreover, he can also dynamically assign tasks (load balancing) in accordance with various vegetables division busy situation.

The middleman is the service registry.

 

 service provider after registering the initiative to start the machine ip to the registry, port and a list of services provided; access to services to the registry when consumers start service provider address list, you can achieve a soft load balancing and Failover;

RPC need to use technology to achieve

Issue a mature RPC framework to consider are many, here only basic technical need to use a remote call, interested friends can find some open source code is look at the RPC framework.

Dynamic Proxy

生成 client stub和server stub需要用到Java 动态代理技术,我们可以使用JDK原生的动态代理机制,可以使用一些开源字节码工具框架 如:CgLib、Javassist等。

序列化 

为了能在网络上传输和接收 Java对象,我们需要对它进行序列化和反序列化操作。

可以使用Java原生的序列化机制,但是效率非常低,推荐使用一些开源的、成熟的序列化技术,例如:protobuf、Thrift、hessian、Kryo、Msgpack

NIO

当前很多RPC框架都直接基于netty这一IO通信框架,比如阿里巴巴的HSF、dubbo,Hadoop Avro,推荐使用Netty 作为底层通信框架。

服务注册中心 

可选技术: Redis、Zookeeper、Consul、Etcd

参考资料 :https://www.jianshu.com/p/dbfac2b876b1

 

开源RPC框架

Dubbo

Dubbo 是阿里巴巴公司开源的一个Java高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成。目前已经进入Apache孵化器。

Motan

Motan是新浪微博开源的一个Java RPC框架。2016年5月开源。Motan 在微博平台中已经广泛应用,每天为数百个服务完成近千亿次的调用。

gRPC

gRPC是Google开发的高性能、通用的开源RPC框架,其由Google主要面向移动应用开发并基于HTTP/2协议标准而设计,基于ProtoBuf(Protocol Buffers)序列化协议开发,且支持众多开发语言。本身它不是分布式的,所以要实现上面的框架的功能需要进一步的开发。

Thrift

Thrift是Apache的一个跨语言的高性能的服务框架,也得到了广泛的应用。

 

 

 

Guess you like

Origin blog.csdn.net/cfun_goodmorning/article/details/86692028