78 lines
3.4 KiB
PHP
78 lines
3.4 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||
// +----------------------------------------------------------------------
|
||
// | Author: liu21st <liu21st@gmail.com>
|
||
// +----------------------------------------------------------------------
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | Workerman设置 仅对 php think worker 指令有效
|
||
// +----------------------------------------------------------------------
|
||
|
||
use GuzzleHttp\Client;
|
||
use think\facade\Log;
|
||
use Workerman\Worker;
|
||
use Workerman\Lib\Timer;
|
||
|
||
return [
|
||
// 扩展自身需要的配置
|
||
'host' => '0.0.0.0', // 监听地址
|
||
'port' => 2346, // 监听端口
|
||
'root' => '', // WEB 根目录 默认会定位public目录
|
||
'app_path' => '', // 应用目录 守护进程模式必须设置(绝对路径)
|
||
'file_monitor' => false, // 是否开启PHP文件更改监控(调试模式下自动开启)
|
||
'file_monitor_interval' => 2, // 文件监控检测时间间隔(秒)
|
||
'file_monitor_path' => [], // 文件监控目录 默认监控application和config目录
|
||
|
||
// 支持workerman的所有配置参数
|
||
'name' => 'thinkphp',
|
||
'count' => 1,
|
||
'daemonize' => false,
|
||
'pidFile' => '',
|
||
'onWorkerStart' => function($worker) {
|
||
$task = new Worker();
|
||
$task->count = 1;
|
||
$task->onWorkerStart = function(Worker $task) {
|
||
// 获取本地IP
|
||
$serverIP = env('SERVER_ADDR', '192.168.1.161');
|
||
$serverPort = env('SERVER_PORT', 2346);
|
||
// 控制台输出
|
||
Log::record('workerman start');
|
||
Log::record('Workerman config:' . $serverIP . ':' . $serverPort, 'info');
|
||
$client = new Client([
|
||
// nacos地址
|
||
"base_uri" => "http://192.168.1.105:8848",
|
||
"timeout" => 5
|
||
]);
|
||
// 每2.5秒执行一次
|
||
Timer::add(5, function() use ($client, $serverIP, $serverPort)
|
||
{
|
||
$response = $client->request("put", "/nacos/v1/ns/instance/beat", [
|
||
"form_params" => [
|
||
"serviceName" => "zeroerroa-file",
|
||
"ip" => $serverIP,
|
||
"port" => $serverPort,
|
||
"namespaceId" => "0a1c32f2-dea1-4ec1-b546-cb4635cd7db2",
|
||
"groupName" => "DEFAULT_GROUP",
|
||
"beat" => '{
|
||
"cluster": "DEFAULT",
|
||
"ip": "' . $serverIP . '",
|
||
"metadata": {},
|
||
"port": "' . $serverPort . '",
|
||
"scheduled": true,
|
||
"serviceName": "zeroerroa-file",
|
||
"weight": 1
|
||
}'
|
||
],
|
||
]);
|
||
echo "send heartbeat: " . $response->getBody()->getContents();
|
||
echo "\n";
|
||
});
|
||
};
|
||
}
|
||
];
|