V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  chengxuyuan0917  ›  全部回复第 1 页 / 共 1 页
回复总数  1
2018-09-26 23:42:33 +08:00
回复了 chengxuyuan0917 创建的主题 Go 编程语言 go thrift 和 gprc 的问题
@Raymon111111
我知道怎么玩了,
正常情况下是这样
```
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
transport, err := thrift.NewTSocket(net.JoinHostPort(HOST, PORT))
useTransport,_ := transportFactory.GetTransport(transport)
transport.Open()
client := usersrv.NewUserInfoServiceClientFactory(useTransport, protocolFactory)


然后 client.GetUserById(context.TODO(), id)
```


参考了 gprc.Invoke,观察了下 thrift 的调用方法
```

type UserInfoServiceClient struct {
c thrift.TClient
}

// Deprecated: Use NewUserInfoService instead
func NewUserInfoServiceClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *UserInfoServiceClient {
return &UserInfoServiceClient{
c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)),
}
}```

后来,直接自己构造里面的 thrift.TClient,
stClient:=thrift.NewTStandardClient(protocolFactory.GetProtocol(useTransport), protocolFactory.GetProtocol(useTransport))

var _args0 usersrv.UserInfoServiceGetUserByNameArgs
_args0.Namea = "123"
var _result1 usersrv.UserInfoServiceGetUserByIdResult
stClient.Call(context.Background(),"getUserById", &_args0, &_result1);
//if err = stClient.Call(context.Background(),"getUserById", &_args0, &_result1); err != nil {
// fmt.Println(err)
// return
//}
fmt.Println(_result1.GetSuccess())


前提是知道入参跟出参是什么,不过自定义网关消息的结构是通用的。比如说现在是
usersrv.UserInfoServiceGetUserByNameArgs,usersrv.UserInfoServiceGetUserByIdResult,
以后通用起来可能就是 ApiGateway.MessageRequest , ApiGateway.MessageResponse 这样。只需要接口定义时候确定成通用的格式就好了。
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   751 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 10ms · UTC 20:56 · PVG 04:56 · LAX 13:56 · JFK 16:56
Developed with CodeLauncher
♥ Do have faith in what you're doing.