Console

Easyswoole 提供了一个基于tcp的基础远程控制台,方便用户做开发阶段的调试或者是线上的一些远程管理。

安装

  1. composer require easyswoole/console

Server

  1. use EasySwoole\Console\Console;
  2. use EasySwoole\Console\ModuleInterface;
  3. $http = new swoole_http_server("127.0.0.1", 9501);
  4. $http->on("request", function ($request, $response) {
  5. $response->header("Content-Type", "text/plain");
  6. $response->end("Hello World\n");
  7. });
  8. /*
  9. * 开一个tcp端口给console 用
  10. */
  11. $tcp = $http->addlistener('0.0.0.0',9600,SWOOLE_TCP);
  12. /*
  13. * 实例化一个控制台,设置密码为123456
  14. */
  15. $console = new Console('myConsole','123456');
  16. /*
  17. * 定义自己的一个命令
  18. */
  19. class Test implements ModuleInterface
  20. {
  21. public function moduleName(): string
  22. {
  23. return 'test';
  24. }
  25. public function exec(array $arg, int $fd, Console $console)
  26. {
  27. return 'this is test exec';
  28. }
  29. public function help(array $arg, int $fd, Console $console)
  30. {
  31. return 'this is test help';
  32. }
  33. }
  34. /*
  35. * 命令注册
  36. */
  37. $console->moduleContainer()->set(new Test());
  38. /*
  39. * 依附给server
  40. */
  41. $console->protocolSet($tcp)->attachToServer($http);
  42. $http->start();

Client

  1. telnet 127.0.0.1 9600

鉴权

  1. auth {PASSWORD}

执行命令

  1. {MODULE} {ARG1} {ARG2}

例子-如何在Easyswoole中实现日志推送

模型定义

  1. namespace App\Utility;
  2. use EasySwoole\Console\Console;
  3. use EasySwoole\Console\ModuleInterface;
  4. use EasySwoole\EasySwoole\Config;
  5. class LogPusher implements ModuleInterface
  6. {
  7. public function moduleName(): string
  8. {
  9. return 'log';
  10. }
  11. public function exec(array $arg, int $fd, Console $console)
  12. {
  13. /*
  14. * 此处能这样做是因为easyswoole3.2.5后的版本改为swoole table存储配置了,因此配置不存在进程隔离
  15. */
  16. $op = array_shift($arg);
  17. switch ($op){
  18. case 'enable':{
  19. Config::getInstance()->setConf('logPush',true);
  20. break;
  21. }
  22. case "disable":{
  23. Config::getInstance()->setConf('logPush',false);
  24. break;
  25. }
  26. }
  27. $status = Config::getInstance()->getConf('logPush');
  28. $status = $status ? 'enable' : 'disable';
  29. return "log push is {$status}";
  30. }
  31. public function help(array $arg, int $fd, Console $console)
  32. {
  33. return 'this is log help';
  34. }
  35. }

服务注册

重点是在easyswoole 全局的事件中进行注册

  1. namespace EasySwoole\EasySwoole;
  2. use App\Utility\LogPusher;
  3. use EasySwoole\Console\Console;
  4. use EasySwoole\EasySwoole\Swoole\EventRegister;
  5. use EasySwoole\EasySwoole\AbstractInterface\Event;
  6. use EasySwoole\Http\Request;
  7. use EasySwoole\Http\Response;
  8. class EasySwooleEvent implements Event
  9. {
  10. public static function initialize()
  11. {
  12. // TODO: Implement initialize() method.
  13. date_default_timezone_set('Asia/Shanghai');
  14. }
  15. public static function mainServerCreate(EventRegister $register)
  16. {
  17. ServerManager::getInstance()->addServer('consoleTcp','9600',SWOOLE_TCP,'0.0.0.0',[
  18. 'open_eof_check'=>false
  19. ]);
  20. $consoleTcp = ServerManager::getInstance()->getSwooleServer('consoleTcp');
  21. /**
  22. 密码为123456
  23. */
  24. $console = new Console("MyConsole",'123456');
  25. /*
  26. * 注册日志模块
  27. */
  28. $console->moduleContainer()->set(new LogPusher());
  29. $console->protocolSet($consoleTcp)->attachToServer(ServerManager::getInstance()->getSwooleServer());
  30. /*
  31. * 给es的日志推送加上hook
  32. */
  33. Logger::getInstance()->onLog()->set('remotePush',function ($msg,$logLevel,$category)use($console){
  34. if(Config::getInstance()->getConf('logPush')){
  35. /*
  36. * 可以在 LogPusher 模型的exec方法中,对loglevel,category进行设置,从而实现对日志等级,和分类的过滤推送
  37. */
  38. foreach ($console->allFd() as $item){
  39. $console->send($item['fd'],$msg);
  40. }
  41. }
  42. });
  43. }
  44. public static function onRequest(Request $request, Response $response): bool
  45. {
  46. return true;
  47. }
  48. public static function afterRequest(Request $request, Response $response): void
  49. {
  50. // TODO: Implement afterAction() method.
  51. }
  52. }

测试

链接远程服务器

  1. telnet {IP} 9600

鉴权登录

  1. auth 123456

打开日志推送

  1. log enable

后续程序中的日志都会推送到你的控制台