08-Thrift高级2
发布时间:2020-12-16 18:06:23 所属栏目:大数据 来源:网络整理
导读:# Thrift高级2 由于之前是使用C++做的,有很多不便,这里尝试使用golang制作thrift over http服务和客户端。 服务器 有句话说的好,远在天边,尽在眼前。 想要制作这个主题时,google了半天也没有一个靠谱的。实在没办法了,翻了翻test项目,哈哈,就有了现
# Thrift高级2 服务器有句话说的好,远在天边,尽在眼前。 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)
}
这里借用了 客户端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!")
}
使用了官方自带的 结论世界很美丽,生活很美好。 计划后续会将常见功能集成到服务中来,大概包含一下几点内容:
客户端:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |