RPC框架——简单高效hessian的使用方式

转载于:https://www.cnblogs.com/xiaoblog/p/4729309.html

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");<br>

完毕!!!

原文地址:https://www.cnblogs.com/it-deepinmind/p/11819904.html