spring集成Hessian的基本使用方法

Easter79
• 阅读 771

一、什么是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;
    }
    
}
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
spring 4.0.3整合Hessian4.0.38 IDEA
1.1Hessian简介Hessian是一个轻量级的remotingonhttp工具,使用简单的方法提供了RMI的功能。相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。1.2整合对于Hessian是分服务端和客户端的,网上的一些例子也是大部分分开的(S
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k