• go标准库的学习-net/rpc/jsonrpc


    参考:https://studygolang.com/pkgdoc

    导入方式:

    import "net/rpc/jsonrpc"

    jsonrpc包实现了JSON-RPC的ClientCodec和ServerCodec接口,可用于rpc包。

    func Dial

    func Dial(network, address string) (*rpc.Client, error)

    Dial在指定的网络和地址连接一个JSON-RPC服务端

    func ServeConn

    func ServeConn(conn io.ReadWriteCloser)

    ServeConn在单个连接上执行DefaultServer。ServeConn会阻塞,服务该连接直到客户端挂起。调用者一般应另开线程调用本函数:"go serveConn(conn)"。ServeConn在该连接使用JSON编解码格式。

    举例:

    JSON RPC

    服务端:

    package main
    
    import (
        "fmt"
        "net"
        "net/rpc"
        "net/rpc/jsonrpc"
        "errors"
        "os"
    )
    
    type Args struct{
        A, B int
    }
    
    type Quotient struct{
        Quo, Rem int
    }
    
    type Arith int
    
    func (t *Arith) Multiply(args *Args, reply *int) error{
        *reply = args.A * args.B
        return nil
    }
    
    func (t *Arith) Divide(args *Args, quo *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)
    
        tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")//jsonrpc是基于TCP协议的,现在他还不支持http协议
        if err != nil{
            fmt.Println(err.Error())
            os.Exit(1)
        }
    
        listener, err := net.ListenTCP("tcp", tcpAddr)
        if err != nil{
            fmt.Println(err.Error())
            os.Exit(1)
        }
        for{
            conn, err := listener.Accept()
            if err != nil{
                continue
            }
            jsonrpc.ServeConn(conn)
        }    
    }

    客户端:

    package main
    
    import (
        "fmt"
        "net/rpc/jsonrpc"
        "log"
        "os"
    )
    type Args struct{
        A, B int
    }
    
    type Quotient struct{
        Quo, Rem int
    }
    
    func main() {
        if len(os.Args) != 2{
            fmt.Println("Usage: ", os.Args[0], "server:port")
            os.Exit(1)
        }
        service := os.Args[1]
    
        client, err := jsonrpc.Dial("tcp", service)
        if err != nil{
            log.Fatal("dialing : ", err)
        }
    
        //Synchronous call
        args := Args{17, 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 
    ", args.A, args.B, reply)
    
        var quot Quotient
        err = client.Call("Arith.Divide", args, &quot)
        if err != nil{
            log.Fatal("arith error : ", err)
        }
        fmt.Printf("Arith: %d/%d = %d remainder %d
    ", args.A, args.B, quot.Quo, quot.Rem)   
    }

    客户端返回:

    userdeMBP:go-learning user$ go run test.go 127.0.0.1:1234
    Arith: 17*8 = 136 
    Arith: 17/8 = 2 remainder 1

    func NewClient

    func NewClient(conn io.ReadWriteCloser) *rpc.Client

    NewClient返回一个新的rpc.Client,以管理对连接另一端的服务的请求。

    func NewClientCodec

    func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec

    NewClientCodec返回一个在连接上使用JSON-RPC的rpc.ClientCodec。

    func NewServerCodec

    func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec

    NewServerCodec返回一个在连接上使用JSON-RPC的rpc. ServerCodec。

  • 相关阅读:
    vba中数据类型
    Excel统计函数COUNTIF()的常规用法介绍
    分类求和
    在每个sheet的相同位置写入相同的值
    Timer函数
    数组总结
    如何制作本地yum源
    HDFS的优缺点
    HDFS安全模式
    HDFS的HA(高可用)
  • 原文地址:https://www.cnblogs.com/wanghui-garcia/p/10449424.html
Copyright © 2020-2023  润新知