golang中的net/rpc包

本文先介绍RPC,然后go原生对RPC的使用,之后是介绍go语言中有哪些RPC框架以及一些其他常见的框架,最后是探究go语言中rpc的源码。

(1)首先介绍下什么RPC?
(2)RPC可以做什么?
(3)RPC与REST风格的API有什么不同?
(4)go语言中使用RPC
(5)常见的RPC框架
(6)RPC源码探究

一、什么是RPC?

RPC是Remote Procedure Call,其中文意思就是 远程过程调用,可以理解成 一台主机上的进程调用另一台主机的进程服务,由一方为其它若干个主机提供服务。从表面上看非常类似于http API,RPC的目的可以屏蔽不同语言之间的关联,最大程度上进行解耦,调用方不需要知道服务方是用什么语言编写和其实现,只要知道服务方的RPC对外服务就行。其本质就是进程间的一种通信方式,可以是本机也可以是不同主机。

二、RPC可以做什么?

API、进程间通信,主要用于分布式应用间通信。

三、RPC与REST风格API有什么不同?

本质区别就是REST是使用http协议,相比RPC的实现协议传输会传更多的内容,但是两个可以做相同的事情。

四、go语言中使用RPC

RPC分服务提供和服务使用,也就是服务端和客户端,我们先来编写服务端:

 

服务端:

1)服务内容

1
2
3
4
5
6
7
8
// 对外提供的必须是对外可见的类型
type Arith int
 
// 对外提供的方法也要是对外可见的类型,其中要被注册的服务至少要有一个对外可见的方法,不然执行的时候的时候会打印错误,还有对外服务的必须是方法第一个参数必须是对外可见的类型,第二个参数可以是对外可见类型或者是内置类型,然后必须要有一个返回值。
func (t *Arith) Multiply(args *Args, reply *int) error {
    *reply = args.A * args.B
    return nil
}

 

2)端口监听

1
l, err := net.Listen("tcp", ":12345")

  

3)注册服务

1
2
3
4
5
t := new(GetServerTime)
// 注册到RPC
err = rpc.Register(t)
// 或者按名称注册
rpc.RegisterName("name",new(GetServerTime))

  

  

4)开启服务

1
rpc.HandleHTTP()

  

5)启动HTTP服务

 

1
http.Serve(l, nil)

  

相比服务端,客户端的编写会简单很多,


客户端:

1)连接服务RPC

 
1
client, err := rpc.DialHTTP(协议, ip:端口)

  

2)调用RPC服务
有两种方式:同步或异步

1
2
3
4
5
// 同步方式
client.Call("rpc上的公开类名:公开方法", 第一个传入的变量, 第二个传入的变量)
// 异步方式
divCall := client.Go("rpc上的公开类名:公开方法", 第一次传入的变量, 第二个传入的变量, nil)
replyCall := <- divCall.Done        阻塞,等待异步完成

  

最基本的流程就是这样,然后下面一段例子,是从标准库那边copy过来的:

 

public.go

1
2
3
4
5
6
7
8
9
package public
 
type Args struct {
    A, B int
}
 
type Quotient struct {
    Quo, Rem int
}

  

server.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
 
import (
    "Songzhibin/test/rpc/public"
    "errors"
    "log"
    "net"
    "net/http"
    "net/rpc"
)
 
type Arith int
 
func (t *Arith) Multiply(args *public.Args, reply *int) error {
    *reply = args.A * args.B
    return nil
}
 
func (t *Arith) Divide(args *public.Args, quo *public.Quotient) error {
    if args.B == 0 {
        return errors.New("divide by zero")
    }
    quo.Quo = args.A / args.B
    quo.Rem = args.A % args.B
    return nil
}
 
func main() {
    arith := new(Arith)
    rpc.Register(arith)
    rpc.HandleHTTP()
    l, e := net.Listen("tcp", ":1234")
    if e != nil {
        log.Fatal("listen error:", e)
    }
    go http.Serve(l, nil)
    select {}
}

  

  

client.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 客户端
package main
 
import (
   "Songzhibin/test/rpc/public"
   "fmt"
   "log"
   "net/rpc"
)
 
func main() {
   client, err := rpc.DialHTTP("tcp", "127.0.0.1:1234")
   if err != nil {
      log.Fatal("dialing:", err)
   }
   // 然后,客户端可以执行远程调用:
 
   // Synchronous call
   args := &public.Args{7, 8}
   var reply int
   err = client.Call("Arith.Multiply", args, &reply)
   if err != nil {
      log.Fatal("arith error:", err)
   }
   fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
   // 或:
 
   // Asynchronous call
   quotient := new(public.Quotient)
   divCall := client.Go("Arith.Divide", args, quotient, nil)
   replyCall := <-divCall.Done // will be equal to divCall
   // check errors, print, etc.
   fmt.Printf("%#v\n", replyCall)
}

  

  

 

TCP-RPC(GOB)

public.go  

1
2
3
4
5
6
7
8
9
package public
 
type Args struct {
    A, B int
}
 
type Quotient struct {
    Quo, Rem int
}

  

server.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
 
