Go实践:基于Thrift框架的Go-RPC简单示例

HYIndex · · 1973 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Thrift架构简介

Thrift自顶向下可分为四层

  1. Server(single-threaded, event-driven)服务器进程调度

  2. Processor(compiler generated)RPC接口处理函数分发,IDL定义接口的实现将挂接到这里面

  3. Protocol (JSON, compact etc)协议,定义数据传输格式

    • TBinaryProtocol(二进制格式)
    • TCompactProtocol(压缩格式)
    • TJSONProtocol (JSON格式)
    • TDebugProtocol (易看的文本格式,方便debug)
  4. Transport(raw TCP, HTTP etc)网络传输,定义数据传输方式

    • TSocket(阻塞式socket)
    • TServerTransport(服务端模式,非阻塞socket)
    • TFramedTransport(以帧为单位,非阻塞式)
    • TMemoryTransport(内存形式)
    • TFileTransport(文件形式)
    • TZlibTransport(使用zlib压缩,与其他方式联合使用)

Thrift实际上是实现了C/S模式,通过代码生成工具将接口定义文件生成服务器端和客户端代码(可以为不同语言),从而实现服务端和客户端跨语言的支持。

开发环境

系统:macOS Big Sur 11.1
IDE :GoLand 2020.3.4
Thrift:0.14.1

软件安装

安装thrift

brew install thrift # 安装
thrift -version # 查看版本检查是否安装成功

安装thrift support插件

Plugins->Marketplace搜索thrift support,安装后重启IDE即可
如果搜不到可以去官网下载对应版本的安装包本地安装

开发

编写thrift IDL

IDL语法官方文档

user.thrift


namespace go demo

struct User {
    1:required i32 id,
    2:required string name,
    3:required string avatar,
    4:required string address,
    5:required string mobile,
}

struct UserList {
    1:required list<User> userList,
    2:required i32 page,
    3:required i32 limit,
}

service.thrift

include "user.thrift"

// 标记各语言的命名空间(包名),不同语言需要单独声明
namespace go demo

// 重新定义类型名称,同c语言
typedef map<string, string> Data

// 定义响应体结构
struct Response {
    1:required i32 errcode,
    2:required string errmsg,
    3:required Data data,
}

// 定义服务接口,相当于go的interface
service Greeter {
    Response SayHello(
        1:required user.User user
    )

    Response GetUser(
        1:required i32 uid
    )
}

生成目标语言代码

执行命令:thrift -r --gen go service.thrift
生成以下代码文件:

编写golang服务端代码

服务端:

package main

import (
    "context"
    "encoding/json"
    "flag"
    "fmt"
    "github.com/apache/thrift/lib/go/thrift"
    "os"
    "thrift_practice/src/gen-go/demo"
)

func Usage() {
    fmt.Fprint(os.Stderr, "Usage of ", os.Args[0], ":\n")
    flag.PrintDefaults()
    fmt.Fprint(os.Stderr, "\n")
}

//定义服务
type Greeter struct {
}

//实现IDL里定义的接口
//SayHello
func (this *Greeter) SayHello(ctx context.Context, u *demo.User) (r *demo.Response, err error) {
    strJson, _ := json.Marshal(u)
    return &demo.Response{Errcode: 0, Errmsg: "success", Data: map[string]string{"User": string(strJson)}}, nil
}

//GetUser
func (this *Greeter) GetUser(ctx context.Context, uid int32) (r *demo.Response, err error) {
    return &demo.Response{Errcode: 1, Errmsg: "user not exist."}, nil
}

func main() {
    //命令行参数
    flag.Usage = Usage
    addr := flag.String("addr", "localhost:9090", "Address to listen to")
    flag.Parse()

    //protocol
    var protocolFactory thrift.TProtocolFactory
    protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()

    //transport
    var transportFactory thrift.TTransportFactory
    transportFactory = thrift.NewTTransportFactory()

    //handler
    handler := &Greeter{}

    //transport,no secure
    var err error
    var transport thrift.TServerTransport
    transport, err = thrift.NewTServerSocket(*addr)
    if err != nil {
        fmt.Println("error running server:", err)
    }

    //processor
    processor := demo.NewGreeterProcessor(handler)

    fmt.Println("Starting the simple server... on ", *addr)

    //start tcp server
    server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory)
    err = server.Serve()

    if err != nil {
        fmt.Println("error running server:", err)
    }
}

客户端:(借助go testing)

package main

import (
    "context"
    "fmt"
    "github.com/apache/thrift/lib/go/thrift"
    "testing"
    "thrift_practice/src/gen-go/demo"
)

var ctx = context.Background()

func GetClient() *demo.GreeterClient {
    addr := ":9090"
    var transport thrift.TTransport
    var err error
    transport, err = thrift.NewTSocket(addr)
    if err != nil {
        fmt.Println("Error opening socket:", err)
    }

    //protocol
    var protocolFactory thrift.TProtocolFactory
    protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()

    //no buffered
    var transportFactory thrift.TTransportFactory
    transportFactory = thrift.NewTTransportFactory()

    transport, err = transportFactory.GetTransport(transport)
    if err != nil {
        fmt.Println("error running client:", err)
    }

    if err := transport.Open(); err != nil {
        fmt.Println("error running client:", err)
    }

    iprot := protocolFactory.GetProtocol(transport)
    oprot := protocolFactory.GetProtocol(transport)

    client := demo.NewGreeterClient(thrift.NewTStandardClient(iprot, oprot))
    return client
}

//GetUser
func TestGetUser(t *testing.T) {
    client := GetClient()
    rep, err := client.GetUser(ctx, 100)
    if err != nil {
        t.Errorf("thrift err: %v\n", err)
    } else {
        t.Logf("Recevied: %v\n", rep)
    }
}

//SayHello
func TestSayHello(t *testing.T) {
    client := GetClient()

    user := &demo.User{}
    user.Name = "thrift"
    user.Address = "address"

    rep, err := client.SayHello(ctx, user)
    if err != nil {
        t.Errorf("thrift err: %v\n", err)
    } else {
        t.Logf("Recevied: %v\n", rep)
    }
}

运行测试

  1. 运行服务端代码
  2. 运行客户端:go test -v

参考资料

【1】从零开始基于go-thrift创建一个RPC服务
【2】Go Tutorial
【3】Thrift RPC框架指南


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:HYIndex

查看原文:Go实践:基于Thrift框架的Go-RPC简单示例

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1973 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传