在异步HTTP SERVER中使用服务

全局方法service_center()

使用service_center($service)获取服务地址,然后使用call()方法调用公开的服务方法

  1. $service = (yield service_center('User'));
  2. $user = (yield $service->call("User::getUser", ['id' => $userId]));

上面的代码会调用src/Service/User/Service/Impl/UserServiceImpl中的getUser方法

串行调用

  1. //设置2秒超时
  2. $service = (yield service_center("User"));
  3. $service->setTimeout(2);
  4. $users = (yield $service->call("User::getUsersCache", ['ids' => [1, 2]]));
  5. $users2 = (yield $service->call("User::getUsersCache", ['ids' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]));

并行调用(只能针对同一服务模块)

  1. $service = (yield service_center("User"));
  2. $service->setTimeout(2);
  3. $callId1 = $service->addCall("User::getUsersCache", ['ids' => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]);
  4. $callId2 = $service->addCall("User::getUser", ['id' => 1]);
  5. $res = (yield $service->multiCall());
  6. dump($res[$callId1]);
  7. dump($res[$callId2]);

全局方法service()

使用service($serverName)获取服务地址,然后使用call()方法调用公开的服务方法.

service()不会通过注册中心发现服务

  1. service("user")->setTimeout(2);
  2. $users = (yield service("user")->call("User\User::getUsersCache", ['ids' => [1, 2, 3, 4, 5]]));

串行调用

  1. service("user")->setTimeout(2);
  2. $users = (yield service("user")->call("User\User::getUsersCache", ['ids' => [1, 2, 3, 4]]));
  3. $users2 = (yield service("user")->call("User\User::getUsersCache", ['ids' => [1, 2, 3, 4]]));

并行调用

  1. service("user")->setTimeout(2);
  2. $callId1 = service("user")->addCall("User\User::getUsersCache", ['ids' => [1, 2, 3, 4, 5]]);
  3. $callId2 = service("user")->addCall("User\User::getUser", ['id' => 1]);
  4. $res = (yield service("user")->multiCall());
  5. dump($res[$callId1]);
  6. dump($res[$callId2]);