PHP头条
热点:

Hessian通讯协议【附PHP源代码】


提示:
  绿色加粗有下划线是文本的链接
  蓝色加粗强调
  红色加粗重点

PHP源码:
  点击下载

  

什么是Hessian

  

  Hessian是由caucho提供的一种开源的远程通讯协议。

  采用二进制 RPC 协议,基于 HTTP 传输,服务器端不用另开放防火墙端口。

  协议的规范是公开的,可以用于任意语言。

  采用客户机/服务器模式。

  请求程序就是一个客户机,而服务提供程序就是一个服务器。

  客户机调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。

  在服务器端,进程保持睡眠状态直到调用信息的到达为止。

  当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用进程接收答复信息,

  获得进程结果,然后调用执行继续进行。

 

Hessian协议工作流程图

  

  客户端程序请求服务端函数 

  1.调用客户端句柄,执行传送参数。

  2.调用本地系统内核发送网络消息。

  3.消息传送到远程主机。

  4.服务器句柄得到消息并取得参数。

  5.执行远程过程。

  

  

  服务端函数返回结果给客户端 

  1.执行的过程将结果返回服务器句柄。

  2.服务器句柄返回结果,调用远程系统内核。

  3.消息传回本地主机。

  4.客户句柄由内核接收消息。

  5.客户接收句柄返回的数据。

  

 

  

