Commit 77fd8169c742815a14a0671f2cae113644c3c89d
1 parent
800e411f
初步整合
Showing
3 changed files
with
27 additions
and
14 deletions
trunk/Swoole/Cli/Controller/Index/IndexController.class.php
trunk/Swoole/SwooleClient.php
... | ... | @@ -53,8 +53,15 @@ class SwooleClient |
53 | 53 | } |
54 | 54 | |
55 | 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 | 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 | 69 | \ No newline at end of file | ... | ... |
trunk/Swoole/SwooleServer.php
... | ... | @@ -16,8 +16,8 @@ class SwooleServer |
16 | 16 | 'port' => $port, |
17 | 17 | 'env' => 'dev', //环境 dev|test|prod |
18 | 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 | 21 | 'task_ipc_mode' => 3, //使用消息队列通信,并设置为争抢模式 |
22 | 22 | 'task_max_request' => 10000, //task进程的最大任务数 |
23 | 23 | 'daemonize' => 1, //以守护进程执行 |
... | ... | @@ -101,13 +101,14 @@ class SwooleServer |
101 | 101 | */ |
102 | 102 | public function onWorkerStart($serv, $workerId) |
103 | 103 | { |
104 | - if ($workerId >= $this->_setting['worker_num']) { | |
104 | + /*if ($workerId >= $this->_setting['worker_num']) { | |
105 | 105 | $this->setProcessName($this->_setting['process_name'] . '-task'); |
106 | 106 | } else { |
107 | 107 | $this->setProcessName($this->_setting['process_name'] . '-event'); |
108 | - } | |
108 | + }*/ | |
109 | + | |
109 | 110 | // 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false |
110 | - define('APP_DEBUG', True); | |
111 | + define('APP_DEBUG', true); | |
111 | 112 | // 定义应用目录 |
112 | 113 | define('APP_PATH', SWOOLE_PATH . DIRECTORY_SEPARATOR . 'Swoole' . DIRECTORY_SEPARATOR); |
113 | 114 | // 定义应用模式 |
... | ... | @@ -208,9 +209,13 @@ class SwooleServer |
208 | 209 | */ |
209 | 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 | 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 | 243 | echo 'Date:' . date('Y-m-d H:i:s') . "\t swoole_server close[" . $fd . "]\n"; |
239 | 244 | } |
240 | 245 | } |
241 | 246 | -} |
247 | +} | |
242 | 248 | \ No newline at end of file | ... | ... |