代理模式——XML-RPC學習筆記

什麼是RPC

​ RPC要像調用本地函數同樣去調用遠程函數。html

https://www.zhihu.com/question/25536695/answer/221638079java

XML-RPC圖解

http://xmlrpc.scripting.com/git

https://www.cnblogs.com/buptzym/archive/2012/11/10/2764342.htmlgithub

原理及底層實現分析

暫無。web

案例分析(附代碼)

經過xml-rpc遠程調用服務端的sayHello方法和add方法。apache

項目建立

  1. 運行環境:win七、eclipse、jdk8.0、apache-xmlrpc-3.1.三、Tomcat7.0
  2. 建立Dynamic Wed Project
  3. 將解壓後的xmlrpc文件所有jar包添加到項目路徑下,add to build path

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;
    }
}

Server

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);
	}
}

web.xml

<?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>

客戶端Client

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();
        }   
    }
}

測試

  1. 若是沒有云服務器,能夠採用局域網測試
  2. 啓動在局域網中的兩臺電腦,修改URL,分別運行服務端和客戶端程序。
  3. 在client端查看運行效果

結果

testxml-rpc

官方文檔參考: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

相關文章
相關標籤/搜索