This is a creation in Article, where the information may have evolved or changed.
After three months in the company, he came back and found that the company had already used Facebook's open-source Apache Thrift. Probably looked at the introduction, the traditional interface is written using JSON or XML as an information format for transmission. In the General Web service, SOAP is used in XML (but I never use it.) While the lightweight Network Service rest uses JSON as the transport medium. Compared to XML, the content of the transfer is much less and the transmission is more convenient. Both of these are HTTP-based transmission modes.
and Apache Thrift, is a more lightweight web Service. Undergraduate do online games when also contacted, called RPC (remote Procudure call, long-distance procedure calls). This should also be considered an RPC. It is simpler to call, and there is no need to send HTTP requests to parse the operations that return the results. Just pre-defined classes and corresponding functions, call the time to create a class, call the function. The returned content is also mapped directly into the class.
A complete look at Apache Thrift, this article mainly introduces Hello, world's writing. If you have not touched thrift before, you can refer to several articles I have written earlier so-called reference.
- Apache Thrift Installation-How to complete offline go get--install Apache Thrift
- Compiling the official example involves parsing the command line Arguments--golang processing command line startup parameters
- Server-side to implement a well-defined interface, interface-related knowledge--golang interface implementation
Apache Thrift gives a complete example that contains many language versions, and Golang's complete code can be seen here. Documents on the official website did not introduce how much of this example, the code is not affixed to the whole, find this is good to do more, directly copied down the compilation on the line. For other languages, return to the top-level directory.
This article is about Hello, world is mainly based on Developworks's article Apache Thrift-A scalable cross-language service development framework.
The Thrfit file is defined as follows: Hello.thrift. Interface Hello, which contains five functions, five must be implemented, otherwise the compiler will assume that the interface is not implemented.
namespace java service.demo service Hello{ string helloString(1:string para) i32 helloInt(1:i32 para) bool helloBoolean(1:bool para) void helloVoid() string helloNull() }
.thrfit
A file is defined as a set of interfaces. Commands enable you to generate an interface definition for the corresponding language. The folder will be generated in the current directory gen-go
, the contents of the inside can be put gopath
in.
$ thrift --gen go Hello.thrift
The automatically generated interface is defined as follows hello.go
:
type Hello interface {// Parameters:// - ParaHelloString(para string) (r string, err error)// Parameters:// - ParaHelloInt(para int32) (r int32, err error)// Parameters:// - ParaHelloBoolean(para bool) (r bool, err error)HelloVoid() (err error)HelloNull() (r string, err error)}
The above operation is used to build the interface, after having the interface, is to implement the interface, development client and development server side of these three parts. Examples of reference are implemented using large Java and are decisively rewritten with go helloimpl.go
. The Go Language implementation interface can refer to the Golang interface implementation.
type HelloHandler struct {}func NewHelloHandler() *HelloHandler {return &HelloHandler{}}func (h *HelloHandler) HelloString(para string) (string, error) {return "hello, world", nil}func (h *HelloHandler) HelloBoolean(para bool) (r bool, err error) {return para, nil}func (h *HelloHandler) HelloInt(para int32) (r int32, err error) {return para, nil}func (h *HelloHandler) HelloVoid() (err error) {return nil}func (h *HelloHandler) HelloNull() (r string, err error) {return "hello null", nil}
Server-side implementationhelloserver.go
func runServer(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string) error {var err errortransport, err = thrift.NewTServerSocket(addr)if err != nil {fmt.Println(err)}fmt.Printf("%T\n", transport)handler := NewHelloHandler()processor := hello.NewHelloProcessor(handler)server := thrift.NewTSimpleServer4(processor, transport, transportFactory, protocolFactory)fmt.Println("Starting the simple server... on ", addr)return server.Serve()}func main() {transportFactory := thrift.NewTTransportFactory()protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()transportFactory = thrift.NewTBufferedTransportFactory(8192)runServer(transportFactory, protocolFactory, "localhost:9090")}
Start the server
$ go run helloserver.go helloimpl.goStarting the simple server... on localhost:9090
Client code helloclient.go
func handleClient(client *hello.HelloClient) error {str, err := client.HelloString("")fmt.Println(str)fmt.Println("----------------")return err}func runClient(transportFactory thrift.TTransportFactory, protocolFactory thrift.TProtocolFactory, addr string) error {var transport thrift.TTransportvar err errortransport, err = thrift.NewTSocket(addr)if err != nil {fmt.Println("Error opening socket:", err)return err}transport = transportFactory.GetTransport(transport)defer transport.Close()if err := transport.Open(); err != nil {return err}fmt.Println(transport, protocolFactory)return handleClient(hello.NewHelloClientFactory(transport, protocolFactory))}func main() {transportFactory := thrift.NewTTransportFactory()protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()transportFactory = thrift.NewTBufferedTransportFactory(8192)runClient(transportFactory, protocolFactory, "localhost:9090")}
To start the client and run the results:
$ go run helloclient.gohello, world
Reference documents
- "1" Apache Thrift-Scalable cross-language service development framework-Developworks
Original link: Golang Development thrift Interface, reproduced please indicate the source!