RPC & SOA

EasySwoole 同样可以做串行、并行化的SOA服务调用,底层基于SWOOLE_TCP实现,支持自定义消息加解密,为方便多种客户端(不同语言)调用,服务交互采用json格式传递,开发者可以快速以其他语言实现。

场景描述

例如,某个应用中,A为前端承载机器,B与C分别部署着不同的服务。一个用户请求进来,A同时向BC发起请求获取,并汇集两个结果返回给用户。

在EasySwoole中,RPC服务以服务名=>多个行为名的形式存在。

创建服务命令注册类

  1. namespace App\RPC;
  2. use Core\Component\RPC\AbstractInterface\AbstractActionRegister;
  3. use Core\Component\RPC\Common\ActionList;
  4. use Core\Component\RPC\Common\Package;
  5. class Goods extends AbstractActionRegister
  6. {
  7. function register(ActionList $actionList)
  8. {
  9. // TODO: Implement register() method.
  10. $actionList->setDefaultAction(function (Package $req,Package $res){
  11. $res->setMessage('this is goods default');
  12. });
  13. }
  14. }
  1. namespace App\RPC;
  2. use Core\Component\RPC\AbstractInterface\AbstractActionRegister;
  3. use Core\Component\RPC\Common\ActionList;
  4. use Core\Component\RPC\Common\Package;
  5. use Core\Component\Socket\Client\TcpClient;
  6. class User extends AbstractActionRegister
  7. {
  8. function register(ActionList $actionList)
  9. {
  10. // TODO: Implement register() method.
  11. $actionList->registerAction('who', function (Package $req, Package $res, TcpClient $client) {
  12. var_dump('your req package is' . $req->__toString());
  13. $res->setMessage('this is User.who');
  14. });
  15. $actionList->registerAction('login', function (Package $req, Package $res, TcpClient $client) {
  16. var_dump('your req package is' . $req->__toString());
  17. $res->setMessage('this is User.login');
  18. });
  19. $actionList->setDefaultAction(function (Package $req, Package $res, TcpClient $client) {
  20. $res->setMessage('this is user.default');
  21. });
  22. }
  23. }
  1. namespace App\RPC;
  2. use Core\Component\RPC\AbstractInterface\AbstractActionRegister;
  3. use Core\Component\RPC\Common\ActionList;
  4. use Core\Component\RPC\Common\Package;
  5. use Core\Component\Socket\Client\TcpClient;
  6. class User2 extends AbstractActionRegister
  7. {
  8. function register(ActionList $actionList)
  9. {
  10. // TODO: Implement register() method.
  11. $actionList->registerAction('who', function (Package $req, Package $res, TcpClient $client) {
  12. var_dump('your req package is' . $req->__toString());
  13. $res->setMessage('this is User.who');
  14. });
  15. $actionList->registerAction('login', function (Package $req, Package $res, TcpClient $client) {
  16. var_dump('your req package is' . $req->__toString());
  17. $res->setMessage('this is User.login');
  18. });
  19. $actionList->setDefaultAction(function (Package $req, Package $res, TcpClient $client) {
  20. $res->setMessage('this is user.default');
  21. });
  22. }
  23. }

以上代码,实现了三个不同服务的命令注册。

监听服务

在服务启动前事件中,进行服务注册。

  1. function beforeWorkerStart(\swoole_server $server)
  2. {
  3. // TODO: Implement beforeWorkerStart() method.
  4. $conf = new \Core\Component\RPC\Common\Config();
  5. $server = new Server($conf);
  6. $server->registerServer('user')->setActionRegisterClass(User::class);
  7. $server->registerServer('goods')->setActionRegisterClass(Goods::class);
  8. $server->attach(9502);
  9. $server2 = new Server($conf);
  10. $server2->registerServer('user')->setActionRegisterClass(User2::class);
  11. $server2->attach(9503);
  12. }

调用客户端

  1. use Core\Component\RPC\Common\Package;
  2. $conf = new \Core\Component\RPC\Common\Config();
  3. $conf->setPort(9502);
  4. $conf->setHost('127.0.0.1');
  5. $client = new \Core\Component\RPC\Client\Client();
  6. $server1 = $client->selectServer($conf);
  7. $server1->addCall('user','who',null,function (Package $req,Package $res){
  8. echo "call success at".$res->__toString()."\n";
  9. },function (Package $req,Package $res){
  10. echo "call fail at".$res->__toString()."\n";
  11. });
  12. $server1->addCall("user",'login',[1,2,3,4],function (Package $req,Package $res){
  13. echo "call success at".$res->__toString()."\n";
  14. });
  15. $server1->addCall("user",'404',[1,2,3,4],function (Package $req,Package $res){
  16. echo "call success at".$res->__toString()."\n";
  17. });
  18. $server1->addCall('goods','404',null,function (){
  19. echo "success"."\n";
  20. });
  21. $conf2 = new \Core\Component\RPC\Common\Config();
  22. $conf2->setHost('127.0.0.1');
  23. $conf2->setPort(9503);
  24. $server2 = $client->selectServer($conf2);
  25. $server2->addCall('user','404',null,function (){
  26. echo "success at server 2";
  27. });
  28. $client->call();