I am using Thrift. Here is the thrift file:
namespace py hello
namespace go hello
service Hello{
string helloString(1:string para)
}
And I then generate the python and golang code
thrift -gen py Hello.thrift
thrift -gen go Hello.thrift
Python server code: 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()
and i can run well:
$ python py-server.py
Starting thrift server in python...
And my client is writen by 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)
}
But it can't run, it output, and it can not stop normally
$ go run go-client.go
1
2
4
and the server do not receive any message.
Is there any problem in my golang code?
if i write a client by python , it can run well..
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
transportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())
please change NewTFramedTransportFactory
to NewTBufferedTransportFactory
, just like transportFactory := thrift.NewTBufferedTransportFactory(1024)
, thanks.