webman自定义RPC进程,tcp协议做RPC服务

本文环境 PHP7.3,webman 不懂的可以评论或联系我邮箱:owen@owenzhang.com 原文链接:https://www.workerman.net/q/6057

1、新建文件 process/Rpc.php 编写rpc进程

send(call_user_func_array([$instances[$class], $method], $args));
    }
}

2、打开 config/process.php 增加配置启动rpc进程

return [
    // ... 这里省略了其它配置...

    'rpc'  => [
        'handler' => process\Rpc::class,
        'listen'  => 'text://0.0.0.0:8888', // 这里用了text协议,也可以用frame或其它协议
        'count'   => 8, // 可以设置多进程
    ]
];

3、新建 service/User.php 服务 (目录不存在自行创建)

 $uid,
            'name' => 'tom'
        ]);
    }
}

4、重启webman

php start.php restart

5、客户端调用

 'User',
    'method'  => 'get',
    'args'    => [100], // 100 是 $uid
];
fwrite($client, json_encode($request)."\n"); // text协议末尾有个换行符"\n"
$result = fgets($client, 10240000);
$result = json_decode($result, true);
var_export($result);

以上客户端代码自己可以封装成一个类

最终结果打印

array (
  'uid' => 100,
  'name' => 'tom',
)

Buy me a cup of coffee 🙂

觉得对你有帮助,就给我打赏吧,谢谢!

微信赞赏码链接,点击跳转:

webman自定义RPC进程,tcp协议做RPC服务插图

Tags:
Next Article