Spring远程调用技术2-Hessian和Burlap
上篇谈到RMI技术,加上Spring的封装,用起来很方便,但也有一些限制
这里的Hessian和Burlap解决了上篇提到的限制,因为他们是基于http的轻量级远程服务。
Hessian,和RMI一样,使用二进制消息进行客户端和服务端的交互,但是它的二进制消息可以移植到其他非java的语言中
Burlap是一种基于XML的远程调用技术,这使它可以移植到任何能解析XML的语言上
pom.xml (这里有的jar是多余的)
"1.0" encoding="UTF-8"?>View Code"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"> 4.0.0 com.spring wzy SpringSource war 1.0.0-BUILD-SNAPSHOT 1.6 4.1.1.RELEASE 1.6.10 1.6.6 org.springframework spring-context ${org.springframework-version} commons-logging commons-logging org.springframework spring-webmvc ${org.springframework-version} org.aspectj aspectjrt ${org.aspectj-version} org.slf4j slf4j-api ${org.slf4j-version} org.slf4j jcl-over-slf4j ${org.slf4j-version} runtime org.slf4j slf4j-log4j12 ${org.slf4j-version} runtime log4j log4j 1.2.15 javax.mail javax.jms jms com.sun.jdmk jmxtools com.sun.jmx jmxri runtime javax.inject javax.inject 1 javax.servlet servlet-api 2.5 provided javax.servlet.jsp jsp-api 2.1 provided javax.servlet jstl 1.2 junit junit 4.11 test com.caucho hessian 4.0.38 org.springframework.security spring-security-core 4.1.1.RELEASE org.springframework.security spring-security-config 4.1.1.RELEASE org.springframework.security spring-security-web 4.1.1.RELEASE net.sf.ehcache ehcache-core 2.6.6 org.springframework spring-test ${org.springframework-version} org.springframework.data spring-data-redis 1.6.2.RELEASE redis.clients jedis 2.8.1 commons-pool commons-pool 1.6 maven-eclipse-plugin 2.9 org.springframework.ide.eclipse.core.springnature org.springframework.ide.eclipse.core.springbuilder true true org.apache.maven.plugins maven-compiler-plugin 2.5.1 1.6 1.6 -Xlint:all true true org.codehaus.mojo exec-maven-plugin 1.2.1 org.test.int1.Main
一、服务端配置:
这里使用的是基于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;}@Overridepublic 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..");}@Overridepublic Person getPerson(){//如果返回的是一个自己创建的对象,需要把这个对象序列化了,不然不行return new Person("aaa",34);}@Overridepublic 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 {/*@Beanpublic ViewResolver viewResolver(){InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix("/WEB-INF/views/");resolver.setSuffix(".jsp");resolver.setExposeContextBeansAsAttributes(true);return resolver;}@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){configurer.enable();}*//*@Beanpublic HandlerMapping hessianMapping(){SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();Properties mappings = new Properties();mappings.setProperty("/hessian.ser", "hessianService");mapping.setMappings(mappings);return mapping;}*/@Beanpublic 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")@Beanpublic BurlapServiceExporter burlapService(PersonServer personServer){System.out.println("-->burlapService");BurlapServiceExporter exporter = new BurlapServiceExporter();exporter.setService(personServer);exporter.setServiceInterface(PersonServer.class);return exporter;}//配置hessian服务@Beanpublic HessianServiceExporter hessianService(PersonServer personServer){System.out.println("-->hessianService");HessianServiceExporter exporter = new HessianServiceExporter();exporter.setService(personServer);exporter.setServiceInterface(PersonServer.class);return exporter;}@Beanpublic PersonServer personServer(){return new PersonServerImpl();} }
6.MyDispatcherServlet.java
package com.mvc.config;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class MyDispatcherServlet extends AbstractAnnotationConfigDispatcherServletInitializer{@Overrideprotected Class>[] getRootConfigClasses() {return new Class>[]{RootConfig.class};}@Overrideprotected Class>[] getServletConfigClasses() {return new Class>[]{WebConfig.class};}@Overrideprotected 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 {@Beanpublic 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 {@Beanpublic 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

转载于:https://www.cnblogs.com/wwzyy/p/6095355.html
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
