PHP code example of imiphp / imi-grpc

1. Go to this page and download the library: Download imiphp/imi-grpc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imiphp / imi-grpc example snippets


[
    'ignorePaths' => [
        // 添加RPC忽略目录
        \dirname(__DIR__) . \DIRECTORY_SEPARATOR . 'grpc',
    ],
]

[
    // 主服务器配置
    'mainServer'    =>  [
        'namespace' =>    'ImiApp\GrpcServer',
        'type'      =>    'GrpcServer',
        'host'      =>    '127.0.0.1',
        'port'      =>    8080,
    ],
]

[
    // 子服务器(端口监听)配置
    'subServers'    =>  [
        // 子服务器名
        'XXX'   =>  [
            'namespace' =>    'ImiApp\GrpcServer',
            'type'      =>    'GrpcServer',
            'host'      =>    '127.0.0.1',
            'port'      =>    8080,
        ]
    ],
]

/**
 * @Controller("/grpc.AuthService/")
 */
class AuthServiceController extends HttpController implements AuthServiceInterface
{
    /**
     * Method <code>login</code>
     *
     * @Action
     *
     * @param \Grpc\LoginRequest $request
     * @return \Grpc\LoginResponse
     */
    public function login(\Grpc\LoginRequest $request)
    {
        $response = new LoginResponse;
        $success = '12345678901' === $request->getPhone() && '123456' === $request->getPassword();
        $response->setSuccess($success);
        $response->setError($success ? '' : '登录失败');
        return $response;
    }

}

[
    // 连接池配置
    'pools'    =>    [
        'grpc'  =>  [
            'async'    =>    [
                'pool'    =>    [
                    'class'        =>    \Imi\Rpc\Client\Pool\RpcClientCoroutinePool::class,
                    'config'    =>    [
                        // 根据实际情况设置
                        'maxResources'  =>    100,
                        'minResources'  =>    1,
                    ],
                ],
                'resource'    =>    [
                    // 这里需要和你的服务端路由一致
                    'url'           =>  'http://127.0.0.1:8080/{package}.{service}/{name}',
                    // 'url'           =>  'http://127.0.0.1:8080/{package}.{service}/{name|ucfirst}', // 参数支持设定函数处理,比如这个将方法名首字母大写,兼容其它部分语言
                    'clientClass'   =>  \Imi\Grpc\Client\GrpcClient::class,
                    'method'        =>  'POST', // 指定请求方式,默认 GET
                    'timeout'       =>  30, // 超时时间,单位:秒
                ]
            ],
        ],
    ],
    'rpc'   =>  [
        'defaultPool'   =>  'grpc',
    ],
]

// $service = \Imi\Rpc\Client\Pool\RpcClientPool::getInstance('连接池名')->getService('服务名', '生成出来的服务接口类名');
$service = \Imi\Rpc\Client\Pool\RpcClientPool::getInstance()->getService('AuthService', \Grpc\AuthServiceInterface::class);
$request = new \Grpc\LoginRequest;
$request->setPhone('');
$service->login($request);

use Imi\Rpc\Annotation\RpcClient;
use Imi\Grpc\Client\Annotation\GrpcService;

class Test
{
    /**
     * @RpcClient()
     *
     * @var \Imi\Rpc\Client\IRpcClient
     */
    protected $rpcClient;

    /**
     * @GrpcService(serviceName="grpc.AuthService", interface=\Grpc\AuthServiceInterface::class)
     *
     * @var \Grpc\AuthServiceInterface
     */
    protected $authService;

    public function aaa()
    {
        $request = new \Grpc\LoginRequest;
        $request->setPhone('');

        // 方法一
        $this->rpcClient->getService('服务名', '生成出来的服务接口类名')->方法名($request);

        // 方法二
        $this->xxxRpc->方法名($request);
    }
}