Go中来自客户端和服务器的RPC

使用net/rpcGo中的软件包,实际上是否可以从服务器向客户端进行RPC调用?如果没有,是否有更好的解决方案?


一只斗牛犬
浏览 200回答 3
3回答

沧海一幻觉

我目前正在使用thrift(thrift4go)来实现服务器->客户端和客户端->服务器RPC功能。默认情况下,thrift仅像net / rpc一样执行客户端->服务器调用。由于还需要服务器与客户机之间的通信,因此我进行了一些研究,发现bidi-thrift。Bidi-thrift解释了如何连接Java服务器+ Java客户端进行双向节俭通信。比迪蒂节俭是做什么的,它的局限性。TCP连接具有传入和传出的通信线路(RC和TX)。bidi-thrift的想法是将RS和TX分开,并将它们提供给客户端应用程序和服务器应用程序上的服务器(处理器)和客户端(远程)。我发现在Go中很难做到这一点。同样,这种方式没有“响应”的可能(正在使用响应线)。因此,服务中的所有方法都必须“单向无效”。(开火忘了,打电话没有结果)。解决方案我改变了“比迪节约”的概念,使客户端打开了到服务器的两个连接,即A和B。第一个连接(A)用于执行客户端->服务器通信(客户端照常进行呼叫)。第二个连接(B)被“劫持”,并且连接到客户端上的服务器(处理器),而第二个连接(B)连接到服务器上的客户端(远程)。我已经将其与Go服务器和Java客户端一起使用了。效果很好。它既快速又可靠(就像普通的节俭一样)。一些来源。B连接(服务器->客户端)的设置如下:转到服务器// factoriesframedTransportFactory := thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()// create socket listeneraddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9091")if err != nil {&nbsp; &nbsp; log.Print("Error resolving address: ", err.Error(), "\n")&nbsp; &nbsp; return}serverTransport, err := thrift.NewTServerSocketAddr(addr)if err != nil {&nbsp; &nbsp; log.Print("Error creating server socket: ", err.Error(), "\n")&nbsp; &nbsp; return}// Start the server to listen for connectionslog.Print("Starting the server for B communication (server->client) on ", addr, "\n")err = serverTransport.Listen()if err != nil {&nbsp; &nbsp; log.Print("Error during B server: ", err.Error(), "\n")&nbsp; &nbsp; return //err}// Accept new connections and handle thosefor {&nbsp; &nbsp; transport, err := serverTransport.Accept()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return //err&nbsp; &nbsp; }&nbsp; &nbsp; if transport != nil {&nbsp; &nbsp; &nbsp; &nbsp; // Each transport is handled in a goroutine so the server is availiable again.&nbsp; &nbsp; &nbsp; &nbsp; go func() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; useTransport := framedTransportFactory.GetTransport(transport)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client := worldclient.NewWorldClientClientFactory(useTransport, protocolFactory)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Thats it!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Lets do something with the connction&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result, err := client.Hello()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Printf("Errror when calling Hello on client: %s\n", err)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // client.CallSomething()&nbsp; &nbsp; &nbsp; &nbsp; }()&nbsp; &nbsp; }}Java客户端// preparations for B connectionTTransportFactory transportFactory = new TTransportFactory();TProtocolFactory protocolFactory = new TBinaryProtocol.Factory();YourServiceProcessor processor = new YourService.Processor<YourServiceProcessor>(new YourServiceProcessor(this));/* Create thrift connection for B calls (server -> client) */try {&nbsp; &nbsp; // create the transport&nbsp; &nbsp; final TTransport transport = new TSocket("127.0.0.1", 9091);&nbsp; &nbsp; // open the transport&nbsp; &nbsp; transport.open();&nbsp; &nbsp; // add framing to the transport layer&nbsp; &nbsp; final TTransport framedTransport = new TFramedTransport(transportFactory.getTransport(transport));&nbsp; &nbsp; // connect framed transports to protocols&nbsp; &nbsp; final TProtocol protocol = protocolFactory.getProtocol(framedTransport);&nbsp; &nbsp; // let the processor handle the requests in new Thread&nbsp; &nbsp; new Thread() {&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (processor.process(protocol, protocol)) {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (TException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (NullPointerException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }.start();} catch(Exception e) {&nbsp; &nbsp; e.printStackTrace();}

SMILET

RPC是一项(远程)服务。每当某些计算机请求远程服务时,它就充当客户端,要求服务器提供该服务。在此“定义”中,服务器调用客户端RPC的概念没有明确定义的含义。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go