简体   繁体   English

PHP 客户端和 Golang 服务器中的 Apache thrift 错误

[英]Apache thrift err in PHP Client and Golang Server

when i use Apache Thrift PHP client and PHP server is OK, but When I use Golang as Server, is not OK, I find nothing in the internet that about Golang thrift use Http mode,The document only show the usage about socket mode in Golang,so i need help.当我使用Apache Thrift PHP客户端和PHP服务器时是可以的,但是当我使用Golang作为Server时,就不行,我在网上找不到关于Golang thrift使用Http模式的内容,文档只展示了Golang中socket模式的用法,所以我需要帮助。

php server code(use laravel5.2, thrift 0.11.0): php 服务器代码(使用 laravel5.2,thrift 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 client code: 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 server code(go1.10.3, gin framework,): 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 err: 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

I refer php server to write golang server code.我参考 php 服务器来编写 golang 服务器代码。

You can use thrift http in golang like below您可以在 golang 中使用 thrift http,如下所示

var protocolFactory = thrift.NewTJSONProtocolFactory()

handler := New<Your_Thrift_Handler>()
processor := New<Your_Thrift_Processor>(handler)

var thriftHandlerFunc = thrift.NewThriftHandlerFunc(processor, protocolFactory, protocolFactory)

http.HandleFunc("/<your_thrift_func_url>", thriftHandlerFunc)
http.ListenAndServe(":8080", nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM