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 TestThrift

struct MyStructReq {
    1:required string reqMessage;
}

struct MyStructResp {
    1:required string respMessage;
}
include "Struct.thrift"

namespace go TestThrift

service 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 main

import (
	"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 = v
		return
	}
	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 main

import (
	"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/[email protected]/lib/go/thrift/deserializer.go:83:12: syntax error: unexpected [, expecting semicolon or newline or }
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pointerize.go:24:13: syntax error: unexpected [, expecting (
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:28:13: syntax error: unexpected any, expecting ]
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:30:16: syntax error: unexpected newline, expecting name or (
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:31:1: syntax error: non-declaration statement outside function body
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:38:13: syntax error: unexpected [, expecting (
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:38:37: method has no receiver
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:40:21: method has no receiver
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:40:21: syntax error: unexpected *, expecting name or (
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:46:16: method has no receiver
../../../go/pkg/mod/github.com/apache/[email protected]/lib/go/thrift/pool.go:46:16: too many errors
note: module requires Go 1.18
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接口文件升级的时候

猜你喜欢

转载自blog.csdn.net/u014704998/article/details/129096842