ITeye博客

一个简易RPC框架

windpoplar · 2017-01-29 13:49 · 分布式系统 · 互联网 · Markdown

转:javatar.iteye.com/blog/1123915

因为要给百技上实训课,让新同学们自行实现一个简易RPC框架,在准备PPT时,就想写个示例,发现原来一个RPC框架只要一个类,10来分钟就可以写完了,虽然简陋,也晒晒: 

Java代码  收藏代码

  1. /*
  2. * Copyright 2011 Alibaba.com All right reserved. This software is the
  3. * confidential and proprietary information of Alibaba.com ("Confidential
  4. * Information"). You shall not disclose such Confidential Information and shall
  5. * use it only in accordance with the terms of the license agreement you entered
  6. * into with Alibaba.com.
  7. */
  8. package com.alibaba.study.rpc.framework;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.lang.reflect.InvocationHandler;
  12. import java.lang.reflect.Method;
  13. import java.lang.reflect.Proxy;
  14. import java.net.ServerSocket;
  15. import java.net.Socket;
  16. /**
  17. * RpcFramework
  18. *
  19. * @author william.liangf
  20. */
  21. public class RpcFramework {
  22. /**
  23. * 暴露服务
  24. *
  25. * @param service 服务实现
  26. * @param port 服务端口
  27. * @throws Exception
  28. */
  29. public static void export(final Object service, int port) throws Exception {
  30. if (service == null)
  31. throw new IllegalArgumentException("service instance == null");
  32. if (port <= 0 || port > 65535)
  33. throw new IllegalArgumentException("Invalid port " + port);
  34. System.out.println("Export service " + service.getClass().getName() + " on port " + port);
  35. ServerSocket server = new ServerSocket(port);
  36. for(;;) {
  37. try {
  38. final Socket socket = server.accept();
  39. new Thread(new Runnable() {
  40. @Override
  41. public void run() {
  42. try {
  43. try {
  44. ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
  45. try {
  46. String methodName = input.readUTF();
  47. Class<?>[] parameterTypes = (Class<?>[])input.readObject();
  48. Object[] arguments = (Object[])input.readObject();
  49. ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
  50. try {
  51. Method method = service.getClass().getMethod(methodName, parameterTypes);
  52. Object result = method.invoke(service, arguments);
  53. output.writeObject(result);
  54. } catch (Throwable t) {
  55. output.writeObject(t);
  56. } finally {
  57. output.close();
  58. }
  59. } finally {
  60. input.close();
  61. }
  62. } finally {
  63. socket.close();
  64. }
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. }).start();
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. /**
  76. * 引用服务
  77. *
  78. * @param <T> 接口泛型
  79. * @param interfaceClass 接口类型
  80. * @param host 服务器主机名
  81. * @param port 服务器端口
  82. * @return 远程服务
  83. * @throws Exception
  84. */
  85. @SuppressWarnings("unchecked")
  86. public static <T> T refer(final Class<T> interfaceClass, final String host, final int port) throws Exception {
  87. if (interfaceClass == null)
  88. throw new IllegalArgumentException("Interface class == null");
  89. if (! interfaceClass.isInterface())
  90. throw new IllegalArgumentException("The " + interfaceClass.getName() + " must be interface class!");
  91. if (host == null || host.length() == 0)
  92. throw new IllegalArgumentException("Host == null!");
  93. if (port <= 0 || port > 65535)
  94. throw new IllegalArgumentException("Invalid port " + port);
  95. System.out.println("Get remote service " + interfaceClass.getName() + " from server " + host + ":" + port);
  96. return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[] {interfaceClass}, new InvocationHandler() {
  97. public Object invoke(Object proxy, Method method, Object[] arguments) throws Throwable {
  98. Socket socket = new Socket(host, port);
  99. try {
  100. ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
  101. try {
  102. output.writeUTF(method.getName());
  103. output.writeObject(method.getParameterTypes());
  104. output.writeObject(arguments);
  105. ObjectInputStream input = new ObjectInputStream(socket.getInputStream());
  106. try {
  107. Object result = input.readObject();
  108. if (result instanceof Throwable) {
  109. throw (Throwable) result;
  110. }
  111. return result;
  112. } finally {
  113. input.close();
  114. }
  115. } finally {
  116. output.close();
  117. }
  118. } finally {
  119. socket.close();
  120. }
  121. }
  122. });
  123. }
  124. }

用起来也像模像样: 

(1) 定义服务接口 

Java代码  收藏代码

  1. /*
  2. * Copyright 2011 Alibaba.com All right reserved. This software is the
  3. * confidential and proprietary information of Alibaba.com ("Confidential
  4. * Information"). You shall not disclose such Confidential Information and shall
  5. * use it only in accordance with the terms of the license agreement you entered
  6. * into with Alibaba.com.
  7. */
  8. package com.alibaba.study.rpc.test;
  9. /**
  10. * HelloService
  11. *
  12. * @author william.liangf
  13. */
  14. public interface HelloService {
  15. String hello(String name);
  16. }

(2) 实现服务 

Java代码  收藏代码

  1. /*
  2. * Copyright 2011 Alibaba.com All right reserved. This software is the
  3. * confidential and proprietary information of Alibaba.com ("Confidential
  4. * Information"). You shall not disclose such Confidential Information and shall
  5. * use it only in accordance with the terms of the license agreement you entered
  6. * into with Alibaba.com.
  7. */
  8. package com.alibaba.study.rpc.test;
  9. /**
  10. * HelloServiceImpl
  11. *
  12. * @author william.liangf
  13. */
  14. public class HelloServiceImpl implements HelloService {
  15. public String hello(String name) {
  16. return "Hello " + name;
  17. }
  18. }

(3) 暴露服务 

Java代码  收藏代码

  1. /*
  2. * Copyright 2011 Alibaba.com All right reserved. This software is the
  3. * confidential and proprietary information of Alibaba.com ("Confidential
  4. * Information"). You shall not disclose such Confidential Information and shall
  5. * use it only in accordance with the terms of the license agreement you entered
  6. * into with Alibaba.com.
  7. */
  8. package com.alibaba.study.rpc.test;
  9. import com.alibaba.study.rpc.framework.RpcFramework;
  10. /**
  11. * RpcProvider
  12. *
  13. * @author william.liangf
  14. */
  15. public class RpcProvider {
  16. public static void main(String[] args) throws Exception {
  17. HelloService service = new HelloServiceImpl();
  18. RpcFramework.export(service, 1234);
  19. }
  20. }

(4) 引用服务 

Java代码  收藏代码

  1. /*
  2. * Copyright 2011 Alibaba.com All right reserved. This software is the
  3. * confidential and proprietary information of Alibaba.com ("Confidential
  4. * Information"). You shall not disclose such Confidential Information and shall
  5. * use it only in accordance with the terms of the license agreement you entered
  6. * into with Alibaba.com.
  7. */
  8. package com.alibaba.study.rpc.test;
  9. import com.alibaba.study.rpc.framework.RpcFramework;
  10. /**
  11. * RpcConsumer
  12. *
  13. * @author william.liangf
  14. */
  15. public class RpcConsumer {
  16. public static void main(String[] args) throws Exception {
  17. HelloService service = RpcFramework.refer(HelloService.class, "127.0.0.1", 1234);
  18. for (int i = 0; i < Integer.MAX_VALUE; i ++) {
  19. String hello = service.hello("World" + i);
  20. System.out.println(hello);
  21. Thread.sleep(1000);
  22. }
  23. }
  24. }

<iframe id="aswift_0" style="left: 0px; position: absolute; top: 0px;" name="aswift_0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="728" height="90"></iframe>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。