The configuration,

Following the steps in the previous article, the config\swoole.php configuration document will be added to the config directory

use app\webscoket\Manager; use Swoole\Table; use think\swoole\websocket\socketio\Parser; Env ('SWOOLE_HOST', '0.0.0.0'); return ['server' => [// set the default value to 127.0.0.1 if IP+ port access is not required. Env ('SWOOLE_PORT', 29999), 'mode' => SWOOLE_PROCESS, // The default mode is SWOOLE_PROCESS 'sock_type' => SWOOLE_SOCK_TCP, [// Swoole process pid is set at \runtime\swoole.pid 'pid_file' => root_path(). 'swoole.pid', // Swoole run the log directory 'log_file' => runtime_path(). 'swoole.log', "Daemonize" => true,// Normally this value should be 1~4 times larger according to your cpu cores. 'reactor_num' => swoole_cpu_num(), 'worker_num' => swoole_cpu_num(), 'task_worker_num' => swoole_cpu_num(), 'task_enable_coroutine' => true, 'task_max_request' => 2000,// set the maximum number of tasks for the task process. 'enable_static_handler' => true, 'document_root' => root_path('public'), 'package_max_length' => 20 * 1024 * 1024, 'buffer_output_size' => 10 * 1024 * 1024, 'socket_BUFFer_size' => 128 * 1024 * 1024,],], // WebSocket configuration area 'Websocket' => [// Whether to enable Websocket 'enable' => true, 'handler' => Manager::class, 'parser' => parser ::class, 'handler' => Manager::class 'ping_interval' = > 25000, / / ping frequency 'ping_timeout' = > 60000, / / there is no exit milliseconds after ping / / the following are some of the configuration of the room it will automatically create a high-performance memory database 'room' = > [ Redis 'type' => 'table', 'table' => ['room_rows' => 4096, 'room_size' => 2048, 'client_rows' => 8192, 'client_size' = > 2048], 'redis' = > [' host' = > '127.0.0.1', 'port' = > 6379, 'max_active' = > 3, 'max_wait_time' = > 5, 'listen' => [], 'subscribe' => [],], // Remote procedure call, which is a request for service from a remote computer program over the network. 'RPC' => ['server' => ['enable' => false, 'port' => 9000, 'services' => [],], 'client' = > []], / / hot update configuration 'hot_update' = > [/ / whether open hot update 'enable' = > env (' APP_DEBUG, false), 'name' => ['*.php']; 'name' => ['*.php']; / / to monitor directory Now listen to directory are: app \ crmeb \ 'include' = > [app_path (), root_path (' crmeb)], / / exclude directory 'exclude' = > [],]. 'Db' => ['enable' => true, 'max_active' => 3, 'max_active' => 3, 'max_wait_time' => 5,], 'cache' => ['enable' => true, 'max_active' => 3, 'max_wait_time' => 5,], // custom connection pool], 'tables' => [' size' => 2048, 'columns' => [['name' => 'fd', 'type' => Table::TYPE_INT], ['name' => 'type', 'type' => Table::TYPE_INT], ['name' => 'uid', 'type' => Table::TYPE_INT], ['name' => 'to_uid', 'type' => Table::TYPE_INT], ['name' => 'tourist', 'type' => Table::TYPE_INT]]]], // there are other configurations that are not explained.Copy the code

Important Configuration Explanation

There are a lot of configuration will need to pay attention to the following several places to explain in detail

Port and listening address

Env ('SWOOLE_HOST', '0.0.0.0'); return ['server' => [// set the default value to 127.0.0.1 if IP+ port access is not required. Env ('SWOOLE_PORT', 29999), 'mode' => SWOOLE_PROCESS, // The default mode is SWOOLE_PROCESS 'sock_type' => SWOOLE_SOCK_TCP, [// Swoole process pid is set at \runtime\swoole.pid 'pid_file' => root_path(). 'swoole.pid', // Swoole run the log directory 'log_file' => runtime_path(). 'swoole.log', 'daemonize' => true,// Whether the swoole daemon process is running [,],];Copy the code

The default configuration for server.host is 127.0.0.1, and 0.0.0.0 is used for debugging of external access

As you can see, we need to use the server IP + port number for access, note that this access requires the port to be enabled

Hot update

Return [// configure 'hot_update' => [// enable' hot_update' => env('APP_DEBUG', false), 'name' => ['*.php']; 'name' => ['*.php']; / / to monitor directory Now listen to directory are: app \ crmeb \ 'include' = > [app_path (), root_path (' crmeb)], / / exclude directory 'exclude' = > [],],]Copy the code

This command is used in the redevelopment phase. You do not need to run the restart command frequently. You are advised to disable debug in the production mode

In-memory database

Return [// memory database field can be created by itself database will be created by itself after swoole startup => [// high performance memory database 'user' => ['size' => 2048, 'columns' => [ ['name' => 'fd', 'type' => Table::TYPE_INT], ['name' => 'type', 'type' => Table::TYPE_INT], ['name' => 'uid', 'type' => Table::TYPE_INT], ['name' => 'to_uid', 'type' => Table::TYPE_INT], ['name' => 'tourist', 'type' => Table::TYPE_INT] ] ] ], ];Copy the code

Let’s start with the official explanation:

Since THE PHP language does not support multithreading, Swoole uses the multi-process mode. In the multi-process mode, there is process memory isolation, and the modification of global and super global variables in the worker process is invalid in other processes.

Advantage:

    • Strong performance, single thread read/write per second200Ten thousand times;
    • Application code does not need to be locked,TableBuilt-in row lock spin lock, all operations are multi-threaded/multi-process safe. The user layer does not need to consider data synchronization at all;
    • Support for multiple processes,TableCan be used to share data between multiple processes;
    • Use a row lock, rather than a global lock, only when two processes are in the sameCPUTime, concurrent reading of the same data will occur snatch lock.

Just look at the first one

For details, see the preceding configuration

Use:

use use think\swoole\Table;
use Swoole\Table as SwooleTable;
/** @var SwooleTable $table */
$table = app()->make(Table::class)->get('user');
Copy the code

Swoole \ table = “swoole\ table”; $table = “swoole\ table”;