专栏首页架构师进阶spring 4.0.3整合Hessian4.0.38 IDEA

spring 4.0.3整合Hessian4.0.38 IDEA

1.1 Hessian简介

Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

1.2 整合

对于Hessian是分服务端和客户端的,网上的一些例子也是大部分分开的(SpringMVC整合Hessian访问远程服务)。这里使用IntelliJ IDEA把服务端和客户端整合在一个项目里面。利用maven统一管理包。其他module直接继承依赖。

项目结构

pom.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mr.web</groupId>
    <artifactId>hessian-web</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>hessian-web</name>
    <description>公共工具包</description>

    <modules>
        <module>api</module>
        <module>service</module>
        <module>web</module>
    </modules>
    <properties>
        <spring_version>4.0.3.RELEASE</spring_version>
        <hessian.version>4.0.38</hessian.version>
    </properties>
    <dependencyManagement>

        <dependencies>
            <!-- spring start -->
            <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-web</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring_version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring_version}</version>
            </dependency>
            <!-- spring end -->
            <!-- hessian -->
            <dependency>
                <groupId>com.caucho</groupId>
                <artifactId>hessian</artifactId>
                <version>${hessian.version}</version>
            </dependency>
            <!-- test -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.8.2</version>
                <scope>test</scope>
            </dependency>

            <!-- servlet start -->
            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>tomcat-servlet-api</artifactId>
                <version>8.0.3</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.tomcat</groupId>
                <artifactId>jsp-api</artifactId>
                <version>6.0.39</version>
                <scope>provided</scope>
            </dependency>

            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>

            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
            </dependency>
            <!-- servlet end -->
        </dependencies>
    </dependencyManagement>
</project>

服务端接口目录

服务端接口ITaskRemoteService

public interface ITaskRemoteService {
    String task();
}

Pom文件

<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.mr.web</groupId>
        <artifactId>hessian-web</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>api</artifactId>
    <packaging>jar</packaging>
    <name>${project.artifactId}</name>
</project>

接口实现目录

接口实现

TaskRemoteService
@Service("taskRemoteService")
public class TaskRemoteService implements ITaskRemoteService {
    public String task() {
        System.out.println("This is a task test for hessian ");
        return "SUCCESS";
    }
}

pom文件

打包为war文件,供客户端调用

<pre style="font-family: 宋体; font-size: 9pt; background-color: rgb(255, 255, 255);"><pre name="code" class="java"><?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.mr.web</groupId>
        <artifactId>hessian-web</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>service</artifactId>
    <packaging>war</packaging>
    <name>${project.artifactId}</name>

    <dependencies>
        <dependency>
            <groupId>com.mr.web</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
    </dependencies>

</project>

配置文件context_hessian_service.xml

<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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="/taskRemoteService" class="org.springframework.remoting.caucho.HessianServiceExporter">
        <property name="service" ref="taskRemoteService"/>
        <property name="serviceInterface" value="com.mr.hessian.api.ITaskRemoteService"/>
    </bean>
    <context:component-scan base-package="com.mr"/>
</beans>

web.xml设置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>hessian-web</display-name>

    <!-- spring mvc -->
    <servlet>
        <servlet-name>hessian-service</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:context_hessian_service.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>hessian-service</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

客户端设置。

客户端目录

controller

@Controller("/")
public class TaskController {

    @Autowired
    private ITaskRemoteService taskRemoteService;

    @RequestMapping("task")
    public String index(Model model) {
        String task = "FAIL";
        task = taskRemoteService.task();
        model.addAttribute("status", task);
        return "task";
    }
}

客户端配置

context_hessian_api.xml

<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.xsd">
    <bean id="taskRemoteService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl" value="http://localhost:8080/service/taskRemoteService"/>
        <property name="serviceInterface" value="com.mr.hessian.api.ITaskRemoteService"/>
    </bean>
</beans>

spring-config.xml

<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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        ">
    <context:component-scan base-package="com.mr"/>

    <import resource="context_hessian_api.xml"/>
    <mvc:annotation-driven/>
    <!-- ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

task.jsp

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<body>
<h2>Hello World!</h2>
<a>Hessian 远程调用 </a>${status}
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>hessian-web</display-name>

    <!-- spring mvc -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

index.jsp

<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<body>
<h2>Hello World!</h2>
<a href="${pageContext.request.contextPath}/task">点我</a>
</body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <parent>
        <groupId>com.mr.web</groupId>
        <artifactId>hessian-web</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>web</artifactId>
    <packaging>war</packaging>
    <name>${project.artifactId}</name>

    <dependencies>

        <dependency>
            <groupId>com.mr.web</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>

        <!-- servlet -->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>jsp-api</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    </dependencies>
</project>

发布

添加war包

在浏览器查看

源码地址:https://github.com/wuxinshui/hessian-web

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • spring data jpa 入门

    项目中有用到spring data jpa,简单看了一下,在基础的数据库操作上,要比mybatis好用,简化了持久层的操作,使coder可以专注于业务逻辑的实现...

    用户2146693
  • Spring Boot 定时任务之Quartz

    当定时任务愈加复杂时,使用Spring注解@Schedule 已经不能满足业务需要。 @Schedule 实现的定时任务:

    用户2146693
  • 基础服务系列-Docker 安装MongoDB

    docker run --name test-mongo -p 27017:27017 -d mongo:latest

    用户2146693
  • maven教程5(聚合工程)

      所谓聚合项目,实际上就是对项目分模块,互联网项目一般来说按照业务分(订单模块、VIP模块、支付模块、CMS模块…),传统的软件项目,大多采用分层的方式(Da...

    用户4919348
  • 利用springboot 将数据库中的数据导出为excle。还实现将excle里面的数据上传到数据库里面

    写一个接口,浏览器一输入这个接口,那么就可以导出数据库里面的数据到excle表里面了。要实现这个功能。我们使用springboot

    一天不写程序难受
  • SOAPUI访问WEBSERVICE案例

    小编最近被要求测试一下webservice接口,于是小编jmeter和postman轮番编码+运行循环了N次,各种网上搜解决方案,倒腾半天无果,于是小编果断放弃...

    muntainyang
  • SSM整合:商城小项目001(配置SSM环境+查询所有操作+添加操作)

    (1)dao、service都由Spring容器构架(applicationContext.xml) (2)controller由SpringMVC容器构建(...

  • 探花交友_搭建开发环境

    此命令为部署 Redis 集群,如果本地的 docker 没有 redis 的镜像,那么他会先自动下载再部署。

    wsuo
  • SSM 单体框架 - 教育平台后台管理系统:Maven 进阶使用

    教育后台管理系统是提供给相关业务人员使用的一个后台管理系统,业务人员可以在这个后台管理系统中,对课程信息、广告信息、用户信息、 权限信息等数据进行维护。

    RendaZhang
  • 振兴杯试题分析:制作简单的登陆页(一)

    之前有做过相关准备文章,最近忙下来决定了利用框架完成该题 业精于勤,荒于嬉,行成于思,毁于随我对这句话坚信不疑。 本文章内容较多所以我就少些废话了。 有兴趣的朋...

    用户1788308

扫码关注云+社区

领取腾讯云代金券