Java 远程调用之Hessian简例

1,导入jar包

<dependency>
	<groupId>org.resthub</groupId>
	<artifactId>hessian</artifactId>
	<version>4.0.8</version>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.3.3</version>
</dependency>

2,提供接口

/**
 * 服务接口
 */
public interface ISayHelloService {

	/**
	 * @param name
	 * @return
	 */
	String doSayHello(String name);
}

3,提供实现类

/**
 * 服务接口实现
 */
@Service
public class SayHelloServiceImpl implements ISayHelloService {
	
	public String doSayHello(String name) {
		return doSayHello(name, "hello");
	}
}


4,在server项目中配置web.xml
  <servlet>
    <servlet-name>hessian</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/hessian-servlet.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>hessian</servlet-name>
    <url-pattern>/hessian/*</url-pattern>
  </servlet-mapping>


5,新建hessian-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
	default-lazy-init="false">
	<bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

	<context:component-scan base-package="com.rpc.service.impl"/>

	<!-- 测试 -->
	<bean id="sayHelloService" class="com.rpc.service.impl.SayHelloServiceImpl" /> 

	<bean id="/hello" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<property name="service" ref="sayHelloService" />
		<!-- 服务接口 -->
		<property name="serviceInterface" value="com.hatchet.rpc.service.ISayHelloService" />
	</bean>
	
</beans>


6,在client 项目中引用remote.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
           <context:property-placeholder ignore-resource-not-found="true"
			location="classpath*:/application.properties" />
     <bean id="hello" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">    
        <property name="serviceUrl" value="http://${rpc.server.ip}:${rpc.server.port}/crowdfunding-rpc-server/hessian/hello" />    
        <property name="serviceInterface" value="com.hatchet.rpc.service.ISayHelloService" />   
         <property name="overloadEnabled" value="true" />  
         <property name="readTimeout" value="${rpc.readTimeout}" />
    </bean>
</beans>
application.properties文件

rpc.server.ip=127.0.0.1
rpc.server.port=8080
rpc.readTimeout=60000


大功告成,通过spring注入接口调用服务即可


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值