golang thrift 客户端/服务端代码记录

栏目: Go · 发布时间: 6年前

内容简介:1,客户端2,服务端3,实现类

1,客户端

package thrift

import (
   "fmt"
   "git.apache.org/thrift.git/lib/go/thrift"
   "net"
   "os"
   "test/idlgen/HelloWorld"
)

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

   transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "9999"))

   if err != nil{
      fmt.Println("err")
      os.Exit(1)
   }

   useTransport := transportFactory.GetTransport(transport)
   client := HelloWorld.NewHelloGoClientFactory(useTransport, protocolFactory)
   if err = transport.Open(); err != nil{
      fmt.Println("err")
      os.Exit(1)
   }
   defer transport.Close()

   res, err := client.Hello("hello")
   if err != nil{
      fmt.Println("err")
      os.Exit(1)
   }
   fmt.Println(res)
}

2,服务端

package thrift

import (
   "fmt"
   "git.apache.org/thrift.git/lib/go/thrift"
   "test/idlgen/HelloWorld"
)

func Server1()  {
   transport, err := thrift.NewTServerSocket(":9999")
   if err != nil{
      fmt.Println("err")
   }

   handler := &HelloGo{}

   processor := HelloWorld.NewHelloGoProcessor(handler)

   transportFactory := thrift.NewTBufferedTransportFactory(8192)
   protocolFactory := thrift.NewTCompactProtocolFactory()

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

   if err := server.Serve(); err != nil{
      fmt.Println("err")
   }
}

3,实现类

package thrift

type HelloGo struct {
   
}

func (hg *HelloGo) Hello(name string) (string, error) {
   return name, nil
}

4,idl定义

namespace php Idlgen.HelloWorld
namespace java Idlgen.HelloWorld
namespace go idlgen.HelloWorld



service HelloGo{
    string hello(1:string name),
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Natural Language Processing with Python

Natural Language Processing with Python

Steven Bird、Ewan Klein、Edward Loper / O'Reilly Media / 2009-7-10 / USD 44.99

This book offers a highly accessible introduction to Natural Language Processing, the field that underpins a variety of language technologies, ranging from predictive text and email filtering to autom......一起来看看 《Natural Language Processing with Python》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具