上篇谈到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的更多相关文章

  1. Spring远程调用技术<1>-RMI

    在java中,我们有多种可以使用的远程调用技术 1.远程方法调用(remote method invocation, RMI)  适用场景:不考虑网络限制时(例如防火墙),访问/发布基于java的服务 ...

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

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

  3. 【Java EE 学习 78 中】【数据采集系统第十天】【Spring远程调用】

    一.远程调用概述 1.远程调用的定义 在一个程序中就像调用本地中的方法一样调用另外一个远程程序中的方法,但是整个过程对本地完全透明,这就是远程调用.spring已经能够非常成熟的完成该项功能了. 2. ...

  4. WebService远程调用技术

    1.---------------------------------介绍-------------------------------------------------- (1)远程调用:一个系统 ...

  5. C#远程调用技术WebService葵花宝典

    一.课程介绍 直接开门见山吧,在学习之前阿笨想问大家一句,关于WebService远程过程调用技术(RPC) 你真的会了吗?不要跟老夫扯什么WebService技术已经过时,如果你的内心有在偷偷告诉你 ...

  6. C#远程调用技术WebService修炼手册

    一.课程介绍 一位伟大的讲师曾经说过一句话:事物存在即合理!意思就是说:任何存在的事物都有其存在的原因,存在的一切事物都可以找到其存在的理由,我们应当把焦点放在因果关联的本质上.所以在本次分享课开课之 ...

  7. Spring 远程调用工具类RestTemplateUtils

    Spring 远程调用Rest服务工具类,包含Get.Post.Put.Delete四种调用方式. 依赖jar <dependency> <groupId>org.spring ...

  8. Java[2] 分布式服务架构之java远程调用技术浅析(转http://www.uml.org.cn/zjjs/201208011.asp)

    转自:http://www.uml.org.cn/zjjs/201208011.asp 在分布式服务框架中,一个最基础的问题就是远程服务是怎么通讯的,在Java领域中有很多可实现远程通讯的技术,例如: ...

  9. Spring的远程调用

    Spring远程支持是由普通(Spring)POJO实现的,这使得开发具有远程访问功能的服务变得相当容易 四种远程调用技术: ◆ 远程方法调用(RMI) ◆ Caucho的Hessian和Burlap ...

随机推荐

  1. 浅谈web网站架构演变过程

    前言 我们以javaweb为例,来搭建一个简单的电商系统,看看这个系统可以如何一步步演变.   该系统具备的功能:   用户模块:用户注册和管理 商品模块:商品展示和管理 交易模块:创建交易和管理 阶 ...

  2. xp IP安全策略 ipseccmd

    ///下载 ipseccmd.exe //禁止 xp 连接 public static void BannedXPRunCmd() { string str = Console.ReadLine(); ...

  3. c++函数模板作为类的成员函数,编译报错LNK2019的解决方法

    为了使某个类的成员函数能对不同的参数进行相同的处理,需要用到函数模板,即template<typename T> void Function(). 编译时报错LNK2019 解决方法: 1 ...

  4. Lind.DDD.IoC(大叔推荐)~在服务定位器中引入IoC容器~容器的适配器

    回到目录 关于依赖倒置(DIP) 高层模块不依赖于低层模块的实现,而低层模块依赖于高层模块定义的接口,通俗的讲,就是高层模块定义接口,低层模块负责实现,这在我们实际开发中经常被用到,层与层之间引用,经 ...

  5. SharePoint 2013 页面访问,Url中间多一段"_layouts/15/start.aspx#"

    问题描述: 我想访问如下页面 http://Host/_layouts/15/ManageFeatures.aspx 点击以后页面地址没有错,但是中间多了一段“_layouts/15/start.as ...

  6. JAVA静态代理模式(从现实生活角度理解代码原理)

    代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问. 代理模式说白了就是"真实对象"的代表,在访问对象时引入一定程度的间接性,因为这种间接性可以附加多种用途. 在 ...

  7. elasticsearch GIS空间查询问题解决

    在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询     我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...

  8. 获取本机IP地址

    这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...

  9. 设计模式 之 命令(Command)模式

    命令模式: 命令模式是对命令的封装.它将发出的命令的责任和执行的命令分隔开,委派给不同的对象.被委派的对象虽然具有执行的命令,但只有在接收到命令后,才会执行命令. 下面是命令模式的结构图: 命令模式涉 ...

  10. java HelloWorld 提示“错误: 找不到或无法加载主类 HelloWorld“解决方案

    在检查环境变量等前提工作准确无误后,注意要配好CLASSPATH,仍然报“错误: 找不到或无法加载主类 HelloWorld“. 本人工程目录:mygs-maven/src/main/java/hel ...