PHP code example of imiphp / imi-hprose
1. Go to this page and download the library: Download imiphp/imi-hprose 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-hprose example snippets
[
'components' => [
// 引入RPC组件以及本组件
'Rpc' => 'Imi\Rpc',
'Hprose' => 'Imi\Hprose',
],
]
[
// 主服务器配置
'mainServer' => [
'namespace' => 'XXX\MainServer', // 你的命名空间
'type' => 'Hprose', // 必须设为 Hprose
'port' => 8080,
],
]
[
// 子服务器(端口监听)配置
'subServers' => [
// 子服务器名
'XXX' => [
'namespace' => 'XXX\Hprose', // 你的命名空间
'type' => 'Hprose', // 必须设为 Hprose
'port' => 50001,
]
],
]
[
'pools' => [
'连接池名' => [
'sync' => [
'pool' => [
'class' => \Imi\Rpc\Client\Pool\RpcClientSyncPool::class,
'config' => [
// 连接池通用,查阅文档
],
],
'resource' => [
'clientClass' => \Imi\Hprose\Client\HproseSocketClient::class,
'uris' => 'tcp://127.0.0.1:50001', // 连接地址
// 其它配置
]
],
'async' => [
'pool' => [
'class' => \Imi\Rpc\Client\Pool\RpcClientCoroutinePool::class,
'config' => [
// 连接池通用,查阅文档
],
],
'resource' => [
'clientClass' => \Imi\Hprose\Client\HproseSocketClient::class,
'uris' => 'tcp://127.0.0.1:50001', // 连接地址
// 其它配置
]
],
],
],
'rpc' => [
'defaultPool' => '连接池名', // 默认连接池名
],
]
\Imi\Rpc\Client\Pool\RpcClientPool::getService('服务名')->方法名(参数);
class Test
{
/**
* @RpcClient()
*
* @var \Imi\Rpc\Client\IRpcClient
*/
protected $rpcClient;
/**
* @RpcService(serviceName="服务名")
*
* @var \Imi\Rpc\Client\IService
*/
protected $xxxRpc;
public function aaa()
{
// 方法一
$this->rpcClient->getService('服务名')->方法名(参数);
// 方法二
$this->xxxRpc->方法名(参数);
}
}