介绍 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 ; }