当前位置: 首页 > news >正文

RPC-thrift实践

参考:https://www.cnblogs.com/52fhy/p/11146047.html
参考:https://juejin.cn/post/7138032523648598030

实践

  • 安装thrift
    • brew install thrift
    • thrift -version
  • 编写thrift文件
    • 新建文件夹thrift
    • 新建文件
      • 结构体文件
        • Struct.thrift
      • 服务文件
        • Service.thrift
namespace go TestThriftstruct MyStructReq {1:required string reqMessage;
}struct MyStructResp {1:required string respMessage;
}
include "Struct.thrift"namespace go TestThriftservice MyService {Struct.MyStructResp MyQuery(1:required Struct.MyStructReq req)
}
  • 生成idl文件
    • Interface Definition Language
    • thrift -r --gen go XXX.thrift
  • 编写服务程序(go)
    • cd test
    • mkdir thrift_go_service
    • go mod init thrift_go_service
    • 生成idl文件,代码中需要依赖这个
      • thrift -r --gen go ~/PycharmProjects/thrift/Service.thrift
    • 编写代码
package mainimport ("context""fmt""github.com/apache/thrift/lib/go/thrift""net""os""strconv""thrift_go_service/gen-go/TestThrift"
)type MyHandler struct {
}func (my *MyHandler) MyQuery(ctx context.Context, req *TestThrift.MyStructReq) (resp *TestThrift.MyStructResp, err error) {resp = &TestThrift.MyStructResp{}s := map[string]string{"hhh":  "nihao","what": "why",}if v, ok := s[req.ReqMessage]; ok {resp.RespMessage = vreturn}resp.RespMessage = strconv.Itoa(len(req.ReqMessage))return
}func main() {transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()serverTransport, err := thrift.NewTServerSocket(net.JoinHostPort("localhost", "9001"))if err != nil {fmt.Println("err:", err)os.Exit(1)}handler := &MyHandler{}processor := TestThrift.NewMyServiceProcessor(handler)server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)err = server.Serve()if err != nil {fmt.Println("err:", err)}
}
  • 编写客户端的代码
    • mkdir thrift_go_client
    • go mod init thrift_go_client
    • 生成idl文件,代码中需要依赖这个
      • thrift -r --gen go ~/PycharmProjects/thrift/Service.thrift
    • 编写代码
package mainimport ("context""fmt""github.com/apache/thrift/lib/go/thrift""net""os""thrift_go_client/gen-go/TestThrift"
)func main() {tSocket, err := thrift.NewTSocket(net.JoinHostPort("localhost", "9001"))if err != nil {fmt.Println("err:", err)os.Exit(1)}transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())transport, _ := transportFactory.GetTransport(tSocket)protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()client := TestThrift.NewMyServiceClientFactory(transport, protocolFactory)defer transport.Close()if err := transport.Open(); err != nil {fmt.Println("err:", err)os.Exit(1)}ctx := context.Background()d, err := client.MyQuery(ctx, &TestThrift.MyStructReq{ReqMessage: "hhh"})fmt.Println(d)
}

问题

go版本问题

启动service服务时候

 ✘ XXX@XXX  ~/XXX/GoProjects/thrift_go_service  go run main.go
# github.com/apache/thrift/lib/go/thrift
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/deserializer.go:83:12: syntax error: unexpected [, expecting semicolon or newline or }
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pointerize.go:24:13: syntax error: unexpected [, expecting (
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:28:13: syntax error: unexpected any, expecting ]
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:30:16: syntax error: unexpected newline, expecting name or (
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:31:1: syntax error: non-declaration statement outside function body
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:38:13: syntax error: unexpected [, expecting (
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:38:37: method has no receiver
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:40:21: method has no receiver
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:40:21: syntax error: unexpected *, expecting name or (
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:46:16: method has no receiver
../../../go/pkg/mod/github.com/apache/thrift@v0.17.0/lib/go/thrift/pool.go:46:16: too many errors
note: module requires Go 1.18
  • 提示需要升级go版本
    • brew install go@1.18
    • 切换版本:
      • 参考:https://learnku.com/go/wikis/61549
      • brew unlink go
      • brew link go@1.18
      • go version
ide的thrift文件打开方式
  • ide中可以配置thrift插件
    • 参考:https://blog.csdn.net/x369201170/article/details/51604284
  • ide中修改.thrift文件的打开方式
    • 参考:https://blog.csdn.net/qq_25046261/article/details/81666118
    • 安装了thrift插件之后,可以找到对应的默认打开方式
      • thrift的执行文件地址:which thrift
  • 查看thrift的执行目录
    • 参考:https://www.cnblogs.com/MakeView660/p/11398492.html
      * which thrift
如果服务端和客户端依赖的idl文件不一样,能行吗
  • 可行
    • 只要结构体名、函数名一致
  • 这种情况出现在idl接口文件升级的时候
http://www.lryc.cn/news/11910.html

相关文章:

  • Maven:工程的拆分与聚合
  • 使用uniapp创建小程序和H5界面
  • 密度峰值聚类算法(DPC)
  • RabbitMQ相关问题
  • 操作系统 三(存储管理)
  • day34 贪心算法 | 860、柠檬水找零 406、根据身高重建队列 452、用最少数量的箭引爆气球
  • 使用canvas给上传的整张图片添加平铺的水印
  • [安装之4] 联想ThinkPad 加装固态硬盘教程
  • Java数据类型、基本与引用数据类型区别、装箱与拆箱、a=a+b与a+=b区别
  • GoLang设置gofmt和goimports自动格式化
  • 【k8s】如何搭建搭建k8s服务器集群(Kubernetes)
  • DIDL4_前向传播与反向传播(模型参数的更新)
  • 链表学习之链表划分
  • (考研湖科大教书匠计算机网络)第五章传输层-第一、二节:传输层概述及端口号、复用分用等概念
  • C#:Krypton控件使用方法详解(第七讲) ——kryptonHeader
  • 5年软件测试工程师分享的自动化测试经验,一定要看
  • 什么是猜疑心理?小猫测试网科普小作文
  • Redis命令行对常用数据结构String、list、set、zset、hash等增删改查操作
  • mycobot 使用教程
  • JVM学习总结,虚拟机性能监控、故障处理工具:jps、jstat、jinfo、jmap、Visual VM、jstack等
  • 指针笔记(指针数组和指向数组的指针,数组中a和a的区别等)
  • MySQL ---基础概念
  • 【基础】Flink -- ProcessFunction
  • JavaEE|网络编程基础与Socket套接字
  • 【SpringBoot】基础协议及邮件配置整合
  • pytorch配置—什么是CUDA,什么是CUDNN、在配置pytorch虚拟环境中遇到的问题、在安装gpu—pytorch中遇到的问题
  • jfr引起的一次jvm异常记录
  • Java智慧校园平台源码:SaaS模式智慧校园运营云平台源码
  • 【yolov5】将标注好的数据集进行划分(附完整可运行python代码)
  • es-05分词器