Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。
架构
1.hessian-api
hessian-api用于存放对外暴露接口;
package com.xiaofeng.hessian.api; /** * @author zhangweixiang * @desc 一句话来描述这个类的作用 * @date $date$ $time$ */ public interface UserService { void addUser(); void updateUser(); void delUser(); String findUser(String username); }
2.hessian-server
hessian-server服务端
服务实现类:
package com.xiaofeng.hessianserver.service; import com.xiaofeng.hessian.api.UserService; /** * @author zhangweixiang * @desc 一句话来描述这个类的作用 * @date $date$ $time$ */ public class UserServiceImpl implements UserService { public void addUser() { System.out.println("-------------invoke addUser()---------------"); } public void updateUser() { System.out.println("-------------invoke updateUser()---------------"); } public void delUser() { System.out.println("-------------invoke delUser()---------------"); } public String findUser(String username) { System.out.println("-------------invoke findUser---------------"); return "return: " + username; } }
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>hessianServer</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hessianServer</servlet-name> <url-pattern>/api/service/*</url-pattern> </servlet-mapping> </web-app>
spring-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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="userService" class="com.xiaofeng.hessianserver.service.UserServiceImpl" /> <bean name="/userService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="userService" /> <property name="serviceInterface" value="com.xiaofeng.hessian.api.UserService" /> </bean> <!--<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean id="userService" class="com.xiaofeng.hessianserver.service.impl.UserServiceImpl" /> <bean name="/userService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="userService" /> <property name="serviceInterface" value="com.xiaofeng.hessianserver.service.UserService" /> </bean>--> </beans>
3.hessian-client
hessian-client客户端调用
import com.xiaofeng.hessian.api.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author zhangweixiang * @desc 一句话来描述这个类的作用 * @date $date$ $time$ */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/springmvc-servlet.xml") public class HessianTest { @Autowired private UserService userService; @Test public void test() { userService.addUser(); userService.updateUser(); userService.delUser(); String user = userService.findUser("ZhangSan"); System.out.println(user); System.out.println("--------- ------------------------finished----------------------------------"); } }
启动服务端,然后通过客户端test调用即可