go实现rpc服务

2022-12-31  本文已影响0人  耍帅oldboy

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)
}

上一篇下一篇

猜你喜欢

热点阅读