当前位置:网站首页>[swoole series 2.1] run the swoole first
[swoole series 2.1] run the swoole first
2022-07-06 18:13:00 【Ma Nong Lao Zhang ZY】
The first Swoole Run
In the face of Swoole After having a preliminary impression , Today we will simply build Swoole Environment and run a simple Http service . Don't be too stressed , Today's content doesn't have much theoretical stuff , Prepare the running environment step by step , Can see Swoole The effect of running is ok .
Environmental preparation
Actually install Swoole Expansion is not troublesome , And others PHP Just extend the same installation process . But here's the thing ,Swoole It will conflict with some extensions , for instance XDebug、phptrace、aop、molten、xhprof、phalcon( The coroutine cannot run on phalcon In the frame ).
Everyone must be worried , Out of commission XDebug , Our debugging will be very troublesome ! No problem ,Swoole It also has its own recommended debugging tools , Interested partners can consult relevant information by themselves .
If you are Windows Environmental Science , Due to operating system differences , There is no direct dll Expansion packs can use . So in Windows In this environment, it is best to build a virtual machine , Then set the PHPStrom The synchronization of can be easily developed . I won't say more about this configuration , Baidu search a lot .
ad locum , I also installed it through virtual machine , The system environment is CentOS8、PHP8.1、Swoole4.8.3、MySQL8.0.27 It's all the latest , If you are PHP7 as well as Swoole4.4 If so, it's no problem , It won't make a big difference . Other tools , such as Nginx、Redis etc. , You can install it according to the situation , We will also use it in subsequent courses .
After the extension is successfully installed , stay php.ini Add extended information to the file , Then you can view it through the following command line Swoole Version information and extended configuration .
[[email protected] ~]# php --ri swoole
swoole
Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 4.8.3
Built => Dec 7 2021 22:01:03
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled
Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
Start a service
I believe you guys have no problem installing the environment and extensions , If there's a problem , It's no use asking me , There are many metaphysics in compiling and installing these things ( Muddled ) The problem is , We can only practice with virtual machines . After the environment is installed successfully , Let's simply build a Http Service! .
what ? Start one directly Http service ? Not to use Nginx perhaps Apache Do you ? Forget what we said in the last article ,Swoole Start the service directly , Not like the traditional PHP Use FastCGI To start up php-fpm In this form . perhaps , You can also understand it as direct use Swoole The service started is php-fpm The one started in the form of connection 9000 Service of port .
About php-fpm Relevant knowledge , We were
understand PHP-FPM https://mp.weixin.qq.com/s/NUpDnfYfbPuWmal4Am3lsg
There are detailed instructions in , You can go back and have a look .
Okay , Let's get to the point , Start with a new file , Then enter the following code .
<?php
$http = new Swoole\Http\Server('0.0.0.0', 9501);
$http->on('Request', function ($request, $response) {
echo " Request received ";
$response->header('Content-Type', 'text/html; charset=utf-8');
$response->end('<h1>Hello Swoole. #' . rand(1000, 9999) . '</h1>');
});
echo " Service startup ";
$http->start();
Put it on Swoole In the system of the environment , Then execute the command-line script to run it .
[[email protected] source]# php 2.1 The first Swoole Run .php
Service startup
As soon as it runs, the command line stops , Obviously , Now the program has been mounted . Then we go to the browser to visit the page , If it's local , direct http://localhost:9501 That's all right. , If it's a virtual machine , Access to the virtual machine IP Address and add the port number . Be careful , If the access fails, you can check whether the firewall is turned off .
The result of visiting the page can be seen as the following screenshot .
meanwhile , The following content will also be output in the command line .
[[email protected] source]# php 2.1 The first Swoole Run .php
Service startup received request
congratulations , Yours Swoole There is no problem with the environment , Now the simplest Http The server has been set up successfully !
Next , Let's try to modify the contents of the file , Output information like above does not wrap , Let's add line breaks .
// .....
echo " Request received ", PHP_EOL;
// .....
echo " Service startup ", PHP_EOL;
Request page again , You will find that the output of the command line still does not wrap , Why is that ?
Remember the problem of dynamic language and static language we talked about in the last article , current Swoole In fact, it is already similar to the operation mode of static language . It has mounted the program as a separate process , In fact, this time is equivalent to having compiled a similar jar perhaps exe The file of , And it runs directly . therefore , Our modification of the file will not have any impact on the program in the current process . If you want to update the modified content , You need to restart the service . Now Ctrl+C Close the app first , Then execute the command line . You will see that the output will be wrapped .
[[email protected] source]# php 2.1 The first Swoole Run .php
Service startup
Request received
Request received
Request received
Request received
echo Why is it printed on the command line
I believe many students will have another question , Why? echo Output to the command line ? Conventional PHP In development , our echo It is directly output to the page ?
This is another Swoole Different from traditional development . stay Swoole in , Our service program is suspended using the command line , The code above actually implements a Http service function , Not through php-fpm To output to Nginx This kind of server . Think from a different angle ,php-fpm It just gives these outputs to the server program , Then the server program outputs it to the page as it is . But in Swoole in ,echo Such output streams directly send the results to the corresponding operating system stdout Yes . Correspondingly , Our service output is directly through Swoole On the service callback parameter in the code response Object to perform the service flow output process .
The thinking of this place needs to be changed . If you have Java Development should not be strange here , stay Swoole In the environment ,echo(print、var_dump() wait ) Such traditional output becomes Java Medium System.out.println() . Again , stay Java in , No matter what framework , You need to output the result value on the page , Also through a similar Response Object . Actually , Just output our printed content to different streams , Ordinary print flows to stdout On , and Response The object is through TCP Output the result to the response stream . The principle of this piece is very deep , At least we have to learn more about the network , Unfortunately, my current level is not up to , So interested partners should check the relevant information by themselves !
summary
Today's learning content is not much , The most important thing is that we first understand the simplest Http How service works , And the similarities and differences between the output and the traditional development mode . Of course , what's more , Now you have to prepare the development environment . Otherwise, the follow-up content cannot be practiced together .
Test code :
https://github.com/zhangyue0503/swoole/blob/main/2. Basics /source/2.1 The first Swoole Run .php
Reference documents :
https://wiki.swoole.com/#/environment
https://wiki.swoole.com/#/start/start_http_server
边栏推荐
- Olivetin can safely run shell commands on Web pages (Part 1)
- Today in history: the mother of Google was born; Two Turing Award pioneers born on the same day
- Jerry's watch deletes the existing dial file [chapter]
- 2022暑期项目实训(三)
- 2019阿里集群数据集使用总结
- 面试突击62:group by 有哪些注意事项?
- std::true_ Type and std:: false_ type
- 最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
- 重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
- Markdown grammar - better blogging
猜你喜欢
30 分钟看懂 PCA 主成分分析
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
Easy introduction to SQL (1): addition, deletion, modification and simple query
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
Jerry's access to additional information on the dial [article]
78 year old professor Huake has been chasing dreams for 40 years, and the domestic database reaches dreams to sprint for IPO
F200——搭载基于模型设计的国产开源飞控系统无人机
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
历史上的今天:Google 之母出生;同一天诞生的两位图灵奖先驱
随机推荐
Excel usage record
J'aimerais dire quelques mots de plus sur ce problème de communication...
STM32按键状态机2——状态简化与增加长按功能
趣-关于undefined的问题
RB157-ASEMI整流桥RB157
1700C - Helping the Nature
Open source and safe "song of ice and fire"
Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
Pytest learning ----- detailed explanation of the request for interface automation test
传统家装有落差,VR全景家装让你体验新房落成效果
MSF horizontal MSF port forwarding + routing table +socks5+proxychains
2022暑期项目实训(三)
DNS hijacking
偷窃他人漏洞报告变卖成副业,漏洞赏金平台出“内鬼”
开源与安全的“冰与火之歌”
C language exchanges two numbers through pointers
Redis的五种数据结构
10 advanced concepts that must be understood in learning SQL
Jerry is the custom background specified by the currently used dial enable [chapter]
重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...