Swoft2.x 小白學習筆記 (四) --- RPC


介紹 swoft 中 RPC使用:搭建訪問服務端和客戶端

   

RPC服務端:

一、配置,在文件 /app/bean.php中添加

return [
    'rpcServer'  => [
        'class' => ServiceServer::class,
        'port' => 18308,
    ],
]

Http server 啟動中集成 RPC 服務:
return [
    'httpServer' => [
        'class'    => HttpServer::class,
        'port'     => 18306,
        'listener' => [
            'rpc' => bean('rpcServer')
        ],

        // ...
    ],
]

 

二、使用

 

 

   1、定義接口,服務提供方定義好接口格式,存放到公共的lib庫里面,服務調用方,加載lib庫,就能使用接口服務,接口定義和普通接口完全一致。

 在/app/Rpc/Lib/ 文件夾下添加文件DemoInterface.php:

<?php

namespace App\Rpc\Lib;

/**
 * Interface DemoInterface
 */
interface DemoInterface{
    /**
     * @return array
     * @param int $id
     */
    public function getLists(int $id): array ;

    /**
     * @return string
     */
    public function getBig():string ;
}
View Code

  2、接口實現,在文件夾 /app/Rpc/Service/ 下添加文件DemoService.php

<?php
namespace App\Rpc\Service;

use App\Rpc\Lib\DemoInterface;
use Swoft\Rpc\Server\Annotation\Mapping\Service;

/**
 * Class DemoService
 *
 * @Service(version="1.0") //定義版本
 *
 */
class DemoService implements DemoInterface{
    /**
     * @param int $id
     * @return array
     */public function getLists(int $id): array
        {
            // TODO: Implement getLists() method.

            return ["id" => $id];
        }

    /**
     * @return string
     */
    public function getBig(): string
    {
        // TODO: Implement getBig() method.

        return "ddddddd";
    }
}
View Code

     定義版本2,在文件夾 /app/Rpc/Service/ 下添加文件DemoServiceV2.php

<?php
namespace App\Rpc\Service;

use App\Rpc\Lib\DemoInterface;
use Swoft\Rpc\Server\Annotation\Mapping\Service;

/**
 * Class DemoService
 *
 * @Service(version="1.2") //定義版本1.2
 *
 */
class DemoServiceV2 implements DemoInterface{

    /**
     * @param int $id
     * @return array
     */public function getLists(int $id): array
    {
        // TODO: Implement getLists() method.

        return ["id" => $id, "ver" => "1.2"];
    }

    /**
     * @return string
     */
    public function getBig(): string
    {
        // TODO: Implement getBig() method.

        return "dddd_V2";
    }
}
View Code
不同的地方在  @Service(version="1.2") ,定義不同版本

  3、啟動訪問:

//單獨啟動rpc
php bin/swoft rpc:start

//啟動http、伴隨啟動RPC
php bin/swoft http:start

 

 

 

 

RPC客戶端:服務調用方法,通過使用服務提供方法,提供的lib接口,調用接口實現服務,不需要了解實現細節

  一:配置,客戶端也需要安裝swoft,在其/app/bean.php 中添加

 //定義RPC客戶端連接,TCP方式,端口為RPC服務器端口
 return [
  
'user' => [ 'class' => ServiceClient::class, 'host' => '127.0.0.1', //服務端IP 'port' => 18307, //服務端RPC的端口 'setting' => [ 'timeout' => 0.5, 'connect_timeout' => 1.0, 'write_timeout' => 10.0, 'read_timeout' => 0.5 ], 'packet' => bean('rpcClientPacket') ], 'user.pool' => [ 'class' => ServicePool::class, 'client' => bean('user'), ],
]

  二:使用:

    (1) : 拷貝服務端的 /app/Rpc/Lib/ 文件夾到客戶端的 /app/Rpc/Lib/

    (2) :在客戶端的 /app/Http/Controller/ 文件夾下添加控制器 RpcClientController.php

<?php
namespace App\Http\Controller;

use App\Rpc\Lib\DemoInterface;
use Exception;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Rpc\Client\Annotation\Mapping\Reference;

/**
 * Class RpcClientController
 *
 * @Controller(prefix="/rpcApi") //定義路由
 *
 */
class RpcClientController{
    /**
     * @Reference(pool="user.pool") //pool 指定使用那個服務的連接池(使用那個服務),version 指定服務的版本 *
     * @var DemoInterface
     */
    private $userSer;

    /**
     * @Reference(pool="user.pool", version="1.2") //pool 指定使用那個服務的連接池(使用那個服務),version 指定服務的版本 *
     * @var DemoInterface
     */
    private $userSerV2;
    
    /**
     * @RequestMapping("rpcV1")   //訪問路由 /rpcApi/recV1/
     */
    public function getRpcApiV1(): array {
      $result = $this->userSer->getLists(21); //調用1.0版本接口 $resultV2 = $this->userSerV2->getLists(33); //調用1.2版本接口 return [$result,$resultV2];
    }

    /**
     * @return array
     * @RequestMapping("rpcV2")
     */
    public function getBigString(): array {
        $bigV1 = $this->userSer->getBig();
        $bigV2 = $this->userSerV2->getBig();

        return [strlen($bigV1),strlen($bigV2)];
    }
}

 

使用非swoft客戶端框架訪問RPC ,參考官方文檔 : https://www.swoft.org/docs/2.x/zh-CN/rpc-client/usage.html

 

參考文檔:https://www.swoft.org/docs/2.x/zh-CN/rpc-server/index.html

       https://www.swoft.org/docs/2.x/zh-CN/rpc-client/index.html

     與Swoft RPC Server通信的Client擴展:  https://www.ctolib.com/article/compares/91157

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM