Tag:thrift go
Category:Go
Article From:https://www.cnblogs.com/lanyangsh/p/9059894.html

thrift Originally the RPC communication framework used by Facebook, it was later contributed to the Apache foundation, which came out earlier, almost supported all the back-end languages, and was very widely used as an unknown network framework.

Like grpc, you need to define communication protocols first and then implement your business logic.

The following methods are introduced.

download the go thrift Library

$ go get git.apache.org/thrift.git/lib/go/thrift/...

Note: the repository time is 2017.7 at that time, so context was not supported at that time.

> define protocol file echo.thrift

Create a thriftExample directory in the GOPATH directory and create a file echo.thrift in the directory. The contents are as follows:

namespace go echo

struct EchoReq {
    1: string msg;
}

struct EchoRes {
    1: string msg;
}

service Echo {
    EchoRes echo(1: EchoReq req);
}

protocol conversion

Command format

thrift --gen <language> <Thrift filename>

As for the usage of thrift, you can use thrift -help to view help.

The following command will generate the gen-go folder, which translates the above protocol into golang code.

$ thrift -r --gen go echo.thrift 

The generated gen-go folder includes the following files

[lanyang@localhost thriftExample]$ ll gen-go/ -R
gen-go:
total 4
drwxrwxr-x. 3 lanyang lanyang 4096 May 13 23:18 echo

gen-go/echo:
total 28
-rw-rw-r--. 1 lanyang lanyang   349 May 13 23:18 echo-consts.go
-rw-rw-r--. 1 lanyang lanyang 16161 May 13 23:18 echo.go
drwxrwxr-x. 2 lanyang lanyang  4096 May 13 23:18 echo-remote
-rw-rw-r--. 1 lanyang lanyang   159 May 13 23:18 GoUnusedProtection__.go

gen-go/echo/echo-remote:
total 4
-rwxr-xr-x. 1 lanyang lanyang 4017 May 13 23:18 echo-remote.go

Among them, echo.go is the type and method definition generated, and echo-remote is the client sample code.

sample code”

The following is a simple example of its use.

server-side code

The server code server.go is as follows:

package main

import (

    "fmt"
    "git.apache.org/thrift.git/lib/go/thrift"

    "thriftExample/gen-go/echo"

)


type EchoServer struct {
}

func (e *EchoServer) Echo(req *echo.EchoReq) (*echo.EchoRes, error) {
    fmt.Printf("message from client: %v\n", req.GetMsg())

    res := &echo.EchoRes{
        Msg: "success",
    }

    return res, nil
}

func main() {
    transport, err := thrift.NewTServerSocket(":9898")
    if err != nil {
        panic(err)
    }

    handler := &EchoServer{}
    processor := echo.NewEchoProcessor(handler)

    transportFactory := thrift.NewTBufferedTransportFactory(8192)
    protocolFactory := thrift.NewTCompactProtocolFactory()
    server := thrift.NewTSimpleServer4(
        processor,
        transport,
        transportFactory,
        protocolFactory,
    )

    if err := server.Serve(); err != nil {
        panic(err)
    }
}

client code

The client code client.go is as follows

package main

import (
    "fmt"
    "log"
    "net"
    "os"

    "git.apache.org/thrift.git/lib/go/thrift"

    "thriftExample/gen-go/echo"
)

func main() {
    transportFactory := thrift.NewTBufferedTransportFactory(8192)
    protocolFactory := thrift.NewTCompactProtocolFactory()

    transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "9898"))
    if err != nil {
        fmt.Fprintln(os.Stderr, "error resolving address:", err)
        os.Exit(1)
    }

    useTransport, err := transportFactory.GetTransport(transport)
    client := echo.NewEchoClientFactory(useTransport, protocolFactory)
    if err := transport.Open(); err != nil {
        fmt.Fprintln(os.Stderr, "Error opening socket to 127.0.0.1:9898", " ", err)
        os.Exit(1)
    }
    defer transport.Close()

    req := &echo.EchoReq{Msg:"You are welcome."}
    res, err := client.Echo(req)
    if err != nil {
        log.Println("Echo failed:", err)
        return
    }

    log.Println("response:", res.Msg)
    fmt.Println("well done")

}

server ouput:

$ ./server
message from client: You are welcome.

client output:

$ ./client
2018/05/19 11:12:37 response: success
well done

Note: the protocol used by the server and the client must be the same, for example, the transport factory is buffer transport factroy, and protocol factory is compaCT protcol.

reference

github
https://github.com/apache/thrift/

github go thrift
https://github.com/apache/thrift/tree/master/lib/go

Building Apache Thrift on CentOS 6.5
http://thrift.apache.org/docs/install/centos

How to install thrift tools for different systems
http://thrift.apache.org/docs/install/

go thrift example(It is of great reference value)
https://github.com/apache/thrift/tree/master/tutorial/go/src

golang Thrift of the network framework
https://segmentfault.com/a/1190000013127570

GolangPerfect implementation of cross language calls through the Thrift framework
https://my.oschina.net/qinerg/blog/165285

Install thrift
http://www.cnblogs.com/yjf512/p/4334622.html

https://thrift.apache.org/tutorial/go

Link of this Article: Go thrift use examples

Leave a Reply

Your email address will not be published. Required fields are marked *