title: Rpc server meta:

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

Server

Independent code

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xcg
  5. * Date: 2019/5/30
  6. * Time: 9:17
  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\Test\UserService;
  13. use EasySwoole\Rpc\Test\OrderService;
  14. use EasySwoole\Rpc\Test\NodeService;
  15. $config = new Config();
  16. $config->setServerIp('127.0.0.1');//Register to provide ip service ip
  17. $config->setNodeManager(new RedisManager('127.0.0.1'));//Register Node Manager
  18. $config->getBroadcastConfig()->setSecretKey('lucky'); //Set key
  19. $config->getBroadcastConfig()->setEnableBroadcast(false); //Whether to enable broadcast (ps: use redis node to close)
  20. $config->getBroadcastConfig()->setEnableListen(false); //Whether to enable listening broadcast (ps: use redis node to close)
  21. $rpc = new Rpc($config);
  22. $rpc->add(new UserService()); //Registration service
  23. $rpc->add(new OrderService());
  24. $rpc->add(new NodeService());
  25. $list = $rpc->generateProcess();
  26. foreach ($list['worker'] as $p) {//Start the rpc process
  27. $p->getProcess()->start();
  28. }
  29. foreach ($list['tickWorker'] as $p) { //Start the timing process (ps: timed broadcast, monitor broadcast)
  30. $p->getProcess()->start();
  31. }
  32. while ($ret = \Swoole\Process::wait()) {//Recycling child process
  33. echo "PID={$ret['pid']}\n";
  34. }

Used under the easyswoole framework

  1. <?php
  2. use EasySwoole\Rpc\Config;
  3. use EasySwoole\Rpc\Rpc;
  4. use EasySwoole\Rpc\NodeManager\RedisManager;
  5. use EasySwoole\Rpc\Test\UserService;
  6. use EasySwoole\Rpc\Test\OrderService;
  7. use EasySwoole\Rpc\Test\NodeService;
  8. use EasySwoole\EasySwoole\ServerManager;
  9. #Register in the global mainServerCreate event of EasySwooleEvent.php
  10. $config = new Config();
  11. $config->setServerIp('127.0.0.1');//Register to provide ip service ip
  12. $config->setNodeManager(new RedisManager('127.0.0.1'));//Register Node Manager
  13. $config->getBroadcastConfig()->setSecretKey('lucky'); //Set key
  14. $rpc = Rpc::getInstance($config);;
  15. $rpc->add(new UserService()); //Registration service
  16. $rpc->add(new OrderService());
  17. $rpc->add(new NodeService());
  18. $rpc->attachToServer(ServerManager::getInstance()->getSwooleServer());