Spring 实现远程访问详解——httpinvoker

上文我们利用Spring rmi实现了Spring的远程访问《Spring 实现远程访问详解——rmi》,本文主要讲解利用HttpInvoke实现远程访问。

Spring httpInvoker使用标准java序列化机制,通过Http暴露业务服务。如果你的参数和返回值是比较复杂的,通过httpInvoker有巨大的优势。

1.      远程访问流程

1)      服务端定义服务接口

2)      服务端实现服务接口

3)      暴露服务对象

4)      客户端定义暴露的服务端接口

5)      客户端配置服务参数

6)      客户端调用服务接口

2.      Spring HttpInvoker具体实现

1)      服务端定义服务接口

<span style="font-family:Times New Roman;">packagecom.lm.core.service;
 
importjava.util.List;
 
importcom.lm.core.entity.User;
 
publicinterface UserHttpService {
   List<User> getUserByAcount(Stringname,String password);
  
   void insert(User user);
}</span>

2)      服务端实现服务接口

<span style="font-family:Times New Roman;">packagecom.lm.core.service.impl;
 
importjava.util.ArrayList;
importjava.util.List;
 
importorg.springframework.beans.factory.annotation.Autowired;
 
importcom.lm.core.entity.User;
importcom.lm.core.mapper.UserMapper;
importcom.lm.core.service.UserHttpService;
importcom.lm.core.service.UserRmiService;
 
publicclass UserHttpServiceImpl implements UserHttpService {
 
   @Autowired
   private UserMapper userMapper;
   @Override
   public List<User>getUserByAcount(String name, String password) {
            System.err.println("httpInvoker获取用户信息:"+ name + password);
            return new ArrayList<User>();
   }
   @Override
   public void insert(User user) {
            System.err.println("httpInvoker开始插入用户信息:"+ user.toString());
   }
 
}</span>

3)      暴露服务对象

<span style="font-family:Times New Roman;"><bean name="userHttpService"class="com.lm.core.service.impl.UserHttpServiceImpl"/>
    <bean name="userExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
    <property name="service" ref="userHttpService"/>
    <property name="serviceInterface" value="com.lm.core.service.UserHttpService"/>
</bean></span>

4)      客户端定义暴露的服务端接口

<span style="font-family:Times New Roman;">packagecom.lm.core.service;
 
importjava.util.List;
 
importcom.lm.core.entity.User;
 
publicinterface UserHttpService {
   List<User> getUserByAcount(Stringname,String password);
  
   void insert(User user);
}</span>

5)      客户端配置服务参数

 

<span style="font-family:Times New Roman;"><bean id="httpInvokerProxy"
       class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
       <property name="serviceUrl"
           value="http://127.0.0.1:8080/spring_remote_server/remoting/userExporter"/>
       <property name="serviceInterface"value="com.lm.core.service.UserHttpService"/>
</bean></span>

6)      客户端调用服务接口

<span style="font-family:Times New Roman;">@RequestMapping(value = "/httpInvokerTest")
    @ResponseBody
    public BaseMapVo httpInvokerTest(String name, String password) {
       BaseMapVo vo = new BaseMapVo();
       long startDate = Calendar.getInstance().getTimeInMillis();
       System.out.println("httpInvoker客户端开始调用" + startDate);
       UserHttpService rmi = (UserHttpService) ApplicationContextUtil.getInstance().getBean("httpInvokerProxy");
       rmi.getUserByAcount("张三", ":张三的密码");
       System.out.println("httpInvoker客户端调用结束" +  (Calendar.getInstance().getTimeInMillis()-startDate));
       vo.setRslt("sucess");
       return vo;
    }</span>

7)      结果输出




  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值