Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time

RPC

RPC鍗宠繙绋嬭繃绋嬭皟鐢紝鏄竴绉嶅父鐢ㄧ殑鍒嗗竷寮忕郴缁熼棿璁块棶鎺ュ彛鐨勬柟寮忋侾hpBoot 鎻愪緵寮哄ぇ鍙堢畝鍗曟槗鐢ㄧ殑 RPC 鏀寔锛屽彲浠ヨ浣犲儚浣跨敤鏈湴鎺ュ彛涓鏍凤紝鏂逛究鐨勪娇鐢ㄨ繙绋嬫帴鍙c

1. 绀轰緥

涓嬮潰灏嗛氳繃瀹炵幇涓涓鍗曟湇鍔$殑绀轰緥锛屾紨绀 PhpBoot RPC 鐨勪娇鐢ㄣ

1.1. 瀹氫箟鎺ュ彛

涓轰繚鎸佺ず渚嬪敖閲忕畝鍗曪紝杩欓噷鎴戜滑鍙疄鐜扳滃垱寤鸿鍗曗濊繖涓涓帴鍙c

/**
 * @path /orders
 */
interface OrderServiceInterface
{
    /**
     * @route POST /
     * @param ProductInfo $product 鍟嗗搧蹇収
     * @return string 杩斿洖璁㈠崟鍙
     */
    public function createOrder(ProductInfo $product);
}

1.2. 瀹炵幇鎺ュ彛

鎺ュ彛瀹氫箟濂戒互鍚庯紝 鎴戜滑闇瑕佸湪鏈嶅姟绔紝瀹炵幇璇ユ湇鍔℃帴鍙o紝浠ヤ究鍙互瀵瑰鎻愪緵璁块棶銆

/**
 * @path /orders
 */
class OrderService implements OrderServiceInterface 
{
    /**
     * @route POST /
     * @param ProductInfo $product 鍟嗗搧蹇収
     * @return string 杩斿洖璁㈠崟鍙
     */
    public function createOrder(ProductInfo $product)
    {
        // create the order
        return $orderId;
    }
}

1.3. 杩滅▼璋冪敤鎺ュ彛

鍦ㄥ鎴风锛屽彲浠ラ氳繃涓嬮潰鏂规硶璋冪敤杩滅▼鐨勬帴鍙c

$orderService =  $app->make(
    RpcProxy::class, 
    [
        'interface'=>OrderServiceInterface::class, 
        'prefix'=>'http://10.x.x.1/'
    ]
);
/**@var OrderServiceInterface $orderService*/

$orderId = $orderService->createOrder($product);

鍙︿竴绉嶆帹鑽愮殑鏂规硶鏄氳繃渚濊禆娉ㄥ叆鍒涘缓浠g悊绫汇傚

//閰嶇疆渚濊禆

return [
    OrderServiceInterface::class 
        => \DI\objet(RpcProxy::class)
            ->constructorParameter('interface', OrderServiceInterface::class)
            ->constructorParameter('prefix', 'http://10.x.x.1/')
    
]
// 娉ㄥ叆渚濊禆

class AnotherService
{
    ...
    
    /**
     * @inject 
     * @var OrderServiceInterface
     */
    private $orderService;
    
    public function doSomething()
    {
        $orderId = $this->orderService->createOrder($product)
    }
}

2. 娉ㄦ剰

鐢变簬 RpcProxy 榛樿閫氳繃 __call 瀹炵幇杩滅▼鏂规硶鐨勮皟鐢紝鎵浠ユ棤娉曚紶閫掑紩鐢ㄥ弬鏁般傚綋鎺ュ彛鍙傛暟涓瓨鍦ㄥ紩鐢ㄥ弬鏁版椂锛屽簲璇ラ拡瀵规帴鍙e疄鐜颁竴涓猂pcProxy鐨勫瓙绫伙紝骞堕噸鍐欏寘鍚紩鐢ㄥ弬鏁扮殑鏂规硶銆備互涓嬫槸绀轰緥

