终于完成了…. 后面还要测试通信协议

一 安装Tars 节点服务器

请参考Tars 文档

二 配置Swoft环境

swoft 安装 swoole 4.4.1 版本
安装 PHP 7.2
Linux 编译安装强的可以手动 安装配置环境.
如果想少踩点坑,可以用下面方法.
在节点服务器安装宝塔, 参考宝塔安装说明..
https://www.bt.cn/bbs/thread-19376-1-1.html
宝塔安装完成登陆进去会提示安装基本环境, 这时值选择 php7.2, 其它几项不需要可以去掉.
完成后在安装php 扩展
image.png
image.png
删除禁用函数

  1. proc_open

到此 swoft 运行环境完成

三 安装tarsphp

项目地址 https://github.com/TarsPHP/tars-extension
下载&解压 https://github.com/TarsPHP/tars-extension/archive/master.zip
解压好进入目录
cd tars-extension-master
修改 auto7.sh 文件
phpize=/www/server/php/72/bin/phpize
phpconfig=/www/server/php/72/bin/php-config
image.png

  1. 设置全局变量
  2. export PATH=/www/server/php/72/bin:$PATH
  3. source /etc/profile

开始执行编译安装

  1. cd tars-extension-master
  2. sudo phpize
  3. sudo ./configure
  4. sudo make
  5. sudo make install
  6. php.ini里面加入extension=phptars.so

四 项目对接Tars代码

修改composer.json,加入phptars的包,以及打包命令。

  1. {
  2. "require": {
  3. ...
  4. "phptars/tars-server": "~0.1",
  5. "phptars/tars-deploy": "~0.1",
  6. "phptars/tars2php": "~0.1",
  7. "phptars/tars-log": "~0.1",
  8. "ext-zip" : ">=0.0.1"
  9. ...
  10. },
  11. "scripts": {
  12. ...
  13. "deploy": "\\Tars\\deploy\\Deploy::run"
  14. ...
  15. }
  16. }

安装依赖 composer install

项目根目录创建index.php文件

  1. <?php
  2. // tars 平台然后文件
  3. //读取tars conf配置
  4. //处理合成 env文件
  5. $args = $_SERVER['argv'];
  6. $swoft_bin = dirname(__FILE__).'/bin/swoft ';
  7. $arg_cmd = $args[2]=='start' ? 'http:start -d' : $args[2] ;
  8. $cmd = "/usr/bin/php " . $swoft_bin . $arg_cmd;
  9. var_dump($cmd);
  10. exec($cmd, $output, $r);

写一个用于调用Tars平台各种接口的class 创建: /app/Tars/Manage.php

  1. <?php
  2. namespace App\Tars;
  3. use \Tars\report\ServerFSync;
  4. use \Tars\report\ServerFAsync;
  5. use \Tars\report\ServerInfo;
  6. use \Tars\Utils;
  7. /**
  8. *
  9. */
  10. class Manage
  11. {
  12. public function getNodeInfo(){
  13. $conf = $this->getTarsConf();
  14. if( !empty($conf) ){
  15. $node = $conf['tars']['application']['server']['node'];
  16. $nodeInfo = Utils::parseNodeInfo($node);
  17. return $nodeInfo;
  18. }else{
  19. return [];
  20. }
  21. }
  22. public function getTarsConf(){
  23. //$tars_conf = "/usr/local/app/tars/tarsnode/data/".env('PNAME')."/conf/".env('PNAME').".config.conf";
  24. $tars_conf = "/usr/local/app/tars/tarsnode/data/".config("tars.pname")."/conf/".config("tars.pname").".config.conf";
  25. //$tars_conf = dirname(__DIR__, 1).'/conf/'.env('PNAME').'.config.conf';
  26. if( is_file($tars_conf) ){
  27. $conf = Utils::parseFile($tars_conf);
  28. //var_dump("配置conf:", $conf);
  29. return $conf;
  30. }else{
  31. var_dump('get tars_conf file error : '.$tars_conf);
  32. return [];
  33. }
  34. }
  35. public function keepAlive()
  36. {
  37. //$pname = env('PNAME');
  38. $pname = config("tars.pname");
  39. //var_dump($pname);
  40. $pname = explode('.',$pname);
  41. //$adapter = env('PNAME').'.objAdapter';
  42. $adapter = config("tars.pname").'.objAdapter';
  43. $application = $pname[0];
  44. $serverName = $pname[1];
  45. $masterPid = getmypid();
  46. $nodeInfo = $this->getNodeInfo();
  47. if( empty($nodeInfo) ){
  48. var_dump('keepAlive getNodeInfo fail');
  49. return null;
  50. }
  51. $host = $nodeInfo['host'];
  52. $port = $nodeInfo['port'];
  53. $objName = $nodeInfo['objName'];
  54. $serverInfo = new ServerInfo();
  55. $serverInfo->adapter = $adapter;
  56. $serverInfo->application = $application;
  57. $serverInfo->serverName = $serverName;
  58. $serverInfo->pid = $masterPid;
  59. $serverF = new ServerFSync($host, $port, $objName);
  60. $serverF->keepAlive($serverInfo);
  61. $adminServerInfo = new ServerInfo();
  62. $adminServerInfo->adapter = 'AdminAdapter';
  63. $adminServerInfo->application = $application;
  64. $adminServerInfo->serverName = $serverName;
  65. $adminServerInfo->pid = $masterPid;
  66. $serverF->keepAlive($adminServerInfo);
  67. var_dump(' keepalive ');
  68. }
  69. }

创建: /config/tars.php

  1. <?php
  2. return [
  3. 'pname' => 'swoft.liu' //Tars 项目名.服务名, 根据项目名称自行修改
  4. ];

在框架启动成功的时候,上报服务存活,这使用的swoft的框架的事件监听 启动的时候上报存活, 先这样写后面改用 服务注册
创建: /app/Listener/APPStart.php

  1. <?php declare(strict_types=1);
  2. namespace App\Listener;
  3. use Swoft\Event\Annotation\Mapping\Listener;
  4. use Swoft\Event\EventHandlerInterface;
  5. use Swoft\Event\EventInterface;
  6. use App\Tars\Manage;
  7. use Swoft\Server\ServerEvent;
  8. /**
  9. * Class APPStart
  10. *
  11. * @Listener(ServerEvent::BEFORE_START)
  12. */
  13. class APPStart implements EventHandlerInterface
  14. {
  15. /**
  16. * @param EventInterface $event
  17. */
  18. public function handle(EventInterface $event): void
  19. {
  20. //服务启动 只上报一次 TODOs
  21. $manage = new Manage();
  22. $manage->keepAlive();
  23. var_dump("服务器启动,上报一次");
  24. }
  25. }

每隔30s上报一次存活,这里使用swoft框架注解试的定时任务 创建: /app/Task/Crontab/TarsKeepAliveTask.php

  1. <?php declare(strict_types=1);
  2. namespace App\Task\Crontab;
  3. use Swoft\Crontab\Annotaion\Mapping\Cron;
  4. use Swoft\Crontab\Annotaion\Mapping\Scheduled;
  5. use App\Tars\Manage;
  6. /**
  7. * Class CronTask
  8. *
  9. * @since 2.0
  10. *
  11. * @Scheduled("TarsKeepAliveTask")
  12. */
  13. class TarsKeepAliveTask
  14. {
  15. /**
  16. * @Cron("30 * * * * *")
  17. */
  18. public function secondTask()
  19. {
  20. $manage = new Manage();
  21. $manage->keepAlive();
  22. return 'cron';
  23. }
  24. }

完成
打包 composer run-script deploy
上传tars平台