title: Rpc跨平台 meta:

  • name: description content: RPC cross-platform implementation in EasySwoole
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|Rpc cross-platform|swoole RPC|RPC

Cross-platform

Rpc’s request response is through the tcp protocol, and the service broadcast uses the udp protocol. We only need to implement the network protocol.

PHP sample code

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xcg
  5. * Date: 2019/6/17
  6. * Time: 14:30
  7. */
  8. $data = [
  9. 'command' => 1,//1:Request, 2: status rpc status of each service
  10. 'request' => [
  11. 'serviceName' => 'UserService',
  12. 'action' => 'register',//Behavior name
  13. 'arg' => [
  14. 'args1' => 'args1',
  15. 'args2' => 'args2'
  16. ]
  17. ]
  18. ];
  19. //$raw = serialize($data);//Note the serialization type, you need to agree with the RPC server agreement $serializeType
  20. $raw = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
  21. $fp = stream_socket_client('tcp://127.0.0.1:9600');
  22. fwrite($fp, pack('N', strlen($raw)) . $raw);//Pack data check
  23. $data = fread($fp, 65533);
  24. //Length check
  25. $len = unpack('N', $data);
  26. $data = substr($data, '4');
  27. if (strlen($data) != $len[1]) {
  28. echo 'data error';
  29. } else {
  30. $data = json_decode($data, true);
  31. // This is the result returned by the server.,
  32. var_dump($data); // By default, a response object will be returned. Modify by $serializeType
  33. }
  34. fclose($fp);

Go sample code

  1. package main
  2. import (
  3. "encoding/binary"
  4. "net"
  5. )
  6. func main() {
  7. var tcpAddr *net.TCPAddr
  8. tcpAddr,_ = net.ResolveTCPAddr("tcp","127.0.0.1:9600")
  9. conn,_ := net.DialTCP("tcp",nil,tcpAddr)
  10. defer conn.Close()
  11. sendEasyswooleMsg(conn)
  12. }
  13. func sendEasyswooleMsg(conn *net.TCPConn) {
  14. var sendData []byte
  15. data := `{"command":1,"request":{"serviceName":"UserService","action":"register","arg":{"args1":"args1","args2":"args2"}}}`
  16. b := []byte(data)
  17. // The big endian (network byte order) big end is to put the high byte to the low address end of the memory, and the low byte to the high address end.
  18. // In the network transmission (such as TCP/IP), the low address end (high byte) is placed at the beginning of the stream. For the 2-byte string (AB), the transmission order is:A(0-7bit)、B(8-15bit)。
  19. sendData = int32ToBytes8(int32(len(data)))
  20. // Assemble the data byte to the back of sendData
  21. for _, value := range b {
  22. sendData = append(sendData, value)
  23. }
  24. conn.Write(sendData)
  25. }
  26. func int32ToBytes8(n int32) []byte {
  27. var buf = make([]byte, 4)
  28. binary.BigEndian.PutUint32(buf, uint32(n))
  29. return buf
  30. }

Java

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.OutputStream;
  4. import java.net.Socket;
  5. public class Main {
  6. public static void main(String[] args) throws IOException {
  7. byte[] msg = "{\"command\":1,\"request\":{\"serviceName\":\"UserService\",\"action\":\"register\",\"arg\":{\"args1\":\"args1\",\"args2\":\"args2\"}}}".getBytes();
  8. byte[] head = Main.toLH(msg.length);
  9. byte[] data = Main.mergeByteArr(head, msg);
  10. //Create a Socket object and connect to the server
  11. Socket socket=new Socket("127.0.0.1",9600);
  12. //Get the byte output stream and write the data to the server through the socket object Socket method of the client.
  13. OutputStream out=socket.getOutputStream();
  14. out.write(data);
  15. //Read the data sent back by the server, using the byte input stream in the socket socket object
  16. InputStream in=socket.getInputStream();
  17. byte[] response=new byte[1024];
  18. int len=in.read(response);
  19. System.out.println(new String(response,4, len-4));
  20. socket.close();
  21. }
  22. static byte[] toLH(int n) {
  23. byte[] b = new byte[4];
  24. b[3] = (byte) (n & 0xff);
  25. b[2] = (byte) (n >> 8 & 0xff);
  26. b[1] = (byte) (n >> 16 & 0xff);
  27. b[0] = (byte) (n >> 24 & 0xff);
  28. return b;
  29. }
  30. static byte[] mergeByteArr(byte[] a, byte[] b) {
  31. byte[] c= new byte[a.length + b.length];
  32. System.arraycopy(a, 0, c, 0, a.length);
  33. System.arraycopy(b, 0, c, a.length, b.length);
  34. return c;
  35. }
  36. }

::: warning Other languages only need to implement the tcp protocol. :::