title: Rpc client meta:

  • name: description content: RPC client implementation in EasySwoole
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|Rpc client|swoole RPC

Client

CLI independent testing (note the introduction of namespaces and autoloading)

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xcg
  5. * Date: 2019/5/30
  6. * Time: 9:19
  7. */
  8. require_once 'vendor/autoload.php';
  9. use EasySwoole\Rpc\Config;
  10. use EasySwoole\Rpc\Rpc;
  11. use EasySwoole\Rpc\NodeManager\RedisManager;
  12. use EasySwoole\Rpc\Response;
  13. //Cli simulation, cross process, renew new rpc object
  14. $config = new Config();
  15. $nodeManager = new RedisManager('127.0.0.1');
  16. $config->setNodeManager($nodeManager);
  17. $rpc = new Rpc($config);
  18. go(function () use ($rpc) {
  19. $client = $rpc->client();
  20. $client->addCall('UserService', 'register', ['arg1', 'arg2'])
  21. ->setOnFail(function (Response $response) {
  22. print_r($response->toArray());
  23. })
  24. ->setOnSuccess(function (Response $response) {
  25. print_r($response->toArray());
  26. });
  27. $client->exec();
  28. });

Under the easyswoole framework

  1. <?php
  2. $client=Rpc::getInstance()->client();
  3. go(function () use ($client) {
  4. $client->addCall('UserService', 'register', ['arg1', 'arg2'])
  5. ->setOnFail(function (Response $response) {
  6. print_r($response->toArray());
  7. })
  8. ->setOnSuccess(function (Response $response) {
  9. print_r($response->toArray());
  10. });
  11. $client->exec();
  12. });