跨平台

Rpc的请求响应通过tcp协议,服务广播使用udp协议,我们只需要实现网络协议即可。

PHP示例代码

  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:请求,2:状态rpc 各个服务的状态
  10. 'request' => [
  11. 'serviceName' => 'UserService',
  12. 'action' => 'register',//行为名称
  13. 'arg' => [
  14. 'args1' => 'args1',
  15. 'args2' => 'args2'
  16. ]
  17. ]
  18. ];
  19. //$raw = serialize($data);//注意序列化类型,需要和RPC服务端约定好协议 $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数据校验
  23. $data = fread($fp, 65533);
  24. //做长度头部校验
  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. // //这就是服务端返回的结果,
  32. var_dump($data);//默认将返回一个response对象 通过$serializeType修改
  33. }
  34. fclose($fp);

Go示例代码

  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. // 大端字节序(网络字节序)大端就是将高位字节放到内存的低地址端,低位字节放到高地址端。
  18. // 网络传输中(比如TCP/IP)低地址端(高位字节)放在流的开始,对于2个字节的字符串(AB),传输顺序为:A(0-7bit)、B(8-15bit)。
  19. sendData = int32ToBytes8(int32(len(data)))
  20. // 将数据byte拼装到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. //创建Socket对象,连接服务器
  11. Socket socket=new Socket("127.0.0.1",9600);
  12. //通过客户端的套接字对象Socket方法,获取字节输出流,将数据写向服务器
  13. OutputStream out=socket.getOutputStream();
  14. out.write(data);
  15. //读取服务器发回的数据,使用socket套接字对象中的字节输入流
  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. }

其他语言只需要实现tcp协议即可