Spring远程调用技术<2>-Hessian和Burlap
上篇谈到RMI技术,加上Spring的封装,用起来很方便,但也有一些限制
这里的Hessian和Burlap解决了上篇提到的限制,因为他们是基于http的轻量级远程服务。
Hessian,和RMI一样,使用二进制消息进行客户端和服务端的交互,但是它的二进制消息可以移植到其他非java的语言中
Burlap是一种基于XML的远程调用技术,这使它可以移植到任何能解析XML的语言上
pom.xml (这里有的jar是多余的)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.spring</groupId>
<artifactId>wzy</artifactId>
<name>SpringSource</name>
<packaging>war</packaging>
<version>1.0.-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>4.1..RELEASE</org.springframework-version>
<org.aspectj-version>1.6.</org.aspectj-version>
<org.slf4j-version>1.6.</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency> <!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency> <!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency> <!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version></version>
</dependency> <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.caucho/hessian -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.1..RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.1..RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.1..RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache-core -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6..RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
一、服务端配置:
这里使用的是基于SpringMVC的配置,配置文件是纯java配置。
当配置好后,web.xml不用再进行任何配置,当tomcat启动时会找到自定义的DispatcherServlet并自动加载它
1.Person.java
package com.mvc.entity; import java.io.Serializable; public class Person
implements Serializable
{ /**
* 注意:如果需要返回这个对象,需要实现序列化
*
* */ public Person(){}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
} }
2.PersonServer.java
package com.mvc.server; import org.springframework.stereotype.Service; import com.mvc.entity.Person; public interface PersonServer { public Person getPerson(); public String getMsg();
}
3.PersonServerImpl.java
package com.mvc.server; import java.io.Serializable; import org.springframework.stereotype.Service; import com.mvc.entity.Person; @Service
public class PersonServerImpl implements PersonServer { public PersonServerImpl(){
System.out.println("PersonServer.."); }
@Override
public Person getPerson(){
//如果返回的是一个自己创建的对象,需要把这个对象序列化了,不然不行
return new Person("aaa",);
} @Override
public String getMsg() {
//String已经实现了序列化,所以说返回的对象必须实现序列化
return "hello wzy";
}
}
4.RootConfig.java
package com.mvc.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
//@ComponentScan("com.mvc.server")
public class RootConfig { //这里没有配置 }
5.WebConfig.java
package com.mvc.config; import java.util.Properties; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.BurlapServiceExporter;
import org.springframework.remoting.caucho.HessianServiceExporter;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import com.mvc.server.PersonServer;
import com.mvc.server.PersonServerImpl; @Configuration
@EnableWebMvc
//@ComponentScan("com.mvc.action")
public class WebConfig
//extends WebMvcConfigurerAdapter
{
/*
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver; } @Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer){
configurer.enable(); }
*/ /*
@Bean
public HandlerMapping hessianMapping(){
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
Properties mappings = new Properties();
mappings.setProperty("/hessian.ser", "hessianService");
mapping.setMappings(mappings);
return mapping; }
*/ @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"); mapping.setMappings(mappings);
return mapping; }
//配置burlap服务
@SuppressWarnings("deprecation")
@Bean
public BurlapServiceExporter burlapService(PersonServer personServer){
System.out.println("-->burlapService");
BurlapServiceExporter exporter = new BurlapServiceExporter();
exporter.setService(personServer);
exporter.setServiceInterface(PersonServer.class);
return exporter;
} //配置hessian服务
@Bean
public HessianServiceExporter hessianService(PersonServer personServer){
System.out.println("-->hessianService");
HessianServiceExporter exporter = new HessianServiceExporter();
exporter.setService(personServer);
exporter.setServiceInterface(PersonServer.class);
return exporter;
} @Bean
public PersonServer personServer(){ return new PersonServerImpl();
}
}
6.MyDispatcherServlet.java
package com.mvc.config; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class MyDispatcherServlet
extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
} @Override
protected String[] getServletMappings() {
return new String[]{"*.ser"};
} }
以上就是服务端的全部配置,放入tomcat就可以跑了
二、客户端的配置,一种是java直接调用,另一种是基于Spring调用
(Spring的jar和Hessian的jar是不能少的)
1.PersonServer.java(还是服务端的那个接口)
package com.mvc.server; import org.springframework.stereotype.Service; import com.mvc.entity.Person; public interface PersonServer { public Person getPerson(); public String getMsg();
}
2.BurlapContext.java(配置burlap客户端)
package com.mvc.wzy; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.BurlapProxyFactoryBean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean; import com.mvc.server.PersonServer; @Configuration
public class BurlapContext { @Bean
public BurlapProxyFactoryBean burlapProxyFactory(){
BurlapProxyFactoryBean proxyFactory = new BurlapProxyFactoryBean();;
proxyFactory.setServiceUrl("http://localhost:8080/Springmvc/burlap.ser");
proxyFactory.setServiceInterface(PersonServer.class); return proxyFactory;
}
}
3.HessianContext.java(配置hessian客户端)
package com.mvc.wzy; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.BurlapProxyFactoryBean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean; import com.mvc.server.PersonServer; @Configuration
public class HessianContext { @Bean
public HessianProxyFactoryBean hessianProxyFactory(){
HessianProxyFactoryBean proxyFactory = new HessianProxyFactoryBean();;
proxyFactory.setServiceUrl("http://localhost:8080/Springmvc/hessian.ser");
proxyFactory.setServiceInterface(PersonServer.class); return proxyFactory;
} }
4.Test.java
package com.mvc.wzy; import java.net.MalformedURLException; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.remoting.caucho.BurlapProxyFactoryBean;
import org.springframework.remoting.caucho.HessianProxyFactoryBean; import com.caucho.burlap.client.BurlapProxyFactory;
import com.caucho.hessian.client.HessianProxyFactory;
import com.mvc.server.PersonServer; public class Test { public static void main(String[] args) throws MalformedURLException { //Spring 实现客户端
ApplicationContext app =
// new AnnotationConfigApplicationContext(com.mvc.wzy.HessianContext.class);
new AnnotationConfigApplicationContext(com.mvc.wzy.BurlapContext.class);
PersonServer p = app.getBean(PersonServer.class);
System.out.println( p.getMsg());
System.out.println(p.getPerson()); /*
* java代码实现Hessian客户端
*/
// HessianProxyFactory hfactory = new HessianProxyFactory();
// PersonServer service =
// (PersonServer) hfactory.create(PersonServer.class, "http://localhost:8080/Springmvc/hessian.ser");
// System.out.println(service.getMsg());//返回的对象一定要实现序列化
// System.out.println(service.getPerson().toString()); /*
* java代码实现Burlap客户端
*/
// BurlapProxyFactory bfactory = new BurlapProxyFactory();
// service =
// (PersonServer) bfactory.create(PersonServer.class, "http://localhost:8080/Springmvc/burlap.ser");
//
// System.out.println(service.getMsg());//返回的对象一定要实现序列化
// System.out.println(service.getPerson().toString()); } }
调用ok
Spring远程调用技术<2>-Hessian和Burlap的更多相关文章
- Spring远程调用技术<1>-RMI
在java中,我们有多种可以使用的远程调用技术 1.远程方法调用(remote method invocation, RMI) 适用场景:不考虑网络限制时(例如防火墙),访问/发布基于java的服务 ...
- Spring远程调用技术<3>-Spring的HTTP Invoker
前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙. 另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制. Spring提供的http invke ...
- 【Java EE 学习 78 中】【数据采集系统第十天】【Spring远程调用】
一.远程调用概述 1.远程调用的定义 在一个程序中就像调用本地中的方法一样调用另外一个远程程序中的方法,但是整个过程对本地完全透明,这就是远程调用.spring已经能够非常成熟的完成该项功能了. 2. ...
- WebService远程调用技术
1.---------------------------------介绍-------------------------------------------------- (1)远程调用:一个系统 ...
- C#远程调用技术WebService葵花宝典
一.课程介绍 直接开门见山吧,在学习之前阿笨想问大家一句,关于WebService远程过程调用技术(RPC) 你真的会了吗?不要跟老夫扯什么WebService技术已经过时,如果你的内心有在偷偷告诉你 ...
- C#远程调用技术WebService修炼手册
一.课程介绍 一位伟大的讲师曾经说过一句话:事物存在即合理!意思就是说:任何存在的事物都有其存在的原因,存在的一切事物都可以找到其存在的理由,我们应当把焦点放在因果关联的本质上.所以在本次分享课开课之 ...
- Spring 远程调用工具类RestTemplateUtils
Spring 远程调用Rest服务工具类,包含Get.Post.Put.Delete四种调用方式. 依赖jar <dependency> <groupId>org.spring ...
- Java[2] 分布式服务架构之java远程调用技术浅析(转http://www.uml.org.cn/zjjs/201208011.asp)
转自:http://www.uml.org.cn/zjjs/201208011.asp 在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如: ...
- Spring的远程调用
Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易 四种远程调用技术: ◆ 远程方法调用(RMI) ◆ Caucho的Hessian和Burlap ...
随机推荐
- Thinking in Java——笔记(19)
Enumerated Types Basic enum features When you create an enum, an associated class is produced for yo ...
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
原因分析 首先我们来看下Spring mvc 中文件上传的配置 <bean id="multipartResolver" class="org.springfram ...
- java Properties异常:Malformed \uxxxx encoding.
昨天项目中遇到一个 java.lang.IllegalArgumentException: Malformed \uxxxx encoding.这样的一个异常,debug了一下发现是读取propert ...
- 国内IT软件开发人员现状
首先在这里讨论的是国内的大陆地区.在今天这个中国IT环境下,开发人员出路何在?一个优秀开发人,应该有致力于编写优雅代码,让别人读得懂,具有可读性,可测试性的代码,不仅仅是可以运行的代 ...
- android px,dp,sp大小转换工具
package com.voole.playerlib.util; import android.content.Context; /** * Android大小单位转换工具类<br/> ...
- FusionCharts的使用方法(超详细)
今天统计价格变化规律的时候找到的一个很好的文档,很详细 一.简介 Ø FusionCharts 是InfoSoft Global 公司的一个产品,InfoSoft Global 公司是专业的Flash ...
- 【转】visio中关于shape属性的修改和读取
PS: 本文转自: http://blog.sina.com.cn/s/blog_6bcfb9420100wzxf.html visio中都是shape,shape就是一个对象,要想实现对shape ...
- SharePoint 2007 User Re-created in AD with new SID issue on MySite
When active directory users get deleted and re-created, even with the same user id, there's a nasty ...
- 视图xsl定制之嵌入服务器控件
SharePoint 2010 视图 xsl 文件中支持嵌入服务器控件,嵌入服务器控件时,系统先采用xsl将视图xml解析成一个类似UserControl的存在,然后执行UserControl. 代码 ...
- Android 监听ScrollView的滑动
我们需要监听ScroView的滑动情况,比如滑动了多少距离,是否滑到布局的顶部或者底部.可惜的是SDK并没有相应的方法,不过倒是提供了一个 protected void onScrollChanged ...