Java 远程调用之Hessian简例
2016-05-25 15:46
495 查看
1,导入jar包
2,提供接口
3,提供实现类
4,在server项目中配置web.xml
5,新建hessian-servlet.xml
6,在client 项目中引用remote.xml
大功告成,通过spring注入接口调用服务即可
<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注入接口调用服务即可
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树