TP6+Swoole4 配置详解

配置详解

按照上一篇文章中得步骤安装后会在config目录下增加config\swoole.php配置文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
php复制代码use app\webscoket\Manager;
use Swoole\Table;
use think\swoole\websocket\socketio\Parser;

return [
'server' => [
// 默认配置为127.0.0.1 如果不需要用IP+端口访问得话可以不用改
'host' => env('SWOOLE_HOST', '0.0.0.0'), // 监听地址
'port' => env('SWOOLE_PORT', 29999), // 监听端口
'mode' => SWOOLE_PROCESS, // 运行模式 默认为SWOOLE_PROCESS
'sock_type' => SWOOLE_SOCK_TCP, // sock type 默认为SWOOLE_SOCK_TCP
'options' => [
// swoole进程得pid默认配置是在\runtime\swoole.pid
'pid_file' => root_path() . 'swoole.pid',
// swoole运行得日志目录
'log_file' => runtime_path() . 'swoole.log',
// 这个配置会影响swoole启动命令后是否进程守护,关闭命令行后还能继续运行
'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,//设置 task 进程的最大任务数
'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配置区域
'websocket' => [
//是否开启websocket
'enable' => true,
//处理事件类名,这是是根据项目自行写得类,下面也会列出类中得方法和处理机制
'handler' => Manager::class,
//解析类可直接使用TP6内置得类就可以了
'parser' => Parser::class,
'ping_interval' => 25000,//ping频率
'ping_timeout' => 60000,//没有ping后退出毫秒数
//下面是一些房间得配置这里会自动创建一个高性能内存数据库
'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,
],
],
//socket监听得事件也可以在这里配置,也可以在app\event.php内配置
'listen' => [],
'subscribe' => [],
],
//远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的思想
//做微服务使用项目中没有使用就不过多说
'rpc' => [
'server' => [
'enable' => false,
'port' => 9000,
'services' => [ ],
],
'client' => [ ],
],
//热更新配置
'hot_update' => [
//是否开启热更新
'enable' => env('APP_DEBUG', false),
//监听文件得类型 例如:*.html / *.js 都是可以得,但这个配置已经够用了不需要再调整
'name' => ['*.php'],
//监听的目录 目前监听得目录有:app\ crmeb\
'include' => [app_path(), root_path('crmeb')],
//排除的目录
'exclude' => [],
], //连接池
'pool' => [
//数据库连接池默认是开启的,在使用Db或者Model中不需要配置什么就自带连接池
'db' => [
'enable' => true,
'max_active' => 3,
'max_wait_time' => 5,
],
//缓存连接池 使用cache方式和之前一模一样没有任何的区别
'cache' => [
'enable' => true,
'max_active' => 3,
'max_wait_time' => 5,
],
//自定义连接池
],
//内存数据库 字段可自行创建 数据库会在swoole启动后自行创建
'tables' => [
//高性能内存数据库
'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]
]
]
], //还有其他配置不做解释,个人用的较少,有需求查阅swoole4的开发文档
];

重要配置讲解

里面有诸多得配置下面会把需要注意得几个地方详细讲解

端口和监听地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
dart复制代码return [
'server' => [
// 默认配置为127.0.0.1 如果不需要用IP+端口访问得话可以不用改
'host' => env('SWOOLE_HOST', '0.0.0.0'), // 监听地址
'port' => env('SWOOLE_PORT', 29999), // 监听端口
'mode' => SWOOLE_PROCESS, // 运行模式 默认为SWOOLE_PROCESS
'sock_type' => SWOOLE_SOCK_TCP, // sock type 默认为SWOOLE_SOCK_TCP
'options' => [
// swoole进程得pid默认配置是在\runtime\swoole.pid
'pid_file' => root_path() . 'swoole.pid',
// swoole运行得日志目录
'log_file' => runtime_path() . 'swoole.log',
// 这个配置会影响swoole启动命令后是否进程守护,关闭命令行后还能继续运行
'daemonize' => true,//是否守护进程
],
],
];

server.host默认配置为127.0.0.1,需要外网访问调试的使用这里要监听0.0.0.0

可以看到需要我们使用服务器ip+端口号进行访问,注意这样访问需要开启端口

热更新

1
2
3
4
5
6
7
8
9
10
11
12
dart复制代码return [
//热更新配置 'hot_update' => [
//是否开启热更新
'enable' => env('APP_DEBUG', false),
//监听文件得类型 例如:*.html / *.js 都是可以得,但这个配置已经够用了不需要再调整
'name' => ['*.php'],
//监听的目录 目前监听得目录有:app\ crmeb\
'include' => [app_path(), root_path('crmeb')],
//排除的目录
'exclude' => [],
],
]

主要使用再开发阶段时使用,不用频繁的手动执行重启命令,建议再生产模式下关闭debug运行

内存数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dart复制代码return [
//内存数据库 字段可自行创建 数据库会在swoole启动后自行创建 'tables' => [
//高性能内存数据库
'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]
]
]
],
];

先来看下官方的讲解:

由于 PHP 语言不支持多线程,因此 Swoole 使用多进程模式,在多进程模式下存在进程内存隔离,在工作进程内修改 global 全局变量和超全局变量时,在其他进程是无效的。

优势:

    • 性能强悍,单线程每秒可读写 200 万次;
      • 应用代码无需加锁,Table 内置行锁自旋锁,所有操作均是多线程 / 多进程安全。用户层完全不需要考虑数据同步问题;
      • 支持多进程,Table 可以用于多进程之间共享数据;
      • 使用行锁,而不是全局锁,仅当 2 个进程在同一 CPU 时间,并发读取同一条数据才会进行发生抢锁。

单看第一条就觉得牛*

配置可参考上述配置

使用:

1
2
3
4
rust复制代码use use think\swoole\Table;
use Swoole\Table as SwooleTable;
/** @var SwooleTable $table */
$table = app()->make(Table::class)->get('user');

返回的$table就可以使用swoole\Table的方法了,详细使用文档可参考:wiki.swoole.com/#/memory/ta…

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%