hessian入门,spring集成

来源:互联网 发布:普鲁申科 亚古丁 知乎 编辑:程序博客网 时间:2024/06/05 05:20
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能.相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。协议越底层传输速度越快.

1.导入依赖(这里导得比较多,cxf的可以干掉)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.ssm-itf.demo</groupId>    <artifactId>ssm-itf</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>war</packaging>    <properties>          <!-- spring版本号 -->          <spring.version>4.0.2.RELEASE</spring.version>        <!-- cxf -->        <cxf.version>3.1.10</cxf.version>        <!-- hessian -->        <hessian.version>4.0.7</hessian.version>    </properties>    <dependencies>        <!-- junit -->        <dependency>              <groupId>junit</groupId>              <artifactId>junit</artifactId>              <version>4.11</version>              <scope>test</scope>          </dependency>        <!-- spring核心包 -->          <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-core</artifactId>              <version>${spring.version}</version>          </dependency>        <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-beans</artifactId>              <version>${spring.version}</version>          </dependency>        <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-tx</artifactId>              <version>${spring.version}</version>              <exclusions>                  <exclusion>                      <groupId>org.springframework</groupId>                      <artifactId>spring-beans</artifactId>                  </exclusion>              </exclusions>        </dependency>        <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-jdbc</artifactId>              <version>${spring.version}</version>            <exclusions>                  <exclusion>                      <groupId>org.springframework</groupId>                      <artifactId>spring-beans</artifactId>                  </exclusion>              </exclusions>          </dependency>        <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-aop</artifactId>              <version>${spring.version}</version>              <exclusions>                  <exclusion>                      <groupId>org.springframework</groupId>                      <artifactId>spring-beans</artifactId>                  </exclusion>              </exclusions>         </dependency>        <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-webmvc</artifactId>              <version>${spring.version}</version>              <!-- 排除依赖 -->            <exclusions>                  <exclusion>                      <groupId>org.springframework</groupId>                      <artifactId>spring-beans</artifactId>                  </exclusion>              </exclusions>         </dependency>        <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-context-support</artifactId>              <version>${spring.version}</version>              <exclusions>                  <exclusion>                      <groupId>org.springframework</groupId>                      <artifactId>spring-beans</artifactId>                  </exclusion>              </exclusions>        </dependency>        <dependency>              <groupId>org.springframework</groupId>              <artifactId>spring-test</artifactId>              <version>${spring.version}</version>          </dependency>        <!-- cxf -->        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxws</artifactId>            <version>${cxf.version}</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-transports-http</artifactId>            <version>${cxf.version}</version>        </dependency>        <dependency>            <groupId>org.apache.cxf</groupId>            <artifactId>cxf-rt-frontend-jaxrs</artifactId>            <version>${cxf.version}</version>        </dependency>        <!-- hessian -->        <dependency>            <groupId>com.caucho</groupId>            <artifactId>hessian</artifactId>            <version>${hessian.version}</version>        </dependency>    </dependencies>    <build>        <finalName>ssm-itf</finalName>        <plugins>            <!-- 资源拷贝插件 -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-resources-plugin</artifactId>                <version>2.7</version>                <configuration>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>            <!-- java编译插件 -->            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.2</version>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>        </plugins>    </build></project>

2.核心配置文件
remote-servlet.xml
为什么叫这个名字,是和web.xml配置的servletName有关系的.

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  <beans>      <!-- 接口的具体实现类 -->      <bean id="helloServiceimpl" class="com.ssm.itf.service.impl.HelloServiceImpl" />      <!-- 使用Spring的HessianServie做代理 -->      <bean name="/hello"  class="org.springframework.remoting.caucho.HessianServiceExporter">        <!-- service引用具体的实现实体Bean-->          <property name="service" ref="helloServiceimpl" />          <property name="serviceInterface" value="com.ssm.itf.service.HelloService" />      </bean>      <!-- 可以配置多个HessianServiceExporter代理Bean -->  </beans> 

web.xml

<?xml version="1.0" encoding="UTF-8"?>  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns="http://java.sun.com/xml/ns/javaee"      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"      version="3.0">      <display-name>ssm-itf</display-name>      <servlet>          <servlet-name>remote</servlet-name>          <!-- 使用Spring的代理Servlet -->          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>          <init-param>              <param-name>namespace</param-name>              <param-value>classes/remote-servlet</param-value><!-- 编译之后的位置 -->            <!-- servlet的名字为remote -->            <!-- 文件名的格式必须是[servlet-name]-servlet.xml格式,否则检测不到。 且放在src下-->        </init-param>          <load-on-startup>1</load-on-startup>      </servlet>      <servlet-mapping>          <servlet-name>remote</servlet-name>          <url-pattern>/remote/*</url-pattern>      </servlet-mapping></web-app>

3.启动并测试接口服务是否正常

package com.ssm.itf.service;import java.net.MalformedURLException;import com.caucho.hessian.client.HessianProxyFactory;/** * @autho 董杨炀 * @time 2017年5月17日 上午9:37:35*/public class MainAppTest {    public static void main(String[] args) throws MalformedURLException {          //Spring Hessian代理Servelet          String url = "http://localhost:8080/ssm-itf/remote/hello";          HessianProxyFactory factory = new HessianProxyFactory();          HelloService api = (HelloService) factory.create(HelloService.class, url);          System.out.println(api.sayHello("World"));      } }

这里写图片描述

4.模拟客户端测试,假如客户端是个单独的应用.如果是maven聚合工程,此时就需要依赖服务接口的jar(只需要依赖接口就好),增加一个客户端配置文件,通过加载spring容器测试.这里不详细描述,如下图:
这里写图片描述

remote-client.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">  <beans>      <!-- 客户端Hessian代理工厂Bean -->      <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">          <!-- 请求代理Servlet路径 -->                  <property name="serviceUrl">          <value>http://localhost:8080/ssm-itf/remote/hello</value>          </property>          <!-- 接口定义 -->          <property name="serviceInterface">              <value>com.ssm.itf.service.HelloService</value>          </property>      </bean>  </beans>