RPC hessian简单案例

RPC(Remote procedure call) 远程服务调用。

dubbox就是RPC框架,hessian是简单的RPC实现。

首先需要有接口及其实现类:

接口。

public interface HessianService {

	
	String getHessianServiceMethod();
	
}

  实现类

public class HessianServiceImpl implements HessianService{
	@Override
	public String getHessianServiceMethod() {
		
		System.out.println("On hessian service implement class!");
		return "hessian service";
	}
}

  servlet映射:

在web.xml里面加入

<servlet> 
           <servlet-name>remoting</servlet-name>
  			  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
	          <!-- 默认是加载WEB-INF/servlet-name+"-servlet.xml"文件,在这里可以配置为加载WEB-INF/classes/remoting-servlet.xml这个文件  -->
	          <init-param>
              <param-name>namespace</param-name>
              <param-value>classes/remoting-servlet</param-value>
          </init-param>
           <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
           <servlet-name>remoting</servlet-name> 
           <url-pattern>/remoting/*</url-pattern> 
	</servlet-mapping> 

  再到remoting-servlet.xml中加入实现类映射:

<!-- hessian RPC -->
	<bean name="HessianService" class="com.*.hessian.impl.HessianServiceImpl" /> 
	<bean name="/hessian/HessianService"
		class="org.springframework.remoting.caucho.HessianServiceExporter">
		<!-- service引用的具体实现类 -->
		<property name="service" ref="HessianService"></property>
		<property name="serviceInterface" value="com.*.hessian.HessianService"></property>
	</bean>

  然后简单的hessianRPC就可以实现了。

测试:

          String url="http://127.0.0.1:8080/spring/remoting/hessian/HessianService";//spring是项目名
		HessianProxyFactory hf= new HessianProxyFactory();
		HessianService hessianService = (HessianService)hf.create(HessianService.class, url);
		String hessianServiceMethod = hessianService.getHessianServiceMethod();
		System.out.println("--------"+hessianServiceMethod);

  

posted @ 2016-12-26 15:01  吐槽村  阅读(322)  评论(0编辑  收藏  举报