6 Star 7 Fork 2

rushmore / zbus-php

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT
            /\\\                                                            /\\\                               
            \/\\\                                                           \/\\\                              
             \/\\\                                               /\\\\\\\\\  \/\\\           /\\\\\\\\\        
 /\\\\\\\\\\\ \/\\\         /\\\    /\\\  /\\\\\\\\\\            /\\\/////\\\ \/\\\          /\\\/////\\\      
 \///////\\\/  \/\\\\\\\\\  \/\\\   \/\\\ \/\\\//////            \/\\\\\\\\\\  \/\\\\\\\\\\  \/\\\\\\\\\\      
       /\\\/    \/\\\////\\\ \/\\\   \/\\\ \/\\\\\\\\\\           \/\\\//////   \/\\\/////\\\ \/\\\//////      
      /\\\/      \/\\\  \/\\\ \/\\\   \/\\\ \////////\\\           \/\\\         \/\\\   \/\\\ \/\\\           
     /\\\\\\\\\\\ \/\\\\\\\\\  \//\\\\\\\\\   /\\\\\\\\\\           \/\\\         \/\\\   \/\\\ \/\\\          
     \///////////  \/////////    \/////////   \//////////            \///          \///    \///  \///          

zbus strives to make Message Queue and Remote Procedure Call fast, light-weighted and easy to build your own service-oriented architecture for many different platforms. Simply put, zbus = mq + rpc.

zbus carefully designed on its protocol and components to embrace KISS(Keep It Simple and Stupid) principle, but in all it delivers power and elasticity.

  • Working as MQ, compare it to RabbitMQ, ActiveMQ.
  • Working as RPC, compare it to many more.

zbus-php-client

NO threading in PHP client, HA not ready yet! Pull request if you are interested.

  • Works for both PHP5(crack on Throwable?) and PHP7
  • zbus.php is the only source file required

API Demo

Only demos the gist of API, more configurable usage calls for your further interest.

Produce message

function biz($broker){ 
	$producer = new Producer($broker);
	$msg = new Message();
	$msg->url = '/abc';
	$msg->topic = 'MyTopic';
	$msg->tag = 'Stock.A.中文';
	$msg->body = 'From PHP sync 中文';
	
	$res = $producer->publish($msg);
	echo $res . PHP_EOL;
}


$loop = new EventLoop(); 
$broker = new Broker($loop, "localhost:15555;localhost:15556", true); // enable sync mode

$broker->on('ready', function() use($loop, $broker){  
	//run after ready
	try {  biz($broker); } catch (Exception $e){ echo $e->getMessage() . PHP_EOL; }
	
	$broker->close();  
	$loop->stop(); //stop anyway
}); 

$loop->runOnce();

Consume message

$messageHandler = function($msg, $client){ //where you should focus on 
    echo $msg->tag . PHP_EOL;
	echo $msg . PHP_EOL;
};


Logger::$Level = Logger::DEBUG; //change to info to disable verbose message
$loop = new EventLoop(); 
$broker = new Broker($loop, "localhost:15555;localhost:15556"); //HA, test it?!
 

$ctrl = new ControlHeaders();
$ctrl->topic = "MyTopic"; 
$ctrl->topic_mask = Protocol::MASK_DISK;
//$ctrl->group_name_auto = true;
//$ctrl->consume_group = "MyTopic_Group12";
//$ctrl->group_mask = Protocol::MASK_DISK;
//$ctrl->group_filter = "abc.*";  


$c = new Consumer($broker, $ctrl);  
$c->connectionCount = 1;
$c->messageHandler = $messageHandler; 

$c->start();  
$loop->run();

RPC client

require_once '../../zbus.php';

$b = new ClientBootstrap();

$b->ha(false)    //直连模式
->async(false)
->serviceName("MyRpc")
->serviceAddress("localhost:15555");//当HA使能时,地址解释为Tracker/NameServer

$b->run(function ($rpc, $boostrap) {
    
    $rpc->module = "InterfaceExample";
    
    // 1)弱类型调用
    $req = new Request("plus", array( 1, 2 ));  //构造出请求,方法+参数+【模块】
    $res = $rpc->invoke($req);
    echo $res . PHP_EOL;
    
    // 2)强类型调用
    $res = $rpc->plus(1, 2);
    echo $res . PHP_EOL;
    
    $res = $rpc->testEncoding();
    echo $res . PHP_EOL;
    
    $res = $rpc->noReturn();
    echo $res . PHP_EOL;
    
    try {
        $rpc->throwException();
    } catch (Exception $e){
        error_log($e);
    }
    
    
    $boostrap->close(); //
});

RPC service

require_once '../../zbus.php';

//函数返回类型除了Message之外都按JSON格式处理
class MyService {   
	public function echo($msg){
		return $msg . ", From PHP";
	}
	
	public function plus($a, $b) {
	    return $a + $b;
	}  
	
	public function testEncoding() {
		return "中文";
	}
	
	public function noReturn() {
		
	}
	
	public function getUser(){
	    return array("name"=>"Hong", "age"=>"18");
	}
	
	public function getBin(){ 
	    $bytes = array();
	    for($i = 0; $i < 10; $i++){
	        array_push($bytes, 0);
	    } 
	    
	    $string = implode(array_map("chr", $bytes));
	    return base64_encode($string);
	}
	
	public function throwException(){
	    throw new Exception("exception throw!");
	}  
	
	//页面跳转
	public function redirect(){
	    $msg = new Message();
	    $msg->status = 302;
	    $msg->headers['location'] = "/"; 
	    return $msg;
	}
}  



Logger::$Level = Logger::DEBUG;
  
$b = new ServiceBootstrap(); 
$b->addModule("InterfaceExample", new MyService()); //模块标识,URL中体现 

$b->serviceName('MyRpc')
  ->serviceAddress('localhost:15555;localhost:15556')
  ->connectionCount(1)
  ->enableDoc(true)
  ->start();
The MIT License (MIT) Copyright (c) 2017 rushmore Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

zbus php client 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/rushmore/zbus-php.git
git@gitee.com:rushmore/zbus-php.git
rushmore
zbus-php
zbus-php
master

搜索帮助

14c37bed 8189591 565d56ea 8189591