RPC要像調用本地函數同樣去調用遠程函數。html
https://www.zhihu.com/question/25536695/answer/221638079java
https://www.cnblogs.com/buptzym/archive/2012/11/10/2764342.htmlgithub
暫無。web
經過xml-rpc遠程調用服務端的sayHello方法和add方法。apache
apache-xmlrpc-3.1.3官網下載:http://archive.apache.org/dist/ws/xmlrpc/設計模式
package com.dong.xmlrpc; /** * * @ClassName: ServicesHandler * @Description: 業務處理接口 * @author: dong * @date: 2018年11月11日 下午3:25:23 * @keyword: * */ public interface ServicesHandler { public String sayHello(String str); public int add(int num1, int num2); }
package com.dong.xmlrpc; /** * * @ClassName: HelloHandler * @Description: 業務接口實現 * @author: dong * @date: 2018年11月11日 下午3:27:10 * @keyword: * */ public class HelloHandler implements ServicesHandler { public String sayHello(String str) { // TODO Auto-generated method stub return "hello " + str + "!"; } public int add(int num1, int num2) { // TODO Auto-generated method stub return num1 + num2; } }
package com.dong.xmlrpc; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.server.PropertyHandlerMapping; import org.apache.xmlrpc.server.XmlRpcServerConfigImpl; import org.apache.xmlrpc.webserver.XmlRpcServletServer; /** * * @ClassName: XmlRpcServicesServlet * @Description: 服務端 * @author: dong * @date: 2018年11月11日 下午3:28:09 * @keyword: * */ public class XmlRpcServicesServlet extends HttpServlet { private XmlRpcServletServer server; @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); try { // 建立XmlRpcServicesServlet對象 server = new XmlRpcServletServer(); //set up handler mapping of XmlRpcServletServer object PropertyHandlerMapping pmp = new PropertyHandlerMapping(); pmp.addHandler("HelloHandler", HelloHandler.class); server.setHandlerMapping(pmp); //more config of XmlRpcServletServer object XmlRpcServerConfigImpl serverConfig = (XmlRpcServerConfigImpl) server.getConfig(); serverConfig.setEnabledForExceptions(true); serverConfig.setContentLengthOptional(false); } catch (XmlRpcException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub server.execute(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub server.execute(request, response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Rpc</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>XmlRpcServicesServlet</display-name> <servlet-name>XmlRpcServicesServlet</servlet-name> <servlet-class>com.dong.xmlrpc.XmlRpcServicesServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>XmlRpcServicesServlet</servlet-name> <url-pattern>/HelloHandler</url-pattern> </servlet-mapping> </web-app>
package com.dong.xmlrpc; import java.net.MalformedURLException; import java.net.URL; import java.util.Vector; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; /** * * @ClassName: TestClient * @Description: 客戶端 * @author dong * @date Nov 6, 2018 10:02:43 PM * @keywords : * */ public class TestClient { public static void main(String[] args) { try { // 配置客戶端 XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); // 設置服務端地址 config.setServerURL(new URL("http://127.0.0.1:8080/Rpc/HelloHandler")); // 建立XmlRpc客戶端 XmlRpcClient client = new XmlRpcClient(); // 綁定以上設置 client.setConfig(config); // 建立參數列表 Vector<String> params1 = new Vector<String>(); params1.addElement("dong"); Object[] params2 = new Object[2]; params2[0] = 23; params2[1] = 20; // 執行xml-rpc請求 String result1 = (String) client.execute("HelloHandler.sayHello", params1); System.out.println("reslut:" + result1); int result2 = (Integer) client.execute("HelloHandler.add", params2); System.out.println("result:" + result2); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XmlRpcException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
官方文檔參考:http://ws.apache.org/xmlrpc/client.html服務器
當前主流的RPC框架有Google開源的gRpc,阿里巴巴開源的Dubbo,以及nettyRpc等。這是一片廣闊而深遠的天地,在學習設計模式的代理模式一節中,有幸涉獵,看了不少論文和大牛的博客文章,依舊沒能登堂入室。知識須要深刻實踐,no code,no text。app
https://github.com/grpc/grpc框架
https://github.com/apache/incubator-dubbo
案例源碼:https://github.com/comeCU/Design-patterns/blob/master/starUML_files/others/Rpc.zip