RPC框架——簡單高效hessian的使用方式


 

RPC(Remote Procedure Call Protocol)


 

RPC使用C/S方式,采用http協議,發送請求到服務器,等待服務器返回結果。這個請求包括一個參數集和一個文本集,通常形成“classname.methodname”形式。優點是跨語言跨平台,C端、S端有更大的獨立性,缺點是不支持對象,無法在編譯器檢查錯誤,只能在運行期檢查。

RPC是指遠程過程調用,也就是說兩台服務器A,B,一個應用部署在A服務器上,想要調用B服務器上應用提供的函數/方法,由於不在一個內存空間,不能直接調用,需要通過網絡來表達調用的語義和傳達調用的數據。

 

關於JAVA中hessian的使用:


 

服務端配置(接收數據方):

1.    講述如何配置Hessian的服務器端(與Spring集成)

2.定義接口類

package cn.demo.hessian;
/**
 * 服務端接口
 * @author xiao
 *
 */
public interface ISayHello {

	public String sayHello(String arg1,String arg2);
}

  

3.接口實現類

package cn.demo.hessian.impl;

import cn.demo.hessian.ISayHello;
/**
 * 服務端接口實現
 * @author xiao
 *
 */
public class SayHelloImpl implements ISayHello {

	public String sayHello(String arg1, String arg2) {
		// TODO Auto-generated method stub
		return "arg1="+arg1+",arg2="+arg2;
	}

}

  

4.配置Web.xml

<servlet>  
    <servlet-name>remote</servlet-name>       <!-- 使用Spring的代理Servlet -->       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>       <init-param>           <param-name>namespace</param-name>           <param-value>classes/remote-servlet</param-value>      </init-param>     <load-on-startup>1</load-on-startup>   </servlet>      <servlet-mapping>      <servlet-name>remote</servlet-name>      <url-pattern>/remote/*</url-pattern>   </servlet-mapping>  

 

5.配置remote-servlet.xml[該文件位於src目錄下,即編譯后存在與classes下]:

 

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  
<beans>       <!-- 接口的具體實現類 -->       <bean id="helloImpl" class="cn.demo.hessian.impl.SayHelloImpl" />      <!-- 使用Spring的HessianServie做代理 -->       <bean name="/helloHessian"      class="org.springframework.remoting.caucho.HessianServiceExporter">          <!-- service引用具體的實現實體Bean-->           <property name="service" ref="helloImpl" />           <property name="serviceInterface" value="cn.demo.hessian.ISayHello" />       </bean>              <!-- 可以配置多個HessianServiceExporter代理Bean -->   </beans>  

 

 

服務器端配置完畢。

客戶端配置(也就是所謂發送數據的一方):

 

1.服務端接口,這里指服務端調用客戶端的接口方法類,和客戶端一致:

package cn.demo.hessian.server;
/**
 * 客戶端接口
 * @author xiao
 *
 */
public interface ISayHello {

	public String sayHello(String arg1,String arg2);
}

2.beans.xml配置

<bean id="helloClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl">
<!--服務端地址-->
            <value>http://127.0.0.1:8088/xs/remote/helloHessian</value>
        </property>
<!--客戶端接口-->
        <property name="serviceInterface" value="cn.demo.hessian.service.ISayHello"/>
</bean>

3.調用

BeanFactory ac = new ClassPathXmlApplicationContext("classpath:/beans.xml");  
ISayHello helloClient=(ISayHello)ac.getBean("helloClient");
helloClient.sayHello("hello","hessian");

 

完畢!!!


 

ps:如果需要傳輸對象,可以轉成json串,然后服務端通過json轉成對象這種方式靈活使用。  

  

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM