综述RPC的实现方法

阿凡达2018-07-27 09:26
RPC的实现
1.Web Service采用HTTP协议做传输层协议,采用SOAP做应用层协议,序列化方式基于文本的xml,多种语言支持。
使用spring-ws的代码如下:
public class WebServiceClient {

    private static final String MESSAGE = "Hello World";

    private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    public void setDefaultUri(String defaultUri) {
        webServiceTemplate.setDefaultUri(defaultUri);
    }

    public void simpleSendAndReceive() {
        StreamSource source = new StreamSource(new StringReader(MESSAGE));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    }

}
2.XML-RPC,采用HTTP协议做传输层协议,使用自定义XML做应用层协议,多种语言支持。
使用apache XML-RPC的代码如下:
服务端代码:
public class Server extends HttpServlet {
      private XmlRpcServletServer server;
     
      public void init(ServletConfig pConfig) throws ServletException {
            super.init(pConfig);
            try {
                  server = new XmlRpcServletServer();
                  PropertyHandlerMapping phm = new PropertyHandlerMapping();
                  phm.addHandler("HelloHandler", HelloHandlerImpl.class);                 
                  server.setHandlerMapping(phm);
                  XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl)server.getConfig();
                  serverConfig.setEnabledForExtensions(true);
                  serverConfig.setContentLengthOptional(false);
            } catch (XmlRpcException e) {
            }
      }
     
      public void doPost(HttpServletRequest pRequest, HttpServletResponse pResponse)
            throws IOException, ServletException {
            server.execute(pRequest, pResponse);
      }
}
客户端代码:
public class Client {
      public static void main(String[] args) {
            try {
                  XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
                  config.setServerURL(new URL("http://localhost:8080/jsp/XmlRpcServer"));      
                  XmlRpcClient client = new XmlRpcClient();
                  client.setConfig(config);
                  Vector params = new Vector();
                  params.addElement("Tom");
                  String result = (String) client.execute("HelloHandler.sayHello", params);                 
                  System.out.println(result);
            } catch (Exception e) {
            }
      }
}
3.RMI基于Java序列化协议,使用TCP做传输协议,使用自定义应用层协议,Java支持。
RMI框架代码如下:
服务端:
public class Server { 
  public static void main(String[] args) { 
      IService service = new ServiceImpl("service"); 
      Context namingContext = new InitialContext(); 
      namingContext.rebind("rmi://localhost/service", service); 
  } 
}
客户端:
public class Client { 
  public static void main(String[] args) { 
    String url = "rmi://localhost/"; 
    try { 
      Context namingContext = new InitialContext(); 
      IService service = (IService) namingContext.lookup(url + "service"); 
      Class stubClass = service.getClass(); 
      System.out.println(service + " 是 " + stubClass.getName() + " 的实例!"); 
      Class[] interfaces = stubClass.getInterfaces(); 
      for (Class c : interfaces) { 
        System.out.println("存根类实现了 " + c.getName() + " 接口!"); 
      } 
      System.out.println(service.service("你好!")); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
}
4.Thrift, Protobuf等都使用TCP做传输协议,使用自定义应用层协议,Thrift, Protobuf等都使用TCP做传输协议,使用自定义应用层协议,多种语言支持。
Apache Thrift的相关代码:
Hello.thrift服务声明
 service Hello{ 
  string helloString(1:string para) 
  i32 helloInt(1:i32 para) 
  bool helloBoolean(1:bool para) 
  void helloVoid() 
  string helloNull() 
 }
HelloServiceImpl服务实现
public class HelloServiceImpl implements Hello.Iface { 
    @Override 
    public boolean helloBoolean(boolean para) throws TException { 
        return para; 
    } 
    @Override 
    public int helloInt(int para) throws TException { 
        try { 
            Thread.sleep(20000); 
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        } 
        return para; 
    } 
    @Override 
    public String helloNull() throws TException { 
        return null; 
    } 
    @Override 
    public String helloString(String para) throws TException { 
        return para; 
    } 
    @Override 
    public void helloVoid() throws TException { 
        System.out.println("Hello World"); 
    } 
 }
服务端:
 public class Server { 
    public static void main(String[] args) { 
        try { 
            TServerSocket serverTransport = new TServerSocket(7911); 
            Factory proFactory = new TBinaryProtocol.Factory(); 
            TProcessor processor = new Hello.Processor(new HelloServiceImpl()); 
            TServer server = new TThreadPoolServer(processor, serverTransport, proFactory); 
            server.serve(); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } 
    } 
 }
客户端:
 public class HelloServiceClient { 
    public static void main(String[] args) { 
        try { 
            TTransport transport = new TSocket("localhost", 7911); 
            transport.open(); 
            TProtocol protocol = new TBinaryProtocol(transport); 
            Hello.Client client = new Hello.Client(protocol); 
            client.helloVoid(); 
            transport.close(); 
        } catch (TTransportException e) { 
            e.printStackTrace(); 
        } catch (TException e) { 
            e.printStackTrace(); 
        } 
    } 
 }

本文来自网易实践者社区,经作者张震权发布。