码迷,mamicode.com
首页 > 编程语言 > 详细

Spring远程调用技术<3>-Spring的HTTP Invoker

时间:2016-11-25 00:45:18      阅读:360      评论:0      收藏:0      [点我收藏+]

标签:部分   server   url   不为   color   system   导出   map   on()   

前面提到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());

 

结果:

技术分享

 

Spring远程调用技术<3>-Spring的HTTP Invoker

标签:部分   server   url   不为   color   system   导出   map   on()   

原文地址:http://www.cnblogs.com/wwzyy/p/6099877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!