Commit 77fd8169c742815a14a0671f2cae113644c3c89d
1 parent
800e411f
初步整合
Showing
3 changed files
with
27 additions
and
14 deletions
trunk/Swoole/Cli/Controller/Index/IndexController.class.php
@@ -14,7 +14,7 @@ class IndexController extends AbstractController | @@ -14,7 +14,7 @@ class IndexController extends AbstractController | ||
14 | { | 14 | { |
15 | public function index() | 15 | public function index() |
16 | { | 16 | { |
17 | - //print_r(C('MODULE_DENY_LIST')); | ||
18 | - echo "Swoole 已启动。\n"; | 17 | + echo "Swoole work进程已成功启动。\n"; |
19 | } | 18 | } |
20 | -} | ||
21 | \ No newline at end of file | 19 | \ No newline at end of file |
20 | +} | ||
21 | + |
trunk/Swoole/SwooleClient.php
@@ -53,8 +53,15 @@ class SwooleClient | @@ -53,8 +53,15 @@ class SwooleClient | ||
53 | } | 53 | } |
54 | 54 | ||
55 | $client = new SwooleClient(); | 55 | $client = new SwooleClient(); |
56 | -$client->connect('127.0.0.1', 9501); | ||
57 | -$client->send('11223'); | 56 | +$client->connect('127.0.0.1',9501); |
58 | 57 | ||
59 | -$recv = $client->recv(); | ||
60 | -echo $recv; | ||
61 | \ No newline at end of file | 58 | \ No newline at end of file |
59 | +$data = [ | ||
60 | + 'class' => 'Cli\Controller\Index\Task', | ||
61 | + 'method' => 'index', | ||
62 | + 'data' => [ | ||
63 | + 'name' => 'task', | ||
64 | + 'env' => 'dev' | ||
65 | + ], | ||
66 | +]; | ||
67 | +$data = json_encode($data); | ||
68 | +$client->send($data); | ||
62 | \ No newline at end of file | 69 | \ No newline at end of file |
trunk/Swoole/SwooleServer.php
@@ -16,8 +16,8 @@ class SwooleServer | @@ -16,8 +16,8 @@ class SwooleServer | ||
16 | 'port' => $port, | 16 | 'port' => $port, |
17 | 'env' => 'dev', //环境 dev|test|prod | 17 | 'env' => 'dev', //环境 dev|test|prod |
18 | 'process_name' => SWOOLE_TASK_NAME_PRE, //swoole 进程名称 | 18 | 'process_name' => SWOOLE_TASK_NAME_PRE, //swoole 进程名称 |
19 | - 'worker_num' => 4, //一般设置为服务器CPU数的1-4倍 | ||
20 | - 'task_worker_num' => 1, //task进程的数量 | 19 | + 'worker_num' => 1, //一般设置为服务器CPU数的1-4倍 |
20 | + 'task_worker_num' => 2, //task进程的数量 | ||
21 | 'task_ipc_mode' => 3, //使用消息队列通信,并设置为争抢模式 | 21 | 'task_ipc_mode' => 3, //使用消息队列通信,并设置为争抢模式 |
22 | 'task_max_request' => 10000, //task进程的最大任务数 | 22 | 'task_max_request' => 10000, //task进程的最大任务数 |
23 | 'daemonize' => 1, //以守护进程执行 | 23 | 'daemonize' => 1, //以守护进程执行 |
@@ -101,13 +101,14 @@ class SwooleServer | @@ -101,13 +101,14 @@ class SwooleServer | ||
101 | */ | 101 | */ |
102 | public function onWorkerStart($serv, $workerId) | 102 | public function onWorkerStart($serv, $workerId) |
103 | { | 103 | { |
104 | - if ($workerId >= $this->_setting['worker_num']) { | 104 | + /*if ($workerId >= $this->_setting['worker_num']) { |
105 | $this->setProcessName($this->_setting['process_name'] . '-task'); | 105 | $this->setProcessName($this->_setting['process_name'] . '-task'); |
106 | } else { | 106 | } else { |
107 | $this->setProcessName($this->_setting['process_name'] . '-event'); | 107 | $this->setProcessName($this->_setting['process_name'] . '-event'); |
108 | - } | 108 | + }*/ |
109 | + | ||
109 | // 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false | 110 | // 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false |
110 | - define('APP_DEBUG', True); | 111 | + define('APP_DEBUG', true); |
111 | // 定义应用目录 | 112 | // 定义应用目录 |
112 | define('APP_PATH', SWOOLE_PATH . DIRECTORY_SEPARATOR . 'Swoole' . DIRECTORY_SEPARATOR); | 113 | define('APP_PATH', SWOOLE_PATH . DIRECTORY_SEPARATOR . 'Swoole' . DIRECTORY_SEPARATOR); |
113 | // 定义应用模式 | 114 | // 定义应用模式 |
@@ -208,9 +209,13 @@ class SwooleServer | @@ -208,9 +209,13 @@ class SwooleServer | ||
208 | */ | 209 | */ |
209 | public function onTask($serv, $task_id, $from_id, $data) | 210 | public function onTask($serv, $task_id, $from_id, $data) |
210 | { | 211 | { |
211 | - $result = json_decode($data, true); | 212 | + $data = json_decode($data, true); |
212 | //用TP处理各种逻辑 | 213 | //用TP处理各种逻辑 |
213 | - $serv->finish($data); | 214 | + $class = new $data['class'](); |
215 | + $method = $data['method'] ?? 'index'; | ||
216 | + $res = $class->$method($data['data']); | ||
217 | + | ||
218 | + $serv->finish($res); | ||
214 | } | 219 | } |
215 | 220 | ||
216 | /** | 221 | /** |
@@ -238,4 +243,4 @@ class SwooleServer | @@ -238,4 +243,4 @@ class SwooleServer | ||
238 | echo 'Date:' . date('Y-m-d H:i:s') . "\t swoole_server close[" . $fd . "]\n"; | 243 | echo 'Date:' . date('Y-m-d H:i:s') . "\t swoole_server close[" . $fd . "]\n"; |
239 | } | 244 | } |
240 | } | 245 | } |
241 | -} | 246 | -} |
247 | +} | ||
242 | \ No newline at end of file | 248 | \ No newline at end of file |