import (
    "Songzhibin/test/rpc/public"
    "errors"
    "fmt"
    "net"
    "net/rpc"
)
 
type Arith int
 
func (t *Arith) Multiply(args *public.Args, reply *int) error {
    *reply = args.A * args.B
    return nil
}
 
func (t *Arith) Divide(args *public.Args, quo *public.Quotient) error {
    if args.B == 0 {
        return errors.New("divide by zero")
    }
    quo.Quo = args.A / args.B
    quo.Rem = args.A % args.B
    return nil
}
 
func main() {
    arith := new(Arith)
    rpc.Register(arith)
    // rpc.HandleHTTP() 不使用 HandleHTTP()
    l, err := net.Listen("tcp", ":1234")
    if err != nil {
        fmt.Println(err)
        return
    }
    for {
        // 获取连接
        conn, err := l.Accept()
        if err != nil {
            fmt.Println(err)
            return
        }
        rpc.ServeConn(conn)
    }
    select {}
}

  

  

client.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
 
import (
    "Songzhibin/test/rpc/public"
    "fmt"
    "log"
    "net/rpc"
)
 
func main() {
    // 这里只需要将 DialHTTP改为Dial 即可
    client, err := rpc.Dial("tcp", "127.0.0.1:1234")
    if err != nil {
        log.Fatal("dialing:", err)
    }
    // 然后,客户端可以执行远程调用:
 
    // Synchronous call
    args := &public.Args{7, 8}
    var reply int
    err = client.Call("Arith.Multiply", args, &reply)
    if err != nil {
        log.Fatal("arith error:", err)
    }
    fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
    // 或:
 
    // Asynchronous call
    quotient := new(public.Quotient)
    divCall := client.Go("Arith.Divide", args, quotient, nil)
    replyCall := <-divCall.Done // will be equal to divCall
    // check errors, print, etc.
    fmt.Printf("%#v\n", replyCall)
}

  

  

 

 

Json-RPC(TCP)

public.go  

1
2
3
4
5
6
7
8
9
package public
 
type Args struct {
    A, B int
}
 
type Quotient struct {
    Quo, Rem int
}
 
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
 
import (
    "Songzhibin/test/rpc/public"
    "errors"
    "fmt"
    "net"
    "net/rpc"
    "net/rpc/jsonrpc"
)
 
type Arith int
 
func (t *Arith) Multiply(args *public.Args, reply *int) error {
    *reply = args.A * args.B
    return nil
}
 
func (t *Arith) Divide(args *public.Args, quo *public.Quotient) error {
    if args.B == 0 {
        return errors.New("divide by zero")
    }
    quo.Quo = args.A / args.B
    quo.Rem = args.A % args.B
    return nil
}
 
func main() {
    arith := new(Arith)
    rpc.Register(arith)
    // rpc.HandleHTTP() 不使用 HandleHTTP()
    l, err := net.Listen("tcp", ":1234")
    if err != nil {
        fmt.Println(err)
        return
    }
    for {
        // 获取连接
        conn, err := l.Accept()
        if err != nil {
            fmt.Println(err)
            return
        }
        // 将 rpc.ServeConn 改为jsonrpc.ServeConn
        // 或使用 rpc.ServeCodec(jsonrpc.NewServerCodec(conn)) 更改指定编码 
        jsonrpc.ServeConn(conn)
    }
    select {}
}

  

  

  

client.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main
 
import (
    "Songzhibin/test/rpc/public"
    "fmt"
    "log"
    "net/rpc/jsonrpc"
)
 
func main() {
    // 这里只需要将 rpc.Dial改为jsonrpc.Dial 即可
        // 或使用原来的 net.Dial拿到句柄后使用        
        // rpc.NewClientWithCodec(jsonrpc.NewClientCodec(conn)) 更换客户端编码
    client, err := jsonrpc.Dial("tcp", "127.0.0.1:1234")
    if err != nil {
        log.Fatal("dialing:", err)
    }
    // 然后,客户端可以执行远程调用:
 
    // Synchronous call
    args := &public.Args{7, 8}
    var reply int
    err = client.Call("Arith.Multiply", args, &reply)
    if err != nil {
        log.Fatal("arith error:", err)
    }
    fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
    // 或:
 
    // Asynchronous call
    quotient := new(public.Quotient)
    divCall := client.Go("Arith.Divide", args, quotient, nil)
    replyCall := <-divCall.Done // will be equal to divCall
    // check errors, print, etc.
    fmt.Printf("%#v\n", replyCall)
}

  

HTTP-RPC

server.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
func main() {
    rpc.RegisterName("HelloService", new(HelloService))
 
    http.HandleFunc("/jsonrpc", func(w http.ResponseWriter, r *http.Request) {
        var conn io.ReadWriteCloser = struct {
            io.Writer
            io.ReadCloser
        }{
            ReadCloser: r.Body,
            Writer:     w,
        }
 
        rpc.ServeRequest(jsonrpc.NewServerCodec(conn))
    })
 
    http.ListenAndServe(":1234", nil)
}

  

  

 
posted @   Binb  阅读(393)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示