客户端

CLI独立测试(注意命名空间以及自动加载引入)

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xcg
  5. * Date: 2019/2/27
  6. * Time: 10:03
  7. */
  8. include_once dirname(__DIR__) . "/vendor/autoload.php";
  9. use EasySwoole\Rpc\Config;
  10. use EasySwoole\Rpc\Rpc;
  11. use EasySwoole\Rpc\Response;
  12. $config = new Config();
  13. //$config->setNodeManager(\EasySwoole\Rpc\NodeManager\TableManager::class);//设置节点管理器处理类,默认是EasySwoole\Rpc\NodeManager\FileManager
  14. $rpc = new Rpc($config);
  15. //获取所有服务节点列表
  16. $nodeList = $config->getNodeManager()->allServiceNodes();
  17. var_dump($nodeList);
  18. go(function () use ($rpc) {
  19. $client = $rpc->client();
  20. //调用服务
  21. $serviceClient = $client->selectService('ser1');
  22. //创建执行任务
  23. $serviceClient->createTask()->setAction('call1')->setArg(['arg' => 1])
  24. ->setOnSuccess(function (Response $response) {
  25. echo ($response->getMessage()).PHP_EOL;
  26. })->setOnFail(function () {
  27. echo ("请求失败1!\n");
  28. });
  29. //创建执行任务
  30. $serviceClient->createTask()->setAction('call3')
  31. ->setOnSuccess(function (Response $response) {
  32. echo ($response->getMessage()).PHP_EOL;
  33. })->setOnFail(function () {
  34. echo ("请求失败2!\n");
  35. });
  36. //创建执行任务
  37. $serviceClient2 = $client->selectService('ser2');
  38. $serviceClient2->createTask()->setAction('call1')
  39. ->setOnSuccess(function (Response $response) {
  40. echo ($response->getMessage()).PHP_EOL;
  41. })->setOnFail(function () {
  42. echo ("请求失败3!\n");
  43. });
  44. $client->exec();//开始执行
  45. });