【RPC框架Hessian一】Hessian RPC Hello World

什么是Hessian

The Hessian binary web service protocol makes web services usable without requiring a large framework, and without learning yet another alphabet soup of protocols. Because it is a binary protocol, it is well-suited to sending binary data without any need to extend the protocol with attachments.

 

Hessian版HelloWorld

1. 创建基于Maven的web项目

2. pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>user.project</artifactId>
        <groupId>com.tom</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>learn.hessian</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>4.0.38</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--jetty plugin to manage embedded jetty-->
            <!--No goal  is specified-->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.7</version>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8668</port>
                            <maxIdleTime>30000</maxIdleTime>
                        </connector>
                    </connectors>
                    <webAppSourceDirectory>${project.basedir}/web
                    </webAppSourceDirectory>
                    <contextPath>/web</contextPath>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

 上面主要配置了三部分内容,一是依赖的Hessian包,二是Hessian依赖的servlet-api包(Hessian包虽然依赖servlet-api,但在Hessian包的依赖关系中并没有显示的去依赖servlet-api包),三是添加jetty插件,用于启动web项目

 

3.定义服务器和客户端都会依赖的接口service

在HelloWorld中,没有复杂的Model对象,在实际项目中,服务器端和客户端共同依赖的内容包括模型对象和接口Service

 

 

package com.tom.hessian.common;

import java.io.IOException;

public interface IGreetingService {
    public String greeting(String name) throws IOException;
}

 

 

4.定义服务器接口实现

 

package com.tom.hessian.server;

import com.tom.hessian.common.IGreetingService;

import java.io.IOException;

public class GreetingService implements IGreetingService{
    @Override
    public String greeting(String name) throws IOException {
        return "Welcome ot the Hassian world, " + name;
    }
}

 

 

5. 配置web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>hessian</servlet-name>
        <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
        <init-param>
            <param-name>home-api</param-name><!--接口声明-->
            <param-value>com.tom.hessian.common.IGreetingService</param-value>
        </init-param>
        <init-param>
            <param-name>home-class</param-name><!--接口实现-->
            <param-value>com.tom.hessian.server.GreetingService</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/hessian</url-pattern>
    </servlet-mapping>
</web-app>

 

 

6.客户端代码

package com.tom.hessian.client;

import com.caucho.hessian.client.HessianProxyFactory;
import com.tom.hessian.common.IGreetingService;


public class GreetingServiceTest {
    public static void main(String[] args) throws Exception {
        //RPC访问地址
        String url = "http://localhost:8668/web/hessian";

        //接口的动态代理工厂
        HessianProxyFactory factory = new HessianProxyFactory();
        IGreetingService greetingService = (IGreetingService) factory.create(IGreetingService.class, url);

        //远程方法调用
        System.out.println("hello(), " + greetingService.greeting("tom"));
    }
}

 

7.运行

启动jetty服务器,运行客户端main方法,得到如下输出,证明远程方法调用成功

hello, welcome ot the Hassian world, tom

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Hello world)