一、什么是RPC

  RPC全称Remote Procedure Call,中文名叫远程过程调用。RPC是一种远程调用技术,用于不同系统之间的远程相互调用。其在分布式系统中应用十分广泛。

二、什么是Hessian

  Hessian是一个轻量级的RPC框架。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

三、Hessian的使用

  1、引入jar包

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

  

  2.编写业务代码(和普通的业务代码一样)

public interface UserService {

    String getUserInfoById (Integer id);
} @Component("uservice")
public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; public String getUserInfoById(Integer id) {
User user = userMapper.selectByPrimaryKey(id);
return user.toString();
}
}

  3.创建并加载hessian-servlet.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 使用Spring的HessianServie做代理 -->
<bean name="/userServiceImpl" class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- service引用具体的实现实体Bean-->
<property name="service" ref="uservice" />
<property name="serviceInterface" value="com.myproject.hessian.UserService" />
</bean> </beans>
public class HessianServiceProxyExporter extends HessianServiceExporter {
private static final Base64 base64 = new Base64(); @Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String authorization = request.getHeader("Authorization");
if(authorization != null && authorization.length() > 0){
String userAndPwd = new String(base64.decode(authorization.split(" ")[1]));
String user = userAndPwd.split(":")[0];
String password = userAndPwd.split(":")[1];
if("user".equalsIgnoreCase(user) && "123456".equalsIgnoreCase(password)) {
super.handleRequest(request, response);
} else {
System.out.println("您无权调用");
}
}
}
}
<beans>
<!-- 自己定义代理类来继承org.springframework.remoting.caucho.HessianServiceExporter类,然后进行权限等一系列操作-->
<bean name="/userServiceImpl" class="com.myproject.hessian.exporter.HessianServiceProxyExporter">
<!-- service引用具体的实现实体Bean-->
<property name="service" ref="uservice" />
<property name="serviceInterface" value="com.myproject.hessian.UserService" />
</bean> </beans>
<!-- web.xml中进行拦截,并加载配置文件hessian -->
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>

  5.客户端编写

    ①普通方式调用。同样需要引入hessian的jar包

//先将服务端的服务接口搬过来,包名和类名方法名最好是要一模一样
public interface UserService { String getUserInfoById (Integer id);
} public class Test { public static void main(String[] args) throws MalformedURLException {
String url = "http://localhost:8080/zmyproject/hessian/userServiceImpl";
HessianProxyFactory factory = new HessianProxyFactory();
     factory.setUser("user");
     factory.setPassword("123456");
     UserService userService = (UserService)factory.create(UserService.class, url);
        System.out.println(userService.getUserInfoById(2));
}
}

    ②spring框架调用

      Ⅰ、先引入jar包,注意jar包的版本我使用的hession-3.1.5.jar,启动会找不到一个factory类,所以用了4.0.38版本的。

       Ⅱ、配置hession-client.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"> <!--客户端Hessian代理工厂Bean-->
<bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!--这是因为接口中出现方法重载,在调用时,服务器端会跑出异常 。在整合spring中,在客户端的配置里面加上如下代码可以解决:-->
<property name="overloadEnabled" value="true" />
<!--请求代理Servlet路径:-->
<property name="serviceUrl" value="http://localhost:8080/zmyproject/hessian/userServiceImpl" />
<!--接口定义:-->
<property name="serviceInterface" value="com.myproject.hessian.UserService" />
<property name="username" value="user" />
<property name="password" value="123456" />
</bean> </beans>
<!-- web.xml中配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hession-servlet.xml</param-value>
</context-param>
//和hessian服务端一样的接口
public interface UserService { String getUserInfoById (Integer id);
} @Controller//将该类标注为处理器,并且加入spring容器中
@RequestMapping("/hessian")
public class HessianController{ @Autowired
private UserService userService; @RequestMapping(value="/getInfo",method=RequestMethod.GET)
@ResponseBody
public String getInfo(HttpServletRequest request,HttpServletResponse response){
String userInfo = userService.getUserInfoById(1);
return userInfo;
} }

  

