大佬教程收集整理的这篇文章主要介绍了08-Thrift高级2,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
# Thrift高级2
由于之前是使用C++做的,有很多不便,这里尝试使用golang制作thrift over http服务和客户端。
有句话说的好,远在天边,尽在眼前。
想要制作这个主题时,google了半天也没有一个靠谱的。实在没办法了,翻了翻test项目,哈哈,就有了现在的服务端。
package main
import (
"fmt"
"net/http"
"testgo/mathservice"
"git.apache.org/thrift.git/lib/go/thrift"
)
const (
NetWorkAddr = "127.0.0.1:9090"
)
type Mymathservice struct {
}
func (this *MymathservicE) Add(A int32,B int32) (r int32,err error) {
r = A + B
err = nil
fmt.Println("Add",A,B)
return
}
func main() {
handler := &Mymathservice{}
processor := mathservice.NewMathserviceProcessor(handler)
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
http.HandleFunc("/",thrift.NewThriftHandlerFunc(processor,protocolFactory,protocolFactory))
fmt.Println("thrift server in http://",NetWorkAddr)
http.ListenAndServe(fmt.Sprintf(":%d", 9090),nil)
}
这里借用了net/http
库的handleFunc功能,实际上,使用gin/beego/...
等Web框架也是可以这样的。完美!!
package main
import (
"fmt"
"os"
"testgo/mathservice"
"git.apache.org/thrift.git/lib/go/thrift"
)
func main() {
client_transport,_ := thrift.NewThttpClient("http://127.0.0.1:9090")
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
client := mathservice.NewMathserviceClientFactory(client_transport,protocolFactory)
if err := client_transport.open(); err != nil {
fmt.Fprintln(os.Stderr,"Error opening socket",err)
os.Exit(1)
}
defer client_transport.Close()
for i := int32(0); i < 100; i++ {
nRet,_ := client.Add(i,i)
fmt.Println(i,"Add",nRet)
}
fmt.Println("Over!")
}
使用了官方自带的thrift.NewThttpClient
,经测试与Cpp服务端通信没有问题。
世界很美丽,生活很美好。
客户端:
以上是大佬教程为你收集整理的08-Thrift高级2全部内容,希望文章能够帮你解决08-Thrift高级2所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。