rpc 与http

1. Remote call method

Whether it is microservices or distributed services (both SOA, both are service-oriented programming), they are all faced with remote calls between services. So what are the remote calling methods between services?

The common remote invocation methods are as follows:

  • RPC: Remote Produce Call remote procedure call, similar to RMI (Remote Methods Invoke) Calling is a concept in JAVA and one of the thirteen major technologies of JAVA). Custom data format, based on native TCP communication, fast speed and high efficiency. Early webservice, now popular dubbo, are typical of RPC

    • RPC framework: webservie (cxf), dubbo
    • RMI framework: hessian
  • Http: http is actually a network transmission protocol, based on TCP, which specifies the format of data transmission. Now the communication between the client browser and the server basically uses the Http protocol. It can also be used to make remote service calls. The disadvantage is that the message package is bloated.

    Now the popular Rest style can be achieved through the http protocol.

    • The implementation technology of http: HttpClient
  • Similarities: The underlying communication is based on sockets , Both can realize remote invocation, and both can realize service invocation service

  • Different points:
    RPC: Framework There are: dubbo, cxf, (RMI remote method call) Hessian
    When using the RPC framework to implement inter-service calls, the service provider and service consumer are required to use a unified RPC framework, either dubbo or cxf

    Use across operating systems in the same programming language
    Advantages: Fast invocation, fast processing

    http: Frameworks are: httpClient
    When When using http to call between services, there is no need to pay attention to the programming language used by the service provider, nor the programming language used by the service consumer. The service provider only needs to provide a restful style interface. The service consumer, in accordance with the principle of restful, Just request service.

    Cross-system and cross-programming language remote call framework
    Advantage: Strong versatility

    Summary: Compare the difference between RPC and http
    1 RPC requires the service provider and the service caller to use the same technology, either hessian or dubbo
    but http does not need to pay attention to the language implementation, only needs to follow the rest specification
    2 RPC There are many development requirements. For example, the Hessian framework also requires the server to provide a complete interface code (package name, class name, and method name must be exactly the same), otherwise the client cannot run
    3 Hessian only supports POST requests
    4 Hessian only Support JAVA language

1.1. Know RPC

RPC, namely Remote Procedure Call (remote procedure call), is a computer communication protocol. This protocol allows programs running on one computer to call subroutines of another computer without the programmer needing to program this interaction. To put it simply, computer A provides a service, and computer B can call the service of computer A just like calling a local service.

Through the above concept, we can know that the realization of RPC is mainly to do two things:

  • Realize remotely calling other computer services
    • To achieve Remote calls must be data transmission through the network. Program A provides services, program B passes request parameters to program A through the network, and A gets the result after local execution, and then returns the result to program B. There are two points to pay attention to here:
      • 1) Which network communication protocol is used?
        • The more popular RPC frameworks now use TCP as the underlying transmission protocol
      • 2) What is the format of data transmission?
        • To communicate between the two programs, the data transmission format must be agreed upon. It’s like two people chatting, they have to use the same language, otherwise they can’t communicate. Therefore, we must define the format of the request and response. In addition, data needs to be serialized for transmission over the network, so a unified serialization method needs to be agreed upon.
  • Call the remote service like a local service
    • If it’s just a remote call , Is not RPC, because RPC emphasizes the procedure call, the call process should be transparent to the user, the user should not care about the details of the call, and can call the remote service like a local service. So RPC must encapsulate the calling process

RPC call flow chart:

Insert picture here Description

1.2. Know Http

Http protocol: Hypertext Transfer Protocol , Is an application layer protocol. It specifies the request format, response format, resource location and operation mode of network transmission. However, there is no stipulation on what network transmission protocol the bottom layer uses, but now TCP protocol is used as the bottom layer transmission protocol. Having said that, you may think that the remote calls of Http and RPC are very similar. They both carry out network communication in accordance with a certain prescribed data format, and there is a request and a response. Yes, at this point, the two are very similar, but there are some subtle differences.

  • RPC does not specify the data transmission format. This format can be arbitrarily specified. The data format may not be the same for different RPC protocols.
  • Http also defines the path of resource location, which is not needed in RPC
  • The most important point: RPC needs to call remote services like local services, that is, to call The process is encapsulated at the API level. Http protocol does not have such requirements, so the details of request and response need to be implemented by ourselves.
    • Advantages: The RPC method is more transparent and more convenient for users. Http method is more flexible, no API and language are specified, cross-language, cross-platform
    • Disadvantage: RPC method needs to be encapsulated at the API level, which limits the language environment for development.

For example, when we visit a website through a browser, it is through the Http protocol. It’s just that the browser encapsulates the request, initiates the request, receives the response, and parses the response for us. If it is not through the browser, then these things need to be done by yourself.

Insert picture description here

1.3. how to choose?

Since the two methods can achieve remote calls, how should we choose?

  • In terms of speed, RPC is faster than http. Although the underlying layer is TCP, the information of http protocol is often bloated
  • In terms of difficulty, RPC implementation is more complicated. , Http is relatively simple
  • In terms of flexibility, http is better because it does not care about implementation details, and is cross-platform and cross-language.

Therefore, both have different usage scenarios:

  • If higher efficiency is required, and the development process uses a unified technology stack, then use RPC is still good.
  • If you need to be more flexible, cross-language, cross-platform, obviously http is more suitable

So how should we choose?

Microservices emphasize independence, autonomy, and flexibility. The RPC method has many restrictions, so in the microservice framework, Http-based Rest style services are generally used.

Leave a Comment

Your email address will not be published.