Spring Remoting with Hessian and Burlap – 用黑纱和麻布制作的春景图

最后修改: 2017年 2月 26日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In the previous article titled “Intro to Spring Remoting with HTTP Invokers” we saw how easy is to set up a client/server application that leverages remote method invocation (RMI) through Spring Remoting.

前一篇题为 “Spring Remoting与HTTP Invokers介绍”的文章中,我们看到通过Spring Remoting建立一个利用远程方法调用(RMI)的客户端/服务器应用程序是多么容易。

In this article, we will show how Spring Remoting supports the implementation of RMI using Hessian and Burlap instead.

在这篇文章中,我们将展示Spring Remoting如何支持使用HessianBurlap代替RMI的实现。

2. Maven Dependencies

2.Maven的依赖性

Both Hessian and Burlap are provided by the following library that you will need to include explicitly in your pom.xml file:

HessianBurlap都由以下库提供,你需要在你的pom.xml文件中明确包含。

<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.38</version>
</dependency>

You can find the latest version on Maven Central.

您可以在Maven Central上找到最新版本。

3. Hessian

3.Hessian

Hessian is a lightweight binary protocol from Caucho, the makers of the Resin application server. Hessian implementations exist for several platforms and languages, Java included.

HessianCaucho的轻量级二进制协议,该公司是Resin应用服务器的制造商。Hessian的实现存在于多个平台和语言中,包括Java。

In the following subsections, we will modify the “cab booking” example presented in the previous article to make the client and the server to communicate using Hessian instead of the Spring Remote HTTP based protocol.

在下面的小节中,我们将修改上一篇文章中介绍的 “出租车预订 “的例子,使客户端和服务器使用Hessian而不是基于Spring Remote HTTP协议进行通信。

3.1. Exposing the Service

3.1.暴露服务

Let’s expose the service by configuring a RemoteExporter of type HessianServiceExporter, replacing the HttpInvokerServiceExporter previously used:

让我们通过配置一个RemoteExporter类型的HessianServiceExporter来公开该服务,取代之前使用的HttpInvokerServiceExporter

@Bean(name = "/booking") 
RemoteExporter bookingService() {
    HessianServiceExporter exporter = new HessianServiceExporter();
    exporter.setService(new CabBookingServiceImpl());
    exporter.setServiceInterface( CabBookingService.class );
    return exporter;
}

We can now start the server and keep it active while we prepare the client.

现在我们可以启动服务器,并在准备客户端时保持其活跃。

3.2. Client Application

3.2.客户端应用程序

Let’s implement the client. Here again, the modifications are quite simple — we need to replace the HttpInvokerProxyFactoryBean with a HessianProxyFactoryBean:

让我们来实现客户端。这里的修改也很简单–我们需要用一个HttpInvokerProxyFactoryBean来代替HessianProxyFactoryBean

@Configuration
public class HessianClient {

    @Bean
    public HessianProxyFactoryBean hessianInvoker() {
        HessianProxyFactoryBean invoker = new HessianProxyFactoryBean();
        invoker.setServiceUrl("http://localhost:8080/booking");
        invoker.setServiceInterface(CabBookingService.class);
        return invoker;
    }

    public static void main(String[] args) throws BookingException {
        CabBookingService service
          = SpringApplication.run(HessianClient.class, args)
              .getBean(CabBookingService.class);
        out.println(
          service.bookRide("13 Seagate Blvd, Key Largo, FL 33037"));
    }
}

Let’s now run the client to make it connect to the server using Hessian.

现在让我们运行客户端,使其使用Hessian连接到服务器。

4. Burlap

4.麻布

Burlap is another lightweight protocol from Caucho, based on XML. Caucho stopped maintaining it a long time ago, and for that, its support has been deprecated in the newest Spring releases, even though it is already present.

BurlapCaucho的另一个轻量级协议,基于XMLCaucho很久以前就停止了对它的维护,为此,在最新的Spring版本中已经取消了对它的支持,尽管它已经存在了。

Therefore you should reasonably continue using Burlap only if you have applications that are already distributed and that cannot easily be migrated to another Spring Remoting implementation.

因此,只有当您的应用程序已经发布,并且不能轻易迁移到另一个Spring Remoting实现时,您才应该合理地继续使用Burlap

4.1. Exposing the Service

4.1.暴露服务

We can use Burlap exactly in the same way that we used Hessian — we just have to choose the proper implementation:

我们可以完全以使用Burlap的方式来使用Hessian–我们只需要选择适当的实现。

@Bean(name = "/booking") 
RemoteExporter burlapService() {
    BurlapServiceExporter exporter = new BurlapServiceExporter();
    exporter.setService(new CabBookingServiceImpl());
    exporter.setServiceInterface( CabBookingService.class );
    return exporter;
}

As you can see, we just changed the type of exporter from HessianServiceExporter to BurlapServiceExporter. All the setup code can be left unchanged.

正如你所看到的,我们只是将导出器的类型从HessianServiceExporter改为BurlapServiceExporter.所有的设置代码都可以不做改动。

Again, let’s start the server and let’s keep it running while we work on the client.

再一次,让我们启动服务器,让它保持运行,同时我们在客户端上工作。

4.2. Client Implementation

4.2.客户端实施

We can likewise swap Hessian for Burlap at the client side, changing out HessianProxyFactoryBean with BurlapProxyFactoryBean:

我们同样可以在客户端将Hessian换成Burlap,将HessianProxyFactoryBean改为BurlapProxyFactoryBean

@Bean
public BurlapProxyFactoryBean burlapInvoker() {
    BurlapProxyFactoryBean invoker = new BurlapProxyFactoryBean();
    invoker.setServiceUrl("http://localhost:8080/booking");
    invoker.setServiceInterface(CabBookingService.class);
    return invoker;
}

We can now run the client and see how it connects successfully to the server application using Burlap.

我们现在可以运行客户端,看看它是如何使用Burlap成功连接到服务器应用程序的。

5. Conclusion

5.结论

With these quick examples, we showed how it is easy with Spring Remoting to choose among different technologies to implement remote method invocation and how you can develop an application being completely unaware of the technical details of the protocol used to represent the remote method invocation.

通过这些快速的例子,我们展示了使用Spring Remoting在不同的技术中选择实现远程方法调用是多么的容易,以及你如何在完全不知道用于表示远程方法调用的协议的技术细节的情况下开发一个应用程序。

As usual, you’ll find the sources over on GitHub, with clients for both Hessian and Burlap and the JUnit test CabBookingServiceTest.java that will take care of running both the server and the clients.

像往常一样,你可以在GitHub上找到源代码,包括HessianBurlap的客户端以及JUnit测试CabBookingServiceTest.java,它将负责运行服务器和客户端。