传输器
Transporter 负责把 Codec 生成的 TransportRequest 发送到网络,并返回 TransportResponse。
1. TransporterInterface
interface TransporterInterface extends Closable
{
public function send(TransportRequestInterface $request): TransportResponseInterface;
public function sendDirect(
mixed $connection,
TransportRequestInterface $request
): TransportResponseInterface;
public function close(): void;
}send():自行建立/管理连接sendDirect():使用连接池或调用方已建立的连接close():释放资源
2. 传输消息
| 消息 | 说明 |
|---|---|
HttpTransportRequest | HTTP method、URI、body、headers |
HttpTransportResponse | status code、body、headers |
TcpTransportRequest | TCP body |
TcpTransportResponse | TCP body |
3. Endpoint
$endpoint = new Endpoint(
protocol: 'http',
host: '127.0.0.1',
port: 8080,
connectTimeout: 5.0,
receiveTimeout: 30.0,
);常用工厂方法:
Endpoint::fromString('http://127.0.0.1:8080/rpc');
Endpoint::fromAddress('127.0.0.1:9000'); // tcpfromString() 会保留 URI 里的路径,(string) $endpoint 输出 http://127.0.0.1:8080/rpc; HTTP codec 用它作为请求 URL,因此 RPC 路径(如 /rpc)不会丢失。
未指定端口时会补全默认端口:
http→80https→443
4. GuzzleHttpTransporter
$client = new \GuzzleHttp\Client(['base_uri' => 'http://127.0.0.1:8080']);
$transporter = new GuzzleHttpTransporter($client);
$handler = new DefaultRpcRequestHandler(
$transporter,
new CodecRegistry([new HttpJsonRpcCodec()])
);特点:
- 只接受
HttpTransportRequest - 连接失败抛
ConnectionException - HTTP 请求异常抛
kuiper\rpc\exception\RequestException sendDirect()忽略传入的连接(Guzzle 自行管理连接池)
5. SocketTcpTransporter
$transporter = new SocketTcpTransporter(
Endpoint::fromAddress('127.0.0.1:9000')
);协议:
- 4 字节大端长度前缀
- 前缀后跟 JSON 内容
[4-byte length][json payload]sendDirect() 使用传入的 socket 资源;send() 每次新建连接并在 finally 中关闭。配合 PooledTransporter 可复用连接。
6. SwooleTcpTransporter
$transporter = new SwooleTcpTransporter(
Endpoint::fromAddress('127.0.0.1:9000')
);特点:
- 需要
ext-swoole - 使用
Swoole\Coroutine\Socket - 直接
send()/recv(),不添加长度前缀 - 服务端需要自行约定分帧方式(EOF、长度前缀或固定包长)
Swoole 5 / Swoole 6 兼容
Swoole\Coroutine\Socket 的构造签名在 Swoole 6 发生了变化,传输器内部按扩展版本自动适配:
| Swoole 版本 | 建连方式 |
|---|---|
| <= 5.x | new Socket(host, port, timeout),构造时即建立连接 |
| >= 6.0 | new Socket(AF_INET, SOCK_STREAM) + 显式 connect(host, port, timeout) |
Swoole 6 上若仍按旧签名构造,会抛 TypeError: Argument #1 ($domain) must be of type int, string given。 连接失败时 send() 会抛出 RuntimeException(消息形如 Swoole socket connect host:port failed: ...)。
SwooleCoroutineTcpTransporter 与 SwooleTcpTransporter 当前实现相同, 保留用于兼容不同命名/使用习惯。
7. PooledTransporter
$pool = $poolFactory->create('demo.DemoService', $connectionFactory);
$transporter = new PooledTransporter($pool, $delegateTransporter);行为:
send()从池中取连接,调用delegate->sendDirect()- 成功:
release()归还连接 - 异常:
discard()丢弃连接 - 可选
healthCheck:返回false时丢弃连接
适合:
- TCP 长连接
- Swoole 协程环境
- 需要控制连接数量的场景
GuzzleHttpTransporter::sendDirect()不使用连接参数,因此PooledTransporter对 HTTP Guzzle 传输没有实际复用效果。
8. PooledSession
PooledSession 是一个延迟发送的会话包装:
$session = new PooledSession($pool, $connection, $transporter, $request);
$response = $session->send();它会在 send() 后自动释放/丢弃连接,适合需要延迟执行的场景。
9. 自定义 Transporter
class MyTransporter implements TransporterInterface
{
public function send(TransportRequestInterface $request): TransportResponseInterface
{
// 发送请求
}
public function sendDirect(mixed $connection, TransportRequestInterface $request): TransportResponseInterface
{
// 使用已有连接发送
}
public function close(): void
{
// 释放资源
}
}然后构造 DefaultRpcRequestHandler:
$handler = new DefaultRpcRequestHandler(
new MyTransporter(),
new CodecRegistry([new HttpJsonRpcCodec()])
);10. 当前限制
RpcClientConfiguration固定使用GuzzleHttpTransporter;编解码器按serialization装配—— 默认tars时是TarsHttpJsonRpcCodec,显式配置jsonrpc时才是HttpJsonRpcCodec。- 使用 TCP / Swoole 传输器需要手动构造
RpcExecutorFactory或代理。 - TCP 服务端监听器不在 RPC 模块内,需要自行实现。