1 Star 0 Fork 0

yonglinux/rpcx-examples

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
client.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
鸟窝 提交于 2020-05-04 11:03 +08:00 . move examples to rpcxio/rpcx-examples
package main
import (
"bytes"
"context"
"encoding/gob"
"flag"
"log"
example "github.com/rpcxio/rpcx-examples"
"github.com/rpcxio/rpcx-examples/codec/iterator/codec"
"github.com/smallnest/rpcx/client"
"github.com/smallnest/rpcx/protocol"
"github.com/smallnest/rpcx/share"
)
var (
addr = flag.String("addr", "localhost:8972", "server address")
)
func main() {
flag.Parse()
// register customized codec
share.Codecs[protocol.SerializeType(4)] = &codec.JsoniterCodec{}
option := client.DefaultOption
option.SerializeType = protocol.SerializeType(4)
d := client.NewPeer2PeerDiscovery("tcp@"+*addr, "")
xclient := client.NewXClient("Arith", client.Failtry, client.RandomSelect, d, option)
defer xclient.Close()
args := &example.Args{
A: 10,
B: 20,
}
reply := &example.Reply{}
err := xclient.Call(context.Background(), "Mul", args, reply)
if err != nil {
log.Fatalf("failed to call: %v", err)
}
log.Printf("%d * %d = %d", args.A, args.B, reply.C)
}
type GobCodec struct {
}
func (c *GobCodec) Decode(data []byte, i interface{}) error {
enc := gob.NewDecoder(bytes.NewBuffer(data))
err := enc.Decode(i)
return err
}
func (c *GobCodec) Encode(i interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
err := enc.Encode(i)
return buf.Bytes(), err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yonglinux/rpcx-examples.git
git@gitee.com:yonglinux/rpcx-examples.git
yonglinux
rpcx-examples
rpcx-examples
v1.1.6

搜索帮助