当我使用Apache Thrift PHP客户端并且PHP服务器可以时,但是当我使用Golang作为服务器时,则不行,我在互联网上找不到关于Golang Thrift使用Http模式的任何内容,该文档仅显示了关于套接字的用法Golang模式,所以我需要帮助。
php服务器代码(使用laravel5.2,节俭0.11.0):
try{
header('Content-Type', 'application/x-thrift');
$dokuHandler = new DokuService();
$multiplexedProcessor = new TMultiplexedProcessor();
// 创建多个服务Processor
$dokuProcessor = new DokuServiceProcessor($dokuHandler);
// 将服务注册到TMultiplexedProcessor中
$multiplexedProcessor->registerProcessor("doku", $dokuProcessor);
// 初始化数据传输方式transport
$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
// 利用该传输方式初始化数据传输格式protocol
$protocol = new TBinaryProtocol($transport, true, true);
// 开始服务
$transport->open();
$multiplexedProcessor->process($protocol, $protocol);
} catch (\TException $exception) {
Log::error("DokuService handleRequest", [
'request' => $request,
'exMsg' => $exception->getMessage(),
'exStack' => $exception->getTraceAsString(),
]);
} finally {
if ($transport) {
$transport->close();
}
}
php客户端代码:
public function __construct()
{
$this->transport = null;
try {
$uri = '/rpc/outgo/server';
$serviceName = 'outgo';
$host = self::getHost();
$port = self::getPort();
$socket = new THttpClient($host, $port, $uri);
$this->transport = new TBufferedTransport($socket, 1024, 1024);
$protocol = new TBinaryProtocol($this->transport);
$this->multiplexedProtocol = new TMultiplexedProtocol($protocol, $serviceName);
parent::__construct($this->multiplexedProtocol);
} catch (\Exception $e) {
Log::error('construct OutgoRpcClient error', [
'host' => $host,
'port' => $port,
'serviceName' => $serviceName,
'ex_msg' => $e->getMessage(),
'ex_trace' => $e->getTraceAsString(),
]);
}
}
golang服务器代码(go1.10.3,gin框架):
func RpcReceive(c *gin.Context) {
//defer func() {
// if err := recover(); err != nil {
// config.LogRecord.WithFields(logrus.Fields{"error":
err}).Error("rpc worker error")
// }
//}()
var transport thrift.TTransport
newTransport, err :=
thrift.NewTBufferedTransportFactory(10).GetTransport(transport)
if err != nil {
panic("new transport error")
}
protocol :=
thrift.NewTBinaryProtocolFactoryDefault().GetProtocol(newTransport)
orderProcessor :=
orderservice.NewOrderServiceProcessor(new(orderService))
OrderServiceName := "outgo"
multiProcessor := thrift.NewTMultiplexedProcessor()
multiProcessor.RegisterProcessor(OrderServiceName, orderProcessor)
newTransport.Open()
multiProcessor.Process(c, protocol, protocol)
//newTransport.Close()
}
golang错误:
runtime error: invalid memory address or nil pointer dereference
/usr/local/opt/go/libexec/src/runtime/panic.go:502 (0x102ad08)
gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/local/opt/go/libexec/src/runtime/panic.go:63 (0x1029d7d)
panicmem: panic(memoryError)
/usr/local/opt/go/libexec/src/runtime/signal_unix.go:388 (0x1040279)
sigpanic: panicmem()
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/git.apache.org/thrift.git/lib/go/thrift/buffered_transport.go:58 (0x18c180a)
(*TBufferedTransport).Open: return p.tp.Open()
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/app/http/controller/rpc/receiveBusiness.go:86 (0x18ce4cf)
RpcReceive: newTransport.Open()
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/context.go:104 (0x1537b42)
(*Context).Next: c.handlers[c.index](c)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/app/http/middleware/catchPanic.go:41 (0x16b2e20)
RecoveryWithWriter.func1: c.Next()
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/context.go:104 (0x1537b42)
(*Context).Next: c.handlers[c.index](c)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/app/http/middleware/commonMw.go:16 (0x16b40a4)
Logger.func1: c.Next()
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/context.go:104 (0x1537b42)
(*Context).Next: c.handlers[c.index](c)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/gin.go:332 (0x153ebe4)
(*Engine).handleHTTPRequest: context.Next()
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/gin.go:296 (0x153e432)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/opt/go/libexec/src/net/http/server.go:2694 (0x128093b)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/opt/go/libexec/src/net/http/server.go:1830 (0x127caf0)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/opt/go/libexec/src/runtime/asm_amd64.s:2361 (0x1057030)
goexit: BYTE $0x90 // NOP
] <nil>}
goroutine 33 [running]:
net/http.(*conn).serve.func1(0xc4202da000)
/usr/local/opt/go/libexec/src/net/http/server.go:1726 +0xd0
panic(0x1b06940, 0xc42033e5a0)
/usr/local/opt/go/libexec/src/runtime/panic.go:502 +0x229
github.com/rule-engine/rule-engine/vendor/github.com/sirupsen/logrus.Entry.log(0xc4200d52c0, 0xc4202f4630, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/sirupsen/logrus/entry.go:126 +0x2d2
github.com/rule-engine/rule-engine/vendor/github.com/sirupsen/logrus.(*Entry).Panic(0xc42033e410, 0xc42056b640, 0x1, 0x1)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/sirupsen/logrus/entry.go:194 +0xaa
github.com/rule-engine/rule-engine/vendor/github.com/sirupsen/logrus.(*Logger).Panic(0xc4200d52c0, 0xc42056b640, 0x1, 0x1)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/sirupsen/logrus/logger.go:244 +0x6d
github.com/rule-engine/rule-engine/app/http/middleware.RecoveryWithWriter.func1.1(0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/app/http/middleware/catchPanic.go:35 +0x46b
panic(0x19db200, 0x21320b0)
/usr/local/opt/go/libexec/src/runtime/panic.go:502 +0x229
github.com/rule-engine/rule-engine/vendor/git.apache.org/thrift.git/lib/go/thrift.(*TBufferedTransport).Open(0xc420248080, 0xc4202f4510, 0x1b241cf)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/git.apache.org/thrift.git/lib/go/thrift/buffered_transport.go:58 +0x2a
github.com/rule-engine/rule-engine/app/http/controller/rpc.RpcReceive(0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/app/http/controller/rpc/receiveBusiness.go:86 +0x370
github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin.(*Context).Next(0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/context.go:104 +0x43
github.com/rule-engine/rule-engine/app/http/middleware.RecoveryWithWriter.func1(0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/app/http/middleware/catchPanic.go:41 +0x51
github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin.(*Context).Next(0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/context.go:104 +0x43
github.com/rule-engine/rule-engine/app/http/middleware.Logger.func1(0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/app/http/middleware/commonMw.go:16 +0x85
github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin.(*Context).Next(0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/context.go:104 +0x43
github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc4200dc6c0, 0xc42037e580)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/gin.go:332 +0x585
github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc4200dc6c0, 0x1c29ae0, 0xc42038c000, 0xc42015a000)
/usr/local/var/www/go/src/github.com/rule-engine/rule-engine/vendor/github.com/gin-gonic/gin/gin.go:296 +0x153
net/http.serverHandler.ServeHTTP(0xc4200dc7e0, 0x1c29ae0, 0xc42038c000, 0xc42015a000)
/usr/local/opt/go/libexec/src/net/http/server.go:2694 +0xbc
net/http.(*conn).serve(0xc4202da000, 0x1c2a860, 0xc4201da4c0)
/usr/local/opt/go/libexec/src/net/http/server.go:1830 +0x651
created by net/http.(*Server).Serve
/usr/local/opt/go/libexec/src/net/http/server.go:2795 +0x27b
我请php服务器编写golang服务器代码。