博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Yar - Yet Another RPC framework for PHP
阅读量:6704 次
发布时间:2019-06-25

本文共 3396 字,大约阅读时间需要 11 分钟。

Introduction

Yar is a RPC framework which aims to provide a simple and easy way to do communication between PHP applications

It has the ability to concurrently call multiple remote services.

Features

  • Fast, Easy, Simple
  • Concurrent RPC calls
  • Multiple data packager supported (php, json, msgpack built-in)
  • Multiple transfer protocols supported (http implemented, tcp/unix will be supported later)
  • Detailed debug informations

Install

Install Yar

Yar is an PECL extension, thus you can simply install it by:

pecl install yar

Compile Yar in Linux

$/path/to/phpize$./configure --with-php-config=/path/to/php-config/$make && make install

Install Yar with msgpack

first you should install msgpack-ext

pecl install msgpack

or , you can get the github source here: 

then:

$phpize$configure --with-php-config=/path/to/php-config/ --enable-msgpack$make && make install

Runtime Configure

  • yar.timeout //default 5000 (ms)
  • yar.connect_timeout //default 1000 (ms)
  • yar.packager //default "php", when built with --enable-msgpack then default "msgpack", it should be one of "php", "json", "msgpack"
  • yar.debug //default Off
  • yar.expose_info // default On, whether output the API info for GET requests
  • yar.content_type // default "application/octet-stream"
  • yar.allow_persistent // default Off

NOTE yar.connect_time is a value in milliseconds, and was measured in seconds in 1.2.1 and before.

Constants

  • YAR_VERSION
  • YAR_OPT_PACKAGER
  • YAR_OPT_PERSISTENT
  • YAR_OPT_TIMEOUT
  • YAR_OPT_CONNECT_TIMEOUT

Server

It's very easy to setup a Yar HTTP RPC Server

handle(); ?>

Usual RPC calls will be issued as HTTP POST requests. If a HTTP GET request is issued to the uri, the service information (commented section above) will be printed on the page:

Client

It's very easy for a PHP client to call remote RPC:

Synchronous call

SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000); /* call remote service */ $result = $client->some_method("parameter"); ?>

Concurrent call

"json")); //this server accept json packager Yar_Concurrent_Client::call("http://host/api/", "some_method", array("parameters"), "callback", "error_callback", array(YAR_OPT_TIMEOUT=>1)); //custom timeout Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests, //the error_callback is optional ?>

Protocols

Yar Header

Since Yar will support multi transfer protocols, so there is a Header struct, I call it Yar Header

#ifdef PHP_WIN32#pragma pack(push)#pragma pack(1)#endiftypedef struct _yar_header { unsigned int id; // transaction id unsigned short version; // protocl version unsigned int magic_num; // default is: 0x80DFEC60 unsigned int reserved; unsigned char provider[32]; // reqeust from who unsigned char token[32]; // request token, used for authentication unsigned int body_len; // request body len } #ifndef PHP_WIN32 __attribute__ ((packed)) #endif yar_header_t; #ifdef PHP_WIN32 #pragma pack(pop) #endif

Packager Header

Since Yar also supports multi packager protocl, so there is a char[8] at the begining of body, to identicate which packager the body is packaged by.

Request

When a Client request a remote server, it will send a struct (in PHP):

'', //transaction id "m" => '', //the method which being called "p" => array(), //parameters )

Server

When a server response a result, it will send a struct (in PHP):

'', "s" => '', //status "r" => '', //return value "o" => '', //output "e" => '', //error or exception )

转载地址:http://qnflo.baihongyu.com/

你可能感兴趣的文章
AT发送短信(转)
查看>>
DataTable.Compute方法使用实例
查看>>
VB操作ISNULL
查看>>
PIC452外部中断进不去的原因?
查看>>
2.9 Fibonacci数列
查看>>
POJ 1721 CARDS(置换)
查看>>
Hypertable 0.9.6.4 发布,分布式数据库
查看>>
Spring源代码解析(六):Spring声明式事务处理
查看>>
精心挑选12款优秀的 JavaScript 日历和时间选择插件
查看>>
iphone sdk
查看>>
如何获取jqGrid中选择的行的数据
查看>>
Android 获取自带浏览器上网记录
查看>>
c++ 静态持续变量
查看>>
MFC超链接静态类的使用
查看>>
我所遭遇过的游戏中间件---SpeedTree
查看>>
csharp .net vb 复制图像
查看>>
android:versionCode和android:versionName 用途(转)
查看>>
android之SeekBar控件用法
查看>>
Fragment Transactions & Activity State Loss
查看>>
jQuery插件 -- 表单验证插件jquery.validate.js
查看>>