What is an RPC framework? What are the mainstream RPC frameworks? How to implement RPC services in Golang?
1. Introduction to RPC Protocol
1.1 What is the RPC protocol
- RPC, also known as Remote Procedure Call (RPC), is a computer communication protocol
- RPC is a protocol that enables programs to execute subroutines or services in different address spaces. Through RPC, programs can call remote services just like calling local functions, thus hiding the complexity of network communication.
- The RPC protocol allows a program on one computer to call a subroutine on another computer.
1.2 Why do we need RPC protocol?
- Distributed system support:In distributed applications, services may run on different machines, and RPC simplifies the communication between them.
- Simplified development:Developers can focus on business logic without having to deal with low-level network details.
- Modularity:Allows functions to be distributed across multiple services, making maintenance and expansion easier.
1.3 Advantages and Disadvantages of RPC
advantage
- Transparency: The way to call remote services is similar to calling local services, and developers do not need to worry about the underlying implementation.
- Cross-platform support: Different systems and programming languages can communicate through RPC.
- Easy to expand: New services can be easily added without affecting existing services.
shortcoming
- Performance overhead: Network latency and data serialization/deserialization increase response time.
- Complexity of troubleshooting: Network problems, service downtime, and other situations may cause call failures and require additional processing.
- Difficult debugging: Remote call errors are more difficult to debug than local calls.
1.4 Characteristics of RPC Protocol
- Serialization: Data needs to be serialized into a byte stream before transmission to facilitate transmission over the network. Common formats include JSON, XML, Protocol Buffers, etc.
- Synchronous vs. Asynchronous: RPC can be synchronous (wait for the call result) or asynchronous (return immediately without waiting for the result).
- State Management: RPC calls are typically stateless, but some implementations may support session state.
- Security: Ensure the security of data during transmission through encryption, authentication, etc.
2. RPC Framework
2.1 Dubbo
- Introduction:Dubbo is a popular RPC framework developed in Java. It is widely used in Java projects. It integrates most service governance components, supports service governance, multiple serialization protocols, supports multiple registration centers, and has its own service management center. The disadvantage is that it does not support cross-language communication in the early days. It is mainly used in Java projects. The latest version version 3.3 already supports multiple language SDKs.
- Project Documentation:https://cn.dubbo.apache.org/zh-cn/overview/
- Github address:https://github.com/apache/dubbo/
2.2 Motan
- Introduction: Motan, like Dubbo, is also a popular RPC framework developed in Java and is widely used in Java projects. Like the Dubbo framework, it did not support cross-language features in its early stages. Subsequent versions have added multi-language versions, support for service governance, multiple serialization protocols, support for multiple registration centers, and a built-in service management center. It is mainly used in Java projects.
- Official Documentation:https://github.com/weibocom/motan/wiki/zh_overview
- Github address:https://github.com/weibocom/motan
2.3 Thrift
- Introduction: Thrift is a cross-language lightweight RPC framework, originally developed and maintained by Facebook, and currently maintained by Apache open source. It only supports the Thrift protocol. The framework itself does not implement service governance and registration components, and developers need to integrate them themselves.
- Official Address:https://thrift.apache.org/
- Github address:https://github.com/apache/thrift
2.4 Grpc
- Introduction:The Grpc framework is developed and open-sourced by Google. It is a language-neutral, platform-neutral, open-source remote procedure call system that supports multiple languages. Grpc uses protocol buffers by default and is the most widely used RPC framework. Most of the current mainstream applications support and integrate the Grpc service interface. In the actual development and use process, we usually define the proto file first and use protoc to automatically create the Grpc server.
- Official Address:https://grpc.io/docs/
- Github address:https://github.com/grpc/grpc
2.5 Rpcx
- Introduction:Rpcx is a high-performance Rpc framework developed and maintained by Guoren Niaowo. Its main features are simplicity and ease of use. It can be easily integrated into your own project according to the document steps. It has high performance, cross-platform, and multi-language support. The framework level integrates service discovery and service governance related functions.
- Official Address:https://rpcx.io/
- Github address:https://github.com/smallnest/rpcx
3. Implementing Rpc service in Golang
It is very simple to implement Rpc service in Golang. Golang official and some third-party libraries provide Rpc service implementation methods. Next, we will use the Rpc service implementation method provided by Golang official.net/rpc
The library is used to simply implement a Golang Rpc service.
3.1 net/rpc library description
- Serialization Protocol:
net/rpc
By defaultencoding/gob
To serialize data, Golang has its own serialization protocol, which is not supported by other development languages.net/rpc
Library DevelopmentR
The server supports internal calls of the Golang language. - Other serialization protocols: For multi-language communication, GOlang officially
net/rpc
The package providesnet/rpc/jsonrpc
Library, supportsjson
Serialize the data in the form ofR
Service cross-language calling.
3.2 Implementing Rpc service based on net/rpc library
3.2.1 Rpc server
package main import ( "log" "net/http" "net/rpc" ) // Golang implements Rpc service based on net/rpc library type Request struct { M int N int } type Server struct{} func (s *Server) Multiply(r Request, res *int) error { *res = rM * rN return nil } // main function func main() { // Service Registration serv := new(Server) // Register serv serviceif err := rpc.Register(serv); err != nil { log.Panicf("register server error, %s\n", err) return } // Bind serv service to Http service rpc.HandleHTTP() // Listen for HTTP serviceif err := http.ListenAndServe("127.0.0.1:8081", nil); err != nil { log.Panicf("ListenAndServe error, %s\n", err) return } }
3.2.2 Rpc Client
package main import ( "fmt" "log" "net/rpc" ) type Request struct { M, N int } func main() { // 创建连接与远程RPC服务 conn, err := rpc.DialHTTP("tcp", "127.0.0.1:8081") if err != nil { log.Panicf("DialHTTP error, %s\n", err) return } // 命令命令倒计时 res := 0 req := Request{ M: 10, N: 20, } if err = conn.Call("serv.Multiply", req, &res); err != nil { log.Panicf("Call func error, %s\n", err) return } fmt.Printf("%d x %d = %d", req.M, req.N, res) }
4. Conclusion
This paper mainly introduces what RPC is, the advantages and disadvantages of RPC, the mainstream RPC framework, and how to use the official RPC framework in Golang.net/rpc
The library implements RPC services and calls. In the next article, let's discuss how to implement RPC services in Golang with the help of Grpc.