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");
}
}
<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>
<?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>
<?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注入接口调用服务即可