spring集成Hessian的基本使用方法的更多相关文章

  1. Spring集成Hessian

    一.概述 Spring 通过org.springframework.remoting.caucho.HessianServiceExporter将POJO中的所有public方法发布为Hessian服 ...

  2. spring集成JPA的三种方法配置

    JPA是Java EE5规范之一,是一个orm规范,由厂商来实现该规范.目前有hibernate,OpenJPA,TopLink和EclipseJPA等实现 spring提供三种方法集成JPA:1.L ...

  3. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  4. 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)

    硬盘和内存的作用是什么 硬盘的作用毫无疑问我们大家都清楚,不就是用来存储数据文件的么?如照片.视频.各种文档或等等,肯定也有你喜欢的某位岛国老师的动作片,这个时候无论我们电脑是否关机重启它们永远在那里 ...

  5. axis2+spring集成

    转载自:http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html 1.新建一个web project项目,最终工程目录如下: 注意 ...

  6. rabbitMQ第五篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  7. spring集成常用技术的配置

    使用spring集成其他技术,最基本的配置都是模板化的,比如配置视图模板引擎.数据库连接池.orm框架.缓存服务.邮件服务.rpc调用等,以spring的xml配置为例,我将这些配置过程整理出来,并不 ...

  8. Activiti工作流学习(三)Activiti工作流与spring集成

    一.前言 前面Activiti工作流的学习,说明了Activiti的基本应用,在我们开发中可以根据实际的业务参考Activiti的API去更好的理解以及巩固.我们实际的开发中我们基本上都使用sprin ...

  9. 重构Mybatis与Spring集成的SqlSessionFactoryBean(1)

    一般来说,修改框架的源代码是极其有风险的,除非万不得已,否则不要去修改.但是今天却小心翼翼的重构了Mybatis官方提供的与Spring集成的SqlSessionFactoryBean类,一来是抱着试 ...

随机推荐

  1. pysphere VMware控制模块的一些函数的说明

    对于虚拟机的操作获得虚拟机对象 当你正常连接了服务器后,你就可以使用以下两种方式来得到虚拟机对象. get_vm_by_path get_vm_by_name 虚拟机路径可以从虚拟机右键信息中的”Ed ...

  2. POJ1151-扫面线+线段树+离散化//入门题

    比较水的入门题 记录矩形竖边的x坐标,离散化排序.以被标记的边建树. 扫描线段树,查询线段树内被标记的边.遇到矩形的右边就删除此边 每一段的面积是查询结果乘边的横坐标之差,求和就是答案 #includ ...

  3. BZOJ3230 相似子串 【后缀数组】

    题目分析: 容易想到sa排好序之后,子串排名就是前面的子串减去height数组.所以正着做一遍,倒着做一遍就行了. 代码: #include<bits/stdc++.h> using na ...

  4. CF1106F Lunar New Year and a Recursive Sequence

    题目链接:CF1106F Lunar New Year and a Recursive Sequence 大意:已知\(f_1,f_2,\cdots,f_{k-1}\)和\(b_1,b_2,\cdot ...

  5. python的生成器与迭代器和可迭代对象

    来简单的说下python中的生成器和可迭代对象以及迭代器的问题.只是简单地记录一下并不涉及太深入的内容. 首先来说一下什么是生成器,先看下面的代码: #_*_ coding:utf-8 _*_ res ...

  6. Codeforces Round #429 (Div. 1) C. On the Bench(dp + 组合数)

    题意 一个长度为 \(n\) 的序列 \(A\) ,定义一个 \(1\) 到 \(n\) 的排列 \(p\) 是合法的,当且仅当 \(\forall i \in [1, n − 1], A_{p_i} ...

  7. 【BZOJ2242】计算器(BSGS,快速幂)

    [BZOJ2242]计算器(BSGS,快速幂) 题面 BZOJ 洛谷 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给 ...

  8. html标题、段落、换行与字符实体

    通过 <h1>.<h2>.<h3>.<h4>.<h5>.<h6>,标签可以在网页上定义6种级别的标题.6种级别的标题表示文档的6 ...

  9. day20

    20.01 IO流(IO流概述及分类) 1.IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 字节流:字节流可以操作任何数据,计算机中任何数据都 ...

  10. [SDOI2015]序列统计(多项式快速幂)

    题目描述 小C有一个集合S,里面的元素都是小于M的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为N的数列,数列中的每个数都属于集合S.小C用这个生成器生成了许多这样的数列.但是小C有一个问 ...