官方链接:https://thrift.apache.org/tutorial/go
我的idl:我的idl目录放在 ../quan-thrift/quan 下的quan_strategy.thrift 文件
namespace go quan_strategy
struct CandleUpRequest {
1:required string ts_code; (go.tag = 'json:"ts_code" bson:"ts_code" form:"ts_code"')
2:required string start_time;
3:required string end_time;
}
struct CandleUpResponse {
1:required list<list<i32>> data;
2:required i32 page;
3:required i32 limit;
}
//定义服务
service QuanStrategy {
CandleUpResponse CandleUp(
1:required CandleUpRequest request
)
}
生成代码(生成go 代码不区分service 和client),如果需要对结构体增加tag 则需要在后面添加注释
比如
struct CandleUpRequest {
1:required string ts_code; (go.tag = 'json:"ts_code" bson:"ts_code" form:"ts_code"')
2:required string start_time;
3:required string end_time;
}
执行下面命令,
指定thrift文件 在当前目录下生成go接口文件
thrift_dir=../quan-thrift/quan
thrift -r --gen go ${thrift_dir}/quan_strategy.thrift
会在本地生成gen-go 目录
如果namespace 为quan_strategy 生成一个gen-go/quan_strategy 目录
目录包含
GoUnusedProtection__.go // 一个定义文件,不需要关注
quan_strategy-consts.go // 定义的常量
quan_strategy.go // 这里存放着我们的定义的结构
quan_strategy-remote/quan_strategy-remote.go // 使用的一些例子。感觉参考价值不大
编写服务端代码参考官网例子
// 先编写 service 接口,
package main
import (
"context"
"crypto/tls"
"fmt"
"github.com/apache/thrift/lib/go/thrift"
"github.com/tankpanv/quan-strategy/gen-go/root/quan/quan_strategy"
)
/*
step 1
先实例化服务类,这里名字不重要。主要是要实现 CandleUp(ctx context.Context,request * quan_strategy.CandleUpRequest) (*quan_strategy.CandleUpResponse,error) 函数,这个函数就是我们的业务函数。
因为实际上 thrift 实际是生成 代码的时候是生成一个interface
type CandleUp interface {
CandleUp(ctx context.Context,request * quan_strategy.CandleUpRequest) (*quan_strategy.CandleUpResponse,error)
}
官网例子 step 1的内容放在handler.go文件
*/
type CandleUpHandler struct {
}
func NewCandleUpHandler() *CandleUpHandler {
return &CandleUpHandler{}
}
func (p *CandleUpHandler) CandleUp(ctx context.Context,request * quan_strategy.CandleUpRequest) (*quan_strategy.CandleUpResponse,error) {
fmt.Print("ping()\n")
return nil,nil
}
// step 2
// 实例化之后,我们再根据观望例子编写创建实例的函数 NewCandleUpHandler() 这里就是调用上面的创建函数创建实例
// 官网例子 step 2 的内容放在service.go 文件中
func runServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string, secure bool) error {
var transport thrift.TServerTransport
var err error
if secure {
cfg := new(tls.Config)
if cert, err := tls.LoadX509KeyPair("server.crt", "server.key"); err == nil {
cfg.Certificates = append(cfg.Certificates, cert)
} else {
return err
}
transport, err = thrift.NewTSSLServerSocket(addr, cfg)
} else {
transport, err = thrift.NewTServerSocket(addr)
}
if err != nil {
return err
}
fmt.Printf("%T\n", transport)
handler := NewCandleUpHandler()
processor := quan_strategy.NewQuanStrategyProcessor(handler)
server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory)
fmt.Println("Starting the simple server... on ", addr)
return server.Serve()
}
// step 3
// 接着我们通过main函数调用 runService 启动实例
// 官网例子 step 3 放在main.go 文件中
func Usage() {
fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
flag.PrintDefaults()
fmt.Fprint(os.Stderr, "\n")
}
func main() {
flag.Usage = Usage
server := flag.Bool("server", true, "Run server")
protocol := flag.String("P", "binary", "Specify the protocol (binary, compact, json, simplejson)")
framed := flag.Bool("framed", false, "Use framed transport")
buffered := flag.Bool("buffered", false, "Use buffered transport")
addr := flag.String("addr", "localhost:20001", "Address to listen to")
secure := flag.Bool("secure", false, "Use tls secure transport")
flag.Parse()
var protocolFactory thrift.TProtocolFactory
switch *protocol {
case "compact":
protocolFactory = thrift.NewTCompactProtocolFactoryConf(nil)
case "simplejson":
protocolFactory = thrift.NewTSimpleJSONProtocolFactoryConf(nil)
case "json":
protocolFactory = thrift.NewTJSONProtocolFactory()
case "binary", "":
protocolFactory = thrift.NewTBinaryProtocolFactoryConf(nil)
default:
fmt.Fprint(os.Stderr, "Invalid protocol specified", protocol, "\n")
Usage()
os.Exit(1)
}
var transportFactory thrift.TTransportFactory
cfg := &thrift.TConfiguration{
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
if *buffered {
transportFactory = thrift.NewTBufferedTransportFactory(8192)
} else {
transportFactory = thrift.NewTTransportFactory()
}
if *framed {
transportFactory = thrift.NewTFramedTransportFactoryConf(transportFactory, cfg)
}
if *server {
if err := runServer(transportFactory, protocolFactory, *addr, *secure); err != nil {
fmt.Println("error running server:", err)
}
}
}