美文网首页
go实现rpc服务

go实现rpc服务

作者: 耍帅oldboy | 来源:发表于2022-12-31 20:55 被阅读0次

    Server端

    package main
    
    import (
        "fmt"
        "net"
        "net/rpc"
    )
    
    type Goods struct{}
    
    type AddGoodsReq struct {
        Id      int
        Title   string
        Price   float32
        Content string
    }
    
    type AddGoodsRes struct {
        Success bool
        Msg     string
    }
    
    func (this Goods) AddGoods(req AddGoodsReq, res *AddGoodsRes) error {
        fmt.Printf("%#v", req)
        *res = AddGoodsRes{
            Success: true,
            Msg:     "成功",
        }
        return nil
    }
    
    func main() {
        err1 := rpc.RegisterName("goods", new(Goods))
        if err1 != nil {
            fmt.Println(err1)
            return
        }
        listener, err2 := net.Listen("tcp", "127.0.0.1:8080")
        if err2 != nil {
            fmt.Println(err2)
            return
        }
        defer listener.Close()
        for {
            fmt.Println("建立链接")
            conn, err3 := listener.Accept()
            if err3 != nil {
                fmt.Println(err3)
                return
            }
            go rpc.ServeConn(conn)
        }
    }
    
    

    client端

    package main
    
    import (
        "fmt"
        "net/rpc"
    )
    
    type AddGoodsReq struct {
        Id      int
        Title   string
        Price   float32
        Content string
    }
    
    type AddGoodsRes struct {
        Success bool
        Msg     string
    }
    
    func main() {
        conn, err1 := rpc.Dial("tcp", "127.0.0.1:8080")
        if err1 != nil {
            fmt.Println(err1)
            return
        }
        defer conn.Close()
        var res AddGoodsRes
        req := AddGoodsReq{
            Id:      1111,
            Title:   "哈哈",
            Price:   18.0,
            Content: "内容",
        }
        err2 := conn.Call("goods.AddGoods", req, &res)
        if err2 != nil {
            fmt.Println(err2)
        }
        fmt.Printf("%#v", res)
    }
    
    

    改造成json协议通信
    server

    package main
    
    import (
        "fmt"
        "net"
        "net/rpc"
        "net/rpc/jsonrpc"
    )
    
    type Goods struct{}
    
    type AddGoodsReq struct {
        Id      int
        Title   string
        Price   float32
        Content string
    }
    
    type AddGoodsRes struct {
        Success bool
        Msg     string
    }
    
    func (this Goods) AddGoods(req AddGoodsReq, res *AddGoodsRes) error {
        fmt.Printf("%#v", req)
        *res = AddGoodsRes{
            Success: true,
            Msg:     "成功",
        }
        return nil
    }
    
    func main() {
        err1 := rpc.RegisterName("goods", new(Goods))
        if err1 != nil {
            fmt.Println(err1)
            return
        }
        listener, err2 := net.Listen("tcp", "127.0.0.1:8080")
        if err2 != nil {
            fmt.Println(err2)
            return
        }
        defer listener.Close()
        for {
            fmt.Println("建立链接")
            conn, err3 := listener.Accept()
            if err3 != nil {
                fmt.Println(err3)
                return
            }
            //go rpc.ServeConn(conn)
            go rpc.ServeCodec(jsonrpc.NewServerCodec(conn))
        }
    }
    
    

    client

    package main
    
    import (
        "fmt"
        "net"
        "net/rpc"
        "net/rpc/jsonrpc"
    )
    
    type AddGoodsReq struct {
        Id      int
        Title   string
        Price   float32
        Content string
    }
    
    type AddGoodsRes struct {
        Success bool
        Msg     string
    }
    
    func main() {
        conn, err1 := net.Dial("tcp", "127.0.0.1:8080")
        if err1 != nil {
            fmt.Println(err1)
            return
        }
        defer conn.Close()
        client := rpc.NewClientWithCodec(jsonrpc.NewClientCodec(conn))
        var res AddGoodsRes
        req := AddGoodsReq{
            Id:      1111,
            Title:   "哈哈",
            Price:   18.0,
            Content: "内容",
        }
        err2 := client.Call("goods.AddGoods", req, &res)
        if err2 != nil {
            fmt.Println(err2)
        }
        fmt.Printf("%#v", res)
    }
    
    

    相关文章

      网友评论

          本文标题:go实现rpc服务

          本文链接:https://www.haomeiwen.com/subject/ckpqcdtx.html