zoukankan      html  css  js  c++  java
  • Spring远程调用技术<3>-Spring的HTTP Invoker

    前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙。  另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制。

    Spring提供的http invker是一个新的远程调用模型,作为Spring框架的一部分,能够执行基于HTTP的远程调用(让防火墙不为难),并使用java的序列化机制(让开发者也乐观其变)。

    Spring的HTTPinvoker把HTTP的简单性和java内置的对象序列化机制融合在一起。这使得HTTPinvoker成为替代RMI Hessian Burlap的可选方案

    但是有一个限制:它只是一个Spring框架提供的远程调用方案,意味着客户端和服务端必须是Spring应用。并且表明客户端和服务端都是基于java的。另外使用了java的序列化机制,客户端和服务端必须使用相同版本的类。

    将bean导出为RMI服务,我们使用RmiServiceExporter

    将bean导出为Hessian服务,我们使用HessianServiceExporter

    将bean导出为Burlap服务,我们使用BurlapServiceExporter

    那么导出HTTP Invoker服务,使用HttpInvokerServiceExporter

    配置流程和Hessian、Burlap的一模一样

    服务端:

    WebConfig.java (添加HttpInvokerServiceExporter的配置并绑定url映射 )

    @Bean
        public HandlerMapping mapping(){
            System.out.println("-->Mapping");
            SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
            Properties mappings = new Properties();
            
        //    mappings.setProperty("/burlap.ser", "burlapService");
            //给bean绑定url,bean的名字(burlapService)必须对应
            //mappings.setProperty("/hessian.ser", "hessianService");
            
            mappings.setProperty("/httpInvoker.ser", "httpInvokerServer");
            
            mapping.setMappings(mappings);
            return mapping;
            
        }
        
        @Bean(name="httpInvokerServer")
        public HttpInvokerServiceExporter httpInvokerServer(PersonServer personServer){
            System.out.println("-->httpInvokerServer");
            HttpInvokerServiceExporter invoker = new HttpInvokerServiceExporter();
            invoker.setServiceInterface(PersonServer.class);
            invoker.setService(personServer);
            return invoker;
        }

    客户端:

    package com.mvc.wzy;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
    
    import com.mvc.server.PersonServer;
    
    @Configuration
    public class HttpInvokerContext {
    
        @Bean
        public HttpInvokerProxyFactoryBean httpProxyFactoryBean(){
            HttpInvokerProxyFactoryBean b = new HttpInvokerProxyFactoryBean();
            b.setServiceUrl("http://localhost:8080/Springmvc/httpInvoker.ser");
            b.setServiceInterface(PersonServer.class);
            
            return b;
            
            
        }
    }

    测试:

    //Spring 加载
             ApplicationContext app = 
                    // new AnnotationConfigApplicationContext(com.mvc.wzy.HessianContext.class);
                    // new AnnotationConfigApplicationContext(com.mvc.wzy.BurlapContext.class);
                     new AnnotationConfigApplicationContext(com.mvc.wzy.HttpInvokerContext.class);
             PersonServer p = app.getBean(PersonServer.class);
             System.out.println( p.getMsg());
             System.out.println(p.getPerson());

    结果:

  • 相关阅读:
    PZISP自动下载软件运行时出现“应用程序无法启动,因为应用程序的并行配置不正确”
    分析Memcached客户端如何把缓存数据分布到多个服务器上
    WebApi参数传递总结
    [NHibernate]一对多关系(级联删除,级联添加)
    提高SQL查询效率
    jquery validate 详解
    Winform子窗体刷新父窗体
    Windows如何修改MySQL用户root密码
    MySQL 5.6 for Windows 解压缩版配置安装
    获取地图文档(*.mxd)中比例尺问题
  • 原文地址:https://www.cnblogs.com/wwzyy/p/6099877.html
Copyright © 2011-2022 走看看