thrift go

官方链接: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)
		}
	} 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值