// 杩欐槸涓吀鍨嬬殑渚嬪瓙锛屾帴鍙g殑鏂规硶涓湁寮曠敤绫诲瀷鍙傛暟
/**
 * @path /orders
 */
interface OrderServiceInterface
{
    /**
     * @route GET /
     * @param int $offset
     * @param int $limit
     * @param int $total 姝や负寮曠敤绫诲瀷鍙傛暟锛 鐢ㄤ簬杩斿洖鏌ヨ鐨勬绘潯鏁
     * @return Order[] 杩斿洖璁㈠崟鍒楄〃
     */
    public function getOrders($offset, $limit, &$total);
}
// 杩欐槸涓吀鍨嬬殑渚嬪瓙锛屾帴鍙g殑鏂规硶涓湁寮曠敤绫诲瀷鍙傛暟
/**
 * @path /orders
 */
class OrderServiceProxy extends RpcProxy implements OrderServiceInterface 
//濡傛灉涓嶆兂瀹炵幇OrderServiceInterface鐨勬墍鏈夋柟娉曪紝涔熷彲浠ヤ笉缁ф壙OrderServiceInterface
{
    /**
     * @route GET /
     * @param int $offset
     * @param int $limit
     * @param int $total 姝や负寮曠敤绫诲瀷鍙傛暟锛 鐢ㄤ簬杩斿洖鏌ヨ鐨勬绘潯鏁
     * @return Order[] 杩斿洖璁㈠崟鍒楄〃
     */
    public function getOrders($offset, $limit, &$total)
    {
        return $this->__call(__FUNCTION__, [$offset, $limit, &$total]);
    }
}
//鎺ヤ笅鏉ュ彲浠ラ氳繃OrderServiceProxy 璁块棶杩滅▼鎺ュ彛浜

$orderService =  $app->make(
    OrderServiceProxy::class, 
    [
        'interface'=>OrderServiceInterface::class, 
        'prefix'=>'http://10.x.x.1/'
    ]
);

$orderService->getOrders...

3. 骞跺彂璁块棶

鍦ㄤ娇鐢ㄨ繙绋嬫湇鍔℃椂锛屾湁鏃跺彲鑳介渶瑕佸悓鏃惰闂涓繙绋嬫帴鍙c傚鏋滆兘骞惰鎵ц锛屽湪涓浜涙儏鍐典笅鍙互澶уぇ鍑忓皯鎺ュ彛鎵ц鏃堕棿銆侾hpBoot RPC 鎻愪緵浜嗗苟鍙戞墽琛岀殑鍔熻兘銆備娇鐢ㄦ柟娉曞涓嬶細

$orderService = $app->make ...
$bookService = $app->make ...


$rpcRes = MultiRpc::run([
    function()use(orderService){
        return orderService->getOrders(...);
    },
    function(){
        return bookService->getBooks(...);

    }
])

$res = []
foreach($rpcRes as $i){
    list($success, $error) = $i
    if($error){
        //鎵ц澶辫触鐨勫師鍥
    }else{
        //鎵ц鎴愬姛锛 澶勭悊$success
    }
}
return $res

娉ㄦ剰锛孧ultiRpc 鍐呴儴鏄皢闇骞跺彂鎵ц鐨勬搷浣滐紝璋冪敤杞崲涓洪掑綊璋冪敤锛屽苟鍦ㄩ掑綊鐨勬渶鍚庯紝绛夊緟鎵鏈夊紓姝ユ搷浣滃畬鎴愩 鎵浠ュ疄闄呬笂锛岀湡姝e苟鍙戞墽琛岀殑鍙槸缃戠粶璇锋眰锛屾墍鏈夌綉缁滆姹傜粨鏉熷悗锛屽悗缁唬鐮佹墽琛岃繕鏄覆琛岀殑