java spring远程调用Hessian 实例
原创    xingfu2017    发表于:2017-04-18 16:16:01
  阅读 :217   收藏   编辑

Hessian介绍

  • Hessian是一个轻量级的,自定义描述的二进制RPC协议。使用简单的方法提供了RMI的功能, 相比WebService,Hessian更简单、快捷,Hessian主要用作面向对象的消息通信。
  • hessian官网 :http://hessian.caucho.com
  • 序列化规则:http://hessian.caucho.com/doc/hessian-serialization.html
  • maven仓库地址:
    <dependency>
          <groupid>com.caucho</groupid>
          <artifactid>hessian</artifactid>
          <version>4.0.38</version>
    </dependency>
    
spring环境下hessian的使用

本次实例中我们会创建两个java web项目,其中一个为client端,一个为server端

server端


新建Java web Maven项目“hessian_server”

项目结构如下:

pom xml依赖,及tomcat启动,端口配置
<dependencies>
        <dependency>
            <groupid>junit</groupid>
            <artifactid>junit</artifactid>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-core</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-aop</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-beans</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-context-support</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-expression</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-web</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.springframework</groupid>
            <artifactid>spring-webmvc</artifactid>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupid>org.slf4j</groupid>
            <artifactid>slf4j-log4j12</artifactid>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupid>log4j</groupid>
            <artifactid>log4j</artifactid>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupid>com.caucho</groupid>
            <artifactid>hessian</artifactid>
            <version>4.0.38</version>
        </dependency>

    </dependencies>
    <build>
        <finalname>hessian_server</finalname>
        <plugins>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-war-plugin</artifactid>
                <version>2.1.1</version>
                <configuration>
                    <warname>hessian_server</warname>
                </configuration>
            </plugin>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-surefire-plugin</artifactid>
                <version>2.19.1</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupid>org.apache.tomcat.maven</groupid>
                <artifactid>tomcat7-maven-plugin</artifactid>
                <version>2.2</version>
                <configuration>
                     <!-- 端口号 -->
                    <port>9090</port>
                    <path>/hessian_server</path>
                    <contextfile>\${tomcatContextXml}</contextfile>
                    <protocol>org.apache.coyote.http11.Http11NioProtocol</protocol>
                    <uriencoding>utf-8</uriencoding>
                </configuration>
            </plugin>
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-archetype-plugin</artifactid>
                <version>2.2</version>
            </plugin>
        </plugins>
    </build>
web.xml 文件配置及说明
<context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- spring配置文件 -->
        <param-value>classpath:app_context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- hessian -->
    <servlet>
        <servlet-name>hessian</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 使用Spring的代理Servlet 这里拦截所有的/remote/*下的所有请求
                   比如server暴露Userservice服务,则客户端请求地址为:
                 http://ip:port/project_name/remote/userService
             -->
            <param-value>classpath:app_hessian.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/remote/*</url-pattern>
    </servlet-mapping>
app_context.xml 配置
<!-- 注解 -->
<context:annotation-config />
<!-- 定义包扫描 -->
<context:component-scan base-package="com.xx" />
app_hessian.xml配置
<!-- hessian 暴露 userService -->
    <bean name="/userService" class="org.springframework.remoting.caucho.HessianServiceExporter"> 
            <!-- service引用具体的实现实体Bean,这里使用的是注解,在UserServiceImpl的类有注解@Service("userService")-->  
            <property name="service" ref="userService"></property> 
            <!-- 暴露的接口地址 -->
            <property name="serviceInterface" value="com.xx.service.IUserService"></property> 
    </bean>
实体类User
public class User implements Serializable {

    private String uid;
    private String uName;
    private String email;
    //get set 省略
}
IUserService接口
public interface IUserService {

    public List<user> getUsers();
}
UserServiceImpl接口实现类
/**
 *    定义bean的名字
 */
@Service("userService")
public class UserServiceImpl implements IUserService {

    //这里是模拟的数据
    public List<user> getUsers() {
        List<user> list = new ArrayList<user>();
        list.add(new User("1","李四","lisi@xx.com"));
        list.add(new User("2","王五","wangwu@xx.com"));
        list.add(new User("3","马六","maliu@xx.com"));
        System.out.println("hessian_server端被调用了...");
        return list;
    }

}
最后右键项目hessian_server,Run As -> Maven Build -> tomcat7:run 启动项目

client端相关


新建Java web Maven项目“hessian_client”

项目结构如下:

为了简单测试,我们client不在配置web.xml,使用Java类来测试,与server端不同的配置文件有:
app_context.xml,app_hessian.xml,

app_context.xml配置文件
<context:annotation-config />
<context:component-scan base-package="com.xx" />
<!-- 将hessian配置bean文件导入 -->
<import resource="app_hessian.xml" />
app_hessian.xml配置文件

    <bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <!-- 服务端地址,端口,项目名称,接口 -->
        <property name="serviceUrl" value="http://127.0.0.1:9090/hessian_server/remote/userService"></property>
        <property name="serviceInterface" value="com.xx.service.IUserService"></property>
        <property name="overloadEnabled" value="true"></property>
    </bean>

运行HessianTest类测试hessian


public static void main(String[] args) {

        //加载app_context.xml创建spring context
        ApplicationContext context = new ClassPathXmlApplicationContext("app_context.xml");

        IUserService userService = (IUserService) context.getBean("userService");
        System.out.println("users: " + userService.getUsers());

    }
评论
条评论

推荐阅读