一 安装Tars 节点服务器
二 配置Swoft环境
swoft 安装 swoole 4.4.1 版本
安装 PHP 7.2
Linux 编译安装强的可以手动 安装配置环境.
如果想少踩点坑,可以用下面方法.
在节点服务器安装宝塔, 参考宝塔安装说明..
https://www.bt.cn/bbs/thread-19376-1-1.html
宝塔安装完成登陆进去会提示安装基本环境, 这时值选择 php7.2, 其它几项不需要可以去掉.
完成后在安装php 扩展
删除禁用函数
proc_open
三 安装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
设置全局变量
export PATH=/www/server/php/72/bin:$PATH
source /etc/profile
开始执行编译安装
cd tars-extension-master
sudo phpize
sudo ./configure
sudo make
sudo make install
在php.ini里面加入extension=phptars.so
四 项目对接Tars代码
修改composer.json,加入phptars的包,以及打包命令。
{
"require": {
...
"phptars/tars-server": "~0.1",
"phptars/tars-deploy": "~0.1",
"phptars/tars2php": "~0.1",
"phptars/tars-log": "~0.1",
"ext-zip" : ">=0.0.1"
...
},
"scripts": {
...
"deploy": "\\Tars\\deploy\\Deploy::run"
...
}
}
安装依赖 composer install
项目根目录创建index.php文件
<?php
// tars 平台然后文件
//读取tars conf配置
//处理合成 env文件
$args = $_SERVER['argv'];
$swoft_bin = dirname(__FILE__).'/bin/swoft ';
$arg_cmd = $args[2]=='start' ? 'http:start -d' : $args[2] ;
$cmd = "/usr/bin/php " . $swoft_bin . $arg_cmd;
var_dump($cmd);
exec($cmd, $output, $r);
写一个用于调用Tars平台各种接口的class 创建: /app/Tars/Manage.php
<?php
namespace App\Tars;
use \Tars\report\ServerFSync;
use \Tars\report\ServerFAsync;
use \Tars\report\ServerInfo;
use \Tars\Utils;
/**
*
*/
class Manage
{
public function getNodeInfo(){
$conf = $this->getTarsConf();
if( !empty($conf) ){
$node = $conf['tars']['application']['server']['node'];
$nodeInfo = Utils::parseNodeInfo($node);
return $nodeInfo;
}else{
return [];
}
}
public function getTarsConf(){
//$tars_conf = "/usr/local/app/tars/tarsnode/data/".env('PNAME')."/conf/".env('PNAME').".config.conf";
$tars_conf = "/usr/local/app/tars/tarsnode/data/".config("tars.pname")."/conf/".config("tars.pname").".config.conf";
//$tars_conf = dirname(__DIR__, 1).'/conf/'.env('PNAME').'.config.conf';
if( is_file($tars_conf) ){
$conf = Utils::parseFile($tars_conf);
//var_dump("配置conf:", $conf);
return $conf;
}else{
var_dump('get tars_conf file error : '.$tars_conf);
return [];
}
}
public function keepAlive()
{
//$pname = env('PNAME');
$pname = config("tars.pname");
//var_dump($pname);
$pname = explode('.',$pname);
//$adapter = env('PNAME').'.objAdapter';
$adapter = config("tars.pname").'.objAdapter';
$application = $pname[0];
$serverName = $pname[1];
$masterPid = getmypid();
$nodeInfo = $this->getNodeInfo();
if( empty($nodeInfo) ){
var_dump('keepAlive getNodeInfo fail');
return null;
}
$host = $nodeInfo['host'];
$port = $nodeInfo['port'];
$objName = $nodeInfo['objName'];
$serverInfo = new ServerInfo();
$serverInfo->adapter = $adapter;
$serverInfo->application = $application;
$serverInfo->serverName = $serverName;
$serverInfo->pid = $masterPid;
$serverF = new ServerFSync($host, $port, $objName);
$serverF->keepAlive($serverInfo);
$adminServerInfo = new ServerInfo();
$adminServerInfo->adapter = 'AdminAdapter';
$adminServerInfo->application = $application;
$adminServerInfo->serverName = $serverName;
$adminServerInfo->pid = $masterPid;
$serverF->keepAlive($adminServerInfo);
var_dump(' keepalive ');
}
}
创建: /config/tars.php
<?php
return [
'pname' => 'swoft.liu' //Tars 项目名.服务名, 根据项目名称自行修改
];
在框架启动成功的时候,上报服务存活,这使用的swoft的框架的事件监听 启动的时候上报存活, 先这样写后面改用 服务注册
创建: /app/Listener/APPStart.php
<?php declare(strict_types=1);
namespace App\Listener;
use Swoft\Event\Annotation\Mapping\Listener;
use Swoft\Event\EventHandlerInterface;
use Swoft\Event\EventInterface;
use App\Tars\Manage;
use Swoft\Server\ServerEvent;
/**
* Class APPStart
*
* @Listener(ServerEvent::BEFORE_START)
*/
class APPStart implements EventHandlerInterface
{
/**
* @param EventInterface $event
*/
public function handle(EventInterface $event): void
{
//服务启动 只上报一次 TODOs
$manage = new Manage();
$manage->keepAlive();
var_dump("服务器启动,上报一次");
}
}
每隔30s上报一次存活,这里使用swoft框架注解试的定时任务 创建: /app/Task/Crontab/TarsKeepAliveTask.php
<?php declare(strict_types=1);
namespace App\Task\Crontab;
use Swoft\Crontab\Annotaion\Mapping\Cron;
use Swoft\Crontab\Annotaion\Mapping\Scheduled;
use App\Tars\Manage;
/**
* Class CronTask
*
* @since 2.0
*
* @Scheduled("TarsKeepAliveTask")
*/
class TarsKeepAliveTask
{
/**
* @Cron("30 * * * * *")
*/
public function secondTask()
{
$manage = new Manage();
$manage->keepAlive();
return 'cron';
}
}
完成
打包 composer run-script deploy
上传tars平台