This commit is contained in:
小王 2024-09-12 23:34:20 +08:00
parent 109e447f6c
commit e1873ae55f
8 changed files with 22 additions and 186 deletions

14
.env Normal file
View File

@ -0,0 +1,14 @@
APP_DEBUG = false
DB_TYPE = mysql
DB_HOST = 127.0.0.1
DB_NAME = test
DB_USER = username
DB_PASS = password
DB_PORT = 3306
DB_CHARSET = utf8
DEFAULT_LANG = zh-cn
SERVER_ADDR = 192.168.1.161
SERVER_PORT = 8000

1
.gitignore vendored
View File

@ -1,5 +1,4 @@
*.log *.log
.env
composer.phar composer.phar
composer.lock composer.lock
.DS_Store .DS_Store

View File

@ -25,7 +25,7 @@
"topthink/think-orm": "^3.0", "topthink/think-orm": "^3.0",
"topthink/think-filesystem": "^2.0", "topthink/think-filesystem": "^2.0",
"guzzlehttp/guzzle": "^7.9", "guzzlehttp/guzzle": "^7.9",
"topthink/think-worker": "^4.0" "workerman/workerman": "^4.1"
}, },
"require-dev": { "require-dev": {
"symfony/var-dumper": ">=4.2", "symfony/var-dumper": ">=4.2",

View File

@ -1,45 +0,0 @@
<?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:gateway 指令有效
// +----------------------------------------------------------------------
return [
// 扩展自身需要的配置
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host' => '0.0.0.0', // 监听地址
'port' => 2348, // 监听端口
'socket' => '', // 完整监听地址
'context' => [], // socket 上下文选项
'register_deploy' => true, // 是否需要部署register
'businessWorker_deploy' => true, // 是否需要部署businessWorker
'gateway_deploy' => true, // 是否需要部署gateway
// Register配置
'registerAddress' => '127.0.0.1:1236',
// Gateway配置
'name' => 'thinkphp',
'count' => 1,
'lanIp' => '127.0.0.1',
'startPort' => 2000,
'daemonize' => false,
'pingInterval' => 30,
'pingNotResponseLimit' => 0,
'pingData' => '{"type":"ping"}',
// BusinsessWorker配置
'businessWorker' => [
'name' => 'BusinessWorker',
'count' => 1,
'eventHandler' => '\think\worker\Events',
],
];

View File

@ -1,77 +0,0 @@
<?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";
});
};
}
];

View File

@ -1,59 +0,0 @@
<?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:server 指令有效
// +----------------------------------------------------------------------
use think\facade\Log;
return [
// 扩展自身需要的配置
'protocol' => 'websocket', // 协议 支持 tcp udp unix http websocket text
'host' => '0.0.0.0', // 监听地址
'port' => 2345, // 监听端口
'socket' => '', // 完整监听地址
'context' => [], // socket 上下文选项
'worker_class' => '', // 自定义Workerman服务类名 支持数组定义多个服务
// 支持workerman的所有配置参数
'name' => 'thinkphp',
'count' => 4,
'daemonize' => false,
'pidFile' => '',
// 支持事件回调
// onWorkerStart
'onWorkerStart' => function ($worker) {
// 添加新线程
// Log::info('WorkerStart');
},
// onWorkerReload
'onWorkerReload' => function ($worker) {
},
// onConnect
'onConnect' => function ($connection) {
},
// onMessage
'onMessage' => function ($connection, $data) {
$connection->send('receive success');
},
// onClose
'onClose' => function ($connection) {
},
// onError
'onError' => function ($connection, $code, $msg) {
echo "error [ $code ] $msg\n";
},
];

View File

@ -6,7 +6,7 @@ server {
server_name localhost; server_name localhost;
root /var/www/html/public; root /var/www/html/public;
index index.html index.htm index.nginx-debian.html; index index.php index.html index.htm index.nginx-debian.html;
location / { location / {
try_files $uri $uri/ =404; try_files $uri $uri/ =404;

8
run.sh
View File

@ -2,5 +2,9 @@
# 后台启动 # 后台启动
php-fpm -D php-fpm -D
# 关闭后台启动hold住进程 # 启动nginx
nginx -g 'daemon off;' nginx
# 启动nacos心跳服务
php ./start.php
# hold后台
sh