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),
}

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

查看所有标签

猜你喜欢:

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

C++数据结构与程序设计

C++数据结构与程序设计

克鲁斯 / 钱丽萍 / 清华大学出版社 / 2004-1 / 59.00元

《C++数据结构与程序设计》这本精心制作的课本结合面向对象程序设计和C++强有力的特性,构建数据结构的基本思想,设计了程序和有趣的应用。在此过程中,《C++数据结构与程序设计》探讨了作为软件设计基本工具的问题求解和设计原理、数据抽象、递归和算法的比较分析。《C++数据结构与程序设计》使用真实的案例研究、可重用的软件开发和程序设计项目来增强理解。一起来看看 《C++数据结构与程序设计》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具