You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.4 KiB

2 years ago
package main
import (
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
/*
@Auth:ShenZ
@Description:
*/
type User struct {
Name, Pwd string
Age, Score int
}
type Goods struct {
}
// 请求的结构体
type AddGoodsReq struct {
Name string
}
type AddGoodsResp struct {
Success bool
Message string
}
type QueryGoodsReq struct {
Id int
Name string
}
type QueryGoodsResp struct {
Success bool
Goods Goods
Message string
}
func main() {
//1.连接远程http服务
//conn, err := rpc.DialHTTP("tcp", "192.168.100.131:8081")
//推荐jsonrpc的方案
//conn, err := jsonrpc.Dial("tcp", "192.168.100.131:8081")
//备用方案
conn, err := net.Dial("tcp", "192.168.100.131:8081")
if err != nil {
fmt.Println("连接远程服务失败:", err)
}
client := rpc.NewClientWithCodec(jsonrpc.NewClientCodec(conn))
//2.远程服务的调用 参数1 服务的方法 参数2 resq 参数三 resp
//resp := 0
//conn.Call("User.GetScore", User{"xx", "oo", 2, 60}, &resp)
var respGoods AddGoodsResp
var reqGoods = AddGoodsReq{
"测试商品1",
}
client.Call("goods.AddGoods", reqGoods, &respGoods)
fmt.Println("远程调用新增的返回:", respGoods)
var respQueryGoods QueryGoodsResp
var reqQueryGoods = QueryGoodsReq{
1,
"测试商品1",
}
client.Call("Goods.QueryGoods", reqQueryGoods, &respQueryGoods)
fmt.Println("远程调用查询的返回:", respQueryGoods)
}