上篇谈到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 ...
随机推荐
- prototype
-
一个程序中关于多个osgGA::GUIEventHandler同时存在的问题
平时使用GUIEventHandler不太注意handle()函数的返回值,觉得返回true或者false都无所谓,其实不然. 我遇到的问题是程序中一个节点添加了GUIEventHandler对象pi ...
-
【HDOJ】3419 The Three Groups
记忆化搜索. /* 3419 */ #include <cstdio> #include <cstring> #include <cstdlib> #define ...
-
SpringMVC的REST风格的四种请求方式
一. 在HTTP 协议里面,四个表示操作方式的动词:GET.POST.PUT.DELETE. ·它们分别对应四种基本操作: 1.GET ====== 获 取资源 2.POST ======新建资源 ...
-
小白的Python之路 if __name__ == &#39;__main__&#39; 解析
if __name__ == '__main__' 参考文献: http://www.cnblogs.com/xuxm2007/archive/2010/08/04/1792463.html http ...
-
Redis+Restful 构造序列号和压力测试【原创】
[本人原创],欢迎交流和分享技术,转载请附上如下内容:如果你觉得这篇文章对你有帮助,请记得帮我点赞, 谢谢!作者:kevin[转自]http://www.cnblogs.com/itshare/ 很多 ...
- Spark思维导图之性能优化
-
【BZOJ2743】[HEOI2012]采花 离线+树状数组
[BZOJ2743][HEOI2012]采花 Description 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花, ...
-
BugkuCTF WEB
web2 打开链接,一大堆表情 查看源代码 得到 flag 文件上传测试 打开链接 选择 1 个 jpg 文件进行上传,用 burp 抓包改包 将 php 改为 jpg,发包 得到 flag 计算器 ...
-
fastjson用法&;Gson
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson&l ...