Installation experience for swoole learning swoole

Identify Network Communication

What is network communication

We often visit a website, such as Baidu, Tencent, Sina, etc. He accesses it through http service protocol, which is called network communication. Here is a simple diagram

Swoole is a leader in this network communication. Let's learn about swoole

Introduction to swoole

Introduction to swoole

swoole is PHP's asynchronous, parallel, high-performance network communication engine. Written in pure C language, it provides PHP-language asynchronous multithreaded server, asynchronous TCP/UDP network client, asynchronous MySQL, asynchronous Redis, database connection pool, AsyncTask, message queue, millisecond timer, asynchronous file read-write, asynchronous DNS query.

Swoole has built-in Http/WebSocket server/client and Http2.0 server.

Function library provided by swoole

What functions are provided by swoole to help us achieve what services we use?

http Service, write a simple web server. 
TCP/UDP Service to write a message acceptance processing system.
Asynchronous, requests can be processed asynchronously.
Concurrent, which allows concurrent processing of the same business logic.
socket,socket Communication processing technology.
Millisecond level timer, can be used in php The timer is in use.
Projects, which are more stable and useful than threads.

If you use the above features in your business and php again, you can use swoole to accomplish it. Here's a more specific scenario:

1,Internet
2,mobile communication
3,Enterprise Software
4,cloud computing
5,Network game
5,The Internet of Things ( IOT)
6,Vehicle networking
7,Smart home and other fields

Frame of swoole

Swoft

The first new era PHP high performance collaboration stack framework based on the Wole native protocol, built-in collaboration network server and common collaboration client, resident memory, independent of traditional PHP-FPM,
Fully asynchronous, non-blocking IO implementation, which implements the use of asynchronous clients in a manner similar to that of synchronous clients, without complex asynchronous callbacks, cumbersome yield s, with protocols similar to the Go language, flexible annotations,
Powerful global dependency injection containers, good service governance, flexible and powerful AOP, standard PSR specification implementations, and so on, can be used to build high-performance Web systems, API s, middleware, basic services, and so on.

EasySwoole

EasySwoole is a resident memory-based PHP framework developed by Swoole Server, designed for API s, which breaks away from the performance loss caused by traditional PHP running modes on process wake-up and file loading.

EasySwoole highly encapsulates Swoole Server while maintaining Swoole Server's original features. EasySwoole supports simultaneous monitoring of HTTP, custom TCP, and UDP protocols, allowing developers to write multi-process, asynchronous and highly available application services with minimal learning cost and effort.

SwooleDistributed

The SwooleDistributed old Swoole framework has the most complete development tools and the most powerful features. It was the first SDHelper developer kit and developer debugging command set for unit testing.
Capture client traffic analysis, visualize remote breakpoint debugging, and code coverage detection (swoole is incompatible with xdebug extensions, SDHelper does not require xdebug extensions).
And there are extremely rich built-in components (MQTT-like subscription publishing/Actor model/memory cache/event dispatch/process management/timed tasks/AMQP task scheduling/background monitoring/cluster/micro service/RPC/asynchronous connection pool/custom commands, etc.).
Developers can use it directly to speed up development.

Almost all functions support clustering, and switching from one machine to a cluster does not require any code modifications.

If business development is more complex, such as (game development), then SD framework will be your choice

Environment Setup

Environment: centos7+nginx1.18+php7.4+mysql5.7+swoole4.48

swoole custom installation

[root@localhost /]# cd /root 
[root@localhost root]# tar -xvf swoole-src-4.4.12.tar.gz 
[root@localhost root]# cd swoole-src-4.4.12 
[root@localhost swoole-src-4.4.12]# phpize 
[root@localhost swoole-src-4.4.12]# ./configure 
[root@localhost swoole-src-4.4.12]# make 
[root@localhost swoole-src-4.4.12]# sudo make install

Note!! The following errors tend to occur when executing. /configure

configure: error: Cannot find PHP-config. Please use --with-php-config=PATH

The meaning of the problem is that the configuration file for PHP was not found and additional specification is required as follows

[root@localhost swoole-src-4.4.12]# ./configure --with-php-config=/usr/local/php/bin/php-config

You can find the file address of php-config either by following the command

[root@localhost swoole-src-4.4.12]# find / -name php-config 
/www/server/php/73/src/scripts/php-config 
/www/server/php/73/bin/php-config

So my installation executes

[root@localhost swoole-src-4.4.12]# ./configure --with-php-config=/www/server/php/73/bin/php-config 
[root@localhost swoole-src-4.4.12]# make 
[root@localhost swoole-src-4.4.12]# sudo make install

Configure to php.ini

[root@localhost swoole-src-4.4.12]# find / -name php.ini 
/www/server/php/73/etc/php.ini 
[root@localhost swoole-src-4.4.12]# vi /www/server/php/73/etc/php.ini

Add to

extension=swoole.so

Then detected by php-m
Finally, restart it

[root@localhost swoole-src-4.4.12]# /etc/init.d/php-fpm-73 restart 
Reload service php-fpm done

Initial swoole experience

TCP Service

server side

Click to view the code
use Swoole\Server;

/*$server = new Server("0.0.0.0",9500);

$server->set([
    //Set up process
    'work_num' => 2
]);

//Listen for events
$server->on('connect',function (){
    echo "There is a new connection in ".PHP_EOL;
});

$server->on('message',function (){
    echo "There is a new message ".PHP_EOL;
});

//Close
$server->on('close',function (){
    echo "The server is down! ".PHP_EOL;
});

//Server Open
$server->start();*/

$server = new Server('0.0.0.0', 9503);

$server->on('start', function ($server) {
    echo "TCP Server is started at tcp://127.0.0.1:9503\n";
});

$server->on('connect', function ($server, $fd){
    echo "connection open: {$fd}\n";
});

$server->on('receive', function ($server, $fd, $reactor_id, $data) {
    $server->send($fd, "Swoole: {$data}");
});

$server->on('close', function ($server, $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();

Client

Click to view the code
use Swoole\Client;

//instantiation
$client = new Client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_SYNC);

//Connect
$client->connect('127.0.0.1',9503);

//fd+id = swoole to identify the current client identity
//send message
$client->send("I am a client");

//Client shutdown
$client->close();

Posted by creativodev on Sun, 21 Nov 2021 10:40:38 -0800