zoukankan      html  css  js  c++  java
  • 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.0</modelVersion>
        <groupId>com.spring</groupId>
        <artifactId>wzy</artifactId>
        <name>SpringSource</name>
        <packaging>war</packaging>
        <version>1.0.0-BUILD-SNAPSHOT</version>
        <properties>
            <java-version>1.6</java-version>
            <org.springframework-version>4.1.1.RELEASE</org.springframework-version>
            <org.aspectj-version>1.6.10</org.aspectj-version>
            <org.slf4j-version>1.6.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.15</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>1</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.38</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.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.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.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.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.2.RELEASE</version>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.8.1</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.1</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.1</version>
                    <configuration>
                        <mainClass>org.test.int1.Main</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    View Code

    一、服务端配置:

    这里使用的是基于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",34);
        }
    
        @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

  • 相关阅读:
    Python 音视频方面资源大全
    MD5( 信息摘要算法)的概念原理及python代码的实现
    base64编解码学习及python代码实现
    操作系统下cache的几个概念
    scp 跨机远程拷贝
    VIM -小技巧汇总
    python中的进制转换
    linux目录跳转快捷方式——z武器
    ubuntu 16.04 清空log文件的方法
    【linux】crontab失效
  • 原文地址:https://www.cnblogs.com/wwzyy/p/6095355.html
Copyright © 2011-2022 走看看