Skip to content

Commit 199de1c

Browse files
committed
Added README.md
1 parent 2dd19cf commit 199de1c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,72 @@
77
```
88
composer require simple-swoole/rpc-multiplex
99
```
10+
11+
## 配置 Server 端
12+
13+
```php
14+
<?php
15+
16+
declare(strict_types=1);
17+
18+
return [
19+
'mode' => SWOOLE_BASE,
20+
'rpc' => [
21+
'class_name' => Simps\RpcMultiplex\TcpServer::class,
22+
'ip' => '0.0.0.0',
23+
'port' => 9503,
24+
'sock_type' => SWOOLE_SOCK_TCP,
25+
'settings' => [
26+
'worker_num' => 1,
27+
// 以下参数除 package_max_length 外,其他不允许修改
28+
'open_length_check' => true,
29+
'package_length_type' => 'N',
30+
'package_length_offset' => 0,
31+
'package_body_offset' => 4,
32+
'package_max_length' => 1024 * 1024 * 2,
33+
],
34+
],
35+
];
36+
37+
```
38+
39+
### 编写对应的服务端代码
40+
41+
```php
42+
<?php
43+
44+
namespace App\Service;
45+
46+
class RpcService
47+
{
48+
public function hello(string $name): string
49+
{
50+
return 'Hello ' . $name;
51+
}
52+
}
53+
```
54+
55+
## 直接使用 Client::instance 调用服务代码
56+
57+
```php
58+
<?php
59+
60+
use Simps\RpcMultiplex\Client;
61+
62+
$data = Client::getInstance('127.0.0.1', 9503)->call('App\Service\RpcService', 'hello', 'World');
63+
64+
var_dump($data->getResult()); // Hello World
65+
```
66+
67+
`getInstance` 不会根据参数重新初始化,故以下代码获得的 `instance` 为同一个实例。
68+
69+
```php
70+
<?php
71+
72+
use Simps\RpcMultiplex\Client;
73+
74+
$client1 = Client::getInstance('127.0.0.1', 9503);
75+
$client2 = Client::getInstance('127.0.0.1', 9504);
76+
```
77+
78+
所以如果存在多个 RpcServer 时,需要使用继承,创建对应的单例。

0 commit comments

Comments
 (0)