JSON-RPC

示例

服务端

package main import ( "github.com/sohaha/zlsgo/znet" ) type Result struct { Num, Ans int } type Cal int func (cal *Cal) Square(num int, result *Result) error { result.Num = num result.Ans = num * num return nil } func main() { r := znet.New() // 注册 RPC 服务 r.Any("/rpc", znet.JSONRPC(map[string]interface{}{ "Cal": new(Cal), })) r.SetAddr(":18181") znet.Run() }
 

客户端

client, err := zhttp.NewJSONRPC("http://127.0.0.1:18181", "/rpc") type Result struct { Num, Ans int } var result Result err = client.Call("Cal.Square", 12, &result) // 或者 HTTP POST 调用 res, err := zhttp.Post("http://127.0.0.1:18181/__rpc", BodyJSON(map[string]interface{}{ "method": "Cal.Square", "params": []int{12}, "id": 1, }))