Hessian的优、缺点

  

  优缺点在实践中根据使用和同事反馈情况而写的,只代表个人的一些观点。

  
  优点
  1.目前的WEB端基本上都支持hessian,hessian本身就是使用http的传输协议,来保证安全性和稳定性。

  2.通信速度不错。(数据少的时候)。

  3.可以以对象方式接受数据。

  

  缺点

  1.报错机制不够完善。

  2.没有事务处理。

  3.PHP和Java之间经常会出现一些问题,如编码,需强制转换字符等。(已经在源码包体现了

  4.返回数据量大,总会感觉有延迟的情况。(返回数据加缓存机制可以避免)

 

附带源码解释

 1.引用配置文件,包括网站根目录,以及Hessian的地址。

 2.我本地配置的虚拟主机(域名qx.com),不会配置虚拟主机请参考我之前搭建环境文章。

php
/**
 * 文件名        : config.php
 * 创建人        : 吴佰清
 * 创建时间    : 2012-05-09 11:12
 * 用途        : Hessian配置文件
 *
 * @author wubaiqing 
 * @package system.core.code applied to the whole site
 * @copyright Copyright (c) 2012 
 * @since 1.0
 */

// 根目录
define( 'PATH' , dirname(__FILE__) . DIRECTORY_SEPARATOR );

// Hessian Url地址
define( 'HESSIAN_URL' , 'http://qx.com/server.php' );

// IDE : Zend Studio 9.0
// IDE Extension : Toggle Vrapper
?>

  3.下载HessianPHP.

  4.配置服务端。

php
/**
 * 文件名        : server.php
 * 创建人        : 吴佰清
 * 创建时间    : 2012-05-09 11:54
 *
 * 参考资料    :
 * 1.http://hessian.caucho.com/ ( Hessian主页 )
 * 2.http://hessianphp.sourceforge.net/ ( Hessian PHP )
 * 3.http://sourceforge.net/projects/hessianphp/ ( Hessian PHP开源 )
 * 4.http://baike.baidu.com/view/1859857.htm ( 单例模式 )
 *
 * @author wubaiqing 
 * @package system.core applied to the whole site
 * @copyright Copyright (c) 2012
 * @since 1.0
 */
require_once ( dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.php' );
require_once ( PATH . 'extensions/HessianPHP/HessianService.php' );

class HessianServer
{
    public function __construct() {}
    /**
     * 商品详细信息APi接口
     * @param string $title 标题
     * @param int $price 价格
     */
    public function goodsInfomationApi( $title , $price ) {
        $price = (int) $price;
        return '

使用Hessian协议调用远程方法. 标题:' . $title . '
价格:'.$price; } } $server = new HessianService( new HessianServer() ); //$server->displayInfo(); $server->handle(); // IDE : Zend Studio 9.0 // IDE Extension : Toggle Vrapper ?>


 5.可以通过HessianService类中的displayInfo方法去查看开启多少个通讯方法。

  如果搭建服务端要使用handle方法,如出现Hessian Requires POST提示,服务端就已经搭建成功。

 

 6.封装Hessian接口

php
/**
 * 类名        : HessianApi
 * 创建人        : 吴佰清
 * 创建时间    : 2012-05-08 18:00
 * 
 * 参考资料    : 
 * 1.http://hessian.caucho.com/ ( Hessian主页 )
 * 2.http://hessianphp.sourceforge.net/ ( Hessian PHP )
 * 3.http://sourceforge.net/projects/hessianphp/ ( Hessian PHP开源 )
 * 4.http://baike.baidu.com/view/1859857.htm ( 单例模式 )
 *
 * @author wubaiqing 
 * @package system.core applied to the whole site
 * @copyright Copyright (c) 2012
 * @since 1.0
 */
class HessianApi
{
    /**
     * @var string 接口地址
     */
    private $_url = NULL;
    
    /**
     * @var result 句柄
     */
    private $_handle = NULL;

    /**
     * @var array 存放单例模式数组
     */
    private static $_objects = array();

    /**
     * 设置URL地址
     * 实例化HessianClient类
     * 参数    : (1) url地址 , 2
     * 
     * 2.Java调用字段
     * @param string $url
     */
    public function __construct( $url )
    {
        $this->setUrl( $url );
        $handler = new HessianClient ( $this->getUrl (), $this->getOptions () );
        $this->setHandler ( $handler );
    }

    /**
     * @return result $_handle 句柄
     */
    public function getHandler() {
        return $this->_handle;
    }

    /**
     * 设置句柄
     * @param result $_handle
     */
    public function setHandler($_handle) {
        $this->_handle = $_handle;
    }

    /**
     * 获取URL地址
     */
    public function getUrl() {
        return $this->_url;
    }

    /**
     * 设置URL地址
     * @param string $url
     */
    public function setUrl($url) {
        $this->_url = $url;
    }

    /**
     * typeMap映射Java等平台对象
     * @return array
     */
    public function getOptions() {
        return array (
      'version' => 1,
      'saveRaw' => TRUE,
      'typeMap' => array(
        'JavaNullPointException' => 'java.lang.NullPointerException' ,
        'StackTraceElement' => 'java.lang.StackTraceElement')
     ); }
/** * 记录接口调用信息 * @param string $method 调用的方法 * @param string $returnMsg 需要记入log的文字信息 */ public function resultLog( $method , $returnMsg ) { $logPath = PATH.'/runtime/hessian/'; if( !is_dir( $logPath ) ) { mkdir($logPath,0777); } error_log(date('Ymd H:i:s', time()) . '|' . $method . '|' . $returnMsg."\n", 3, $logPath . date('Y-m-d', time()) . '.log'); } /** * 静态工厂方法,生成单个URL的唯一实例 * @param string $url */ public static function start( $url ) { $key = md5( $url ); if ( isset(self::$_objects[$key]) ) { return self::$_objects[$key]; } self::$_objects[$key] = new HessianApi( $url ); return self::$_objects[$key]; } } class JavaNullPointException extends Exception {} class StackTraceElement extends Exception {} // IDE : Zend Studio 9.0 // IDE Extension : Toggle Vrapper ?>

 7.封装客户端请求方法,继承HessianApi类

php
/**
 * 类名        : Goods
 * 继承类        : HessianApi
 * 创建人        : 吴佰清
 * 创建时间    : 2012-05-09 12:12
 * 用途        : 调用server.php方法
 *
 * @author wubaiqing 
 * @package system.core.code applied to the whole site
 * @copyright Copyright (c) 2012 
 * @since 1.0
 */
class Goods extends HessianApi
{
    /**
     * 设置接口地址
     * @param string $url
     */
    public function __construct( $url ) {
        parent::__construct( $url );
    }

    /**
     * 获取商品信息
     * 调用server.php文件中的goodsInfomationApi方法
     * @param string $title 标题
     * @param string $title 价格
     */
    public function getGoodsInfomation( $title , $price )
    {
        // 如果调用java平台的hessian服务 需要指定你传递参数的类型,特别是整形和字符串.
        $price = (int) $price; 
        
        $result = $this->getHandler()->goodsInfomationApi( $title , $price );
        $this->resultLog( 'getGoodsInfomation' , '访问接口,但接口没有进行逻辑验证.');
        return $result;
    }
}

// IDE : Zend Studio 9.0
// IDE Extension : Toggle Vrapper
?>

 8.修改index.php可以请求服务端接口

php 
/**
 * 文件名        : index.php
 * 创建人        : 吴佰清
 * 创建时间    : 2012-05-09 11:57
 *
 * 参考资料    :
 * 1.http://hessian.caucho.com/ ( Hessian主页 )
 * 2.http://hessianphp.sourceforge.net/ ( Hessian PHP )
 * 3.http://sourceforge.net/projects/hessianphp/ ( Hessian PHP开源 )
 * 4.http://baike.baidu.com/view/1859857.htm ( 单例模式 )
 *
 * @author wubaiqing 
 * @package system.core applied to the whole site
 * @copyright Copyright (c) 2012
 * @since 1.0
 */


require_once ( dirname(__FILE__) . DIRECTORY_SEPARATOR .'config.php' );

// Hessian 扩展及配置文件
require_once ( PATH . 'extensions/HessianPHP/HessianClient.php' );
require_once ( PATH . 'class/HessianApi.php' );


// 调用 server.php 方法
require_once ( PATH . 'class/Goods.php');

// 请求接口获取数据
$goods = new Goods( HESSIAN_URL );

// 设置商品标题 , 价格.
$title = '北京移动充值平台';
$price = '50';

// 请求Hessian协议
$goodsInfo = $goods->getGoodsInfomation( (string) $title , (int) $price );

// 打印请求结果
echo ( $goodsInfo );

// IDE : Zend Studio 9.0
// IDE Extension : Toggle Vrapper

?>

 

参考资料

  1.Hessian主页:http://hessian.caucho.com/Hessian

  2.PHP实现主页:http://hessianphp.sourceforge.net/

  3.HessianPHP开源项目主页:http://sourceforge.net/projects/hessianphp/

  4.RMI协议:http://baike.baidu.com/view/99017.htm

  5.维基百科:http://en.wikipedia.org/wiki/Hessian

  6.PHP源码:http://files.cnblogs.com/wubaiqing/Hessian.zip

 

www.phpzy.comtrue/phpyy/660.htmlTechArticleHessian通讯协议【附PHP源代码】 提示: 绿色加粗有下划线是文本的 链接 蓝色加粗强调 红色加粗重点 PHP源码: 点击下载 什么是 Hessian Hessian 是由 caucho 提供的一种开源的远程通讯协议。...

相关文章

相关频道:

PHP之友评论

今天推荐