spring集成Hessian的基本使用方法

一、什么是RPC

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

二、什么是Hessian

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

三、Hessian的使用

  1、引入jar包

1<dependency> 2 <groupId>com.caucho</groupId> 3 <artifactId>hessian</artifactId> 4 <version>4.0.38</version> 5 </dependency>

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

1public interface UserService { 2 3 String getUserInfoById (Integer id); 4} 5 6 7@Component("uservice") 8public class UserServiceImpl implements UserService { 9 10 @Autowired 11 private UserMapper userMapper; 12 13 public String getUserInfoById(Integer id) { 14 User user = userMapper.selectByPrimaryKey(id); 15 return user.toString(); 16 } 17}

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

1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 3<beans> 4 <!-- 使用Spring的HessianServie做代理 --> 5 <bean name="/userServiceImpl" class="org.springframework.remoting.caucho.HessianServiceExporter"> 6 <!-- service引用具体的实现实体Bean--> 7 <property name="service" ref="uservice" /> 8 <property name="serviceInterface" value="com.myproject.hessian.UserService" /> 9 </bean> 10 11</beans> 12 13public class HessianServiceProxyExporter extends HessianServiceExporter { 14 private static final Base64 base64 = new Base64(); 15 16 @Override 17 public void handleRequest(HttpServletRequest request, HttpServletResponse response) 18 throws ServletException, IOException { 19 20 String authorization = request.getHeader("Authorization"); 21 if(authorization != null && authorization.length() > 0){ 22 String userAndPwd = new String(base64.decode(authorization.split(" ")[1])); 23 String user = userAndPwd.split(":")[0]; 24 String password = userAndPwd.split(":")[1]; 25 if("user".equalsIgnoreCase(user) && "123456".equalsIgnoreCase(password)) { 26 super.handleRequest(request, response); 27 } else { 28 System.out.println("您无权调用"); 29 } 30 } 31 } 32} 33 34<beans> 35 <!-- 自己定义代理类来继承org.springframework.remoting.caucho.HessianServiceExporter类,然后进行权限等一系列操作--> 36 <bean name="/userServiceImpl" class="com.myproject.hessian.exporter.HessianServiceProxyExporter"> 37 <!-- service引用具体的实现实体Bean--> 38 <property name="service" ref="uservice" /> 39 <property name="serviceInterface" value="com.myproject.hessian.UserService" /> 40 </bean> 41 42</beans> 43 44<!-- web.xml中进行拦截,并加载配置文件hessian --> 45 <servlet> 46 <servlet-name>hessian</servlet-name> 47 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 48 <init-param> 49 <param-name>contextConfigLocation</param-name> 50 <param-value>classpath:hessian-servlet.xml</param-value> 51 </init-param> 52 <load-on-startup>1</load-on-startup> 53 </servlet> 54 <servlet-mapping> 55 <servlet-name>hessian</servlet-name> 56 <url-pattern>/hessian/*</url-pattern> 57 </servlet-mapping>

  5.客户端编写

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

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

    ②spring框架调用

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

       Ⅱ、配置hession-client.xml,并加载该文件

1<?xml version="1.0" encoding="UTF-8"?> 2<beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:task="http://www.springframework.org/schema/task" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context-4.0.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 13 http://www.springframework.org/schema/aop 14 http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 15 http://www.springframework.org/schema/task 16 http://www.springframework.org/schema/task/spring-task-3.1.xsd"> 17 18 <!--客户端Hessian代理工厂Bean--> 19 <bean id="userService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> 20 <!--这是因为接口中出现方法重载,在调用时,服务器端会跑出异常 。在整合spring中,在客户端的配置里面加上如下代码可以解决:--> 21 <property name="overloadEnabled" value="true" /> 22 <!--请求代理Servlet路径:--> 23 <property name="serviceUrl" value="http://localhost:8080/zmyproject/hessian/userServiceImpl" /> 24 <!--接口定义:--> 25 <property name="serviceInterface" value="com.myproject.hessian.UserService" /> 26 <property name="username" value="user" /> 27 <property name="password" value="123456" /> 28 </bean> 29 30 31</beans> 32 33<!-- web.xml中配置 --> 34 <listener> 35 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 36 </listener> 37 <context-param> 38 <param-name>contextConfigLocation</param-name> 39 <param-value>classpath:hession-servlet.xml</param-value> 40 </context-param> 41 42//和hessian服务端一样的接口 43public interface UserService { 44 45 String getUserInfoById (Integer id); 46} 47 48 49@Controller//将该类标注为处理器,并且加入spring容器中 50@RequestMapping("/hessian") 51public class HessianController{ 52 53 @Autowired 54 private UserService userService; 55 56 @RequestMapping(value="/getInfo",method=RequestMethod.GET) 57 @ResponseBody 58 public String getInfo(HttpServletRequest request,HttpServletResponse response){ 59 String userInfo = userService.getUserInfoById(1); 60 return userInfo; 61 } 62 63}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

mysql设置时区

mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0