您的位置:首页 > 理论基础 > 计算机网络

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

2016-11-24 22:55 405 查看
前面提到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());


结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: