我正在使用Thrift。 这是节俭文件:
namespace py hello
namespace go hello
service Hello{
string helloString(1:string para)
}
然后我生成python和golang代码
thrift -gen py Hello.thrift
thrift -gen go Hello.thrift
Python服务器代码:py-server.py
import socket
import sys
sys.path.append('./gen-py/')
from hello import Hello
from hello.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
class HelloHandler:
def helloString(self, msg):
ret = "helloString Received: " + msg
print ret
return msg
handler = HelloHandler()
processor = Hello.Processor(handler)
transport = TSocket.TServerSocket("127.0.0.1", 19090)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print "Starting thrift server in python..."
server.serve()
我可以顺利运行:
$ python py-server.py
Starting thrift server in python...
我的客户是golang写的,go-client.go:
package main
import (
"./gen-go/hello"
"fmt"
"git.apache.org/thrift.git/lib/go/thrift"
"net"
"os"
)
func main() {
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
transport, err := thrift.NewTSocket(net.JoinHostPort("127.0.0.1", "19090"))
fmt.Println(1)
if err != nil {
fmt.Fprintln(os.Stderr, "error resolving address:", err)
os.Exit(1)
}
useTransport := transportFactory.GetTransport(transport)
client := hello.NewHelloClientFactory(useTransport, protocolFactory)
fmt.Println(2)
if err := transport.Open(); err != nil {
fmt.Println(3)
fmt.Fprintln(os.Stderr, "localhsot:19090", " ", err)
os.Exit(1)
}
defer transport.Close()
fmt.Println(4)
r1, e1 := client.HelloString("lalalalalalalallalaal")
fmt.Println(5)
fmt.Println("Call->", r1, e1)
}
但它无法运行,输出,无法正常停止
$ go run go-client.go
1
2
4
并且服务器不会收到任何消息。
我的golang代码有问题吗?
如果我用python编写客户端,它可以运行良好..
import socket
import sys
sys.path.append('./gen-py/')
from hello import Hello
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
try:
transport = TSocket.TSocket('127.0.0.1', 19090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hello.Client(protocol)
transport.open()
print "client - helloString"
msg = client.helloString("lalalalalla")
print "server - " + msg
transport.close()
except Thrift.TException, ex:
print "%s" % (ex.message)
$ python py-client.py
client - helloString
server - lalalalalla
答案 0 :(得分:0)
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
请将NewTFramedTransportFactory
更改为NewTBufferedTransportFactory
,就像transportFactory := thrift.NewTBufferedTransportFactory(1024)
一样,谢谢。