使用LaravelS (laravel+swoole)

LaravelS是一个胶水项目,用于快速集成Swoole到Laravel或Lumen,然后赋予它们更好的性能、更多可能性。

使用LaravelS (laravel+swoole)

LaravelS是一个胶水项目,用于快速集成Swoole到Laravel或Lumen,然后赋予它们更好的性能、更多可能性。

在使用laravels之前,必须先装上swoole的扩展。

swoole文档:https://wiki.swoole.com/wiki/index/prid-1

laraves文档:https://github.com/hhxsv5/laravel-s

环境:在VMware虚拟机中自己搭的环境,Centos7,PHP7.2.12,Nginx1.14.2,MySQL 5.5.60。

安装swoole扩展

1、直接 pecl install swoole ,会报错,因为装有php老版本的依赖,所以要装新的

 yum install -y php72w-mysqlnd php72w-fpm php70w-pecl-redis php72w-common php72w-pdo php72w php72w-gd php72w-gd php72w-cli php72w-mbstring php72w-devel

因为我是PHP7.2,如果你是PHP7.0,将上面的php72替换为php70即可。

2、如果报错:PHP Parse error: syntax error, unexpected //'new' (T_NEW) in /usr/share/pear/PEAR/Frontend.php on //line 91

安装PECL
//php版本 > 7
$ wget http://pear.php.net/go-pear.phar
$ php go-pear.phar

//php版本 < 7
$ yum install php-pear

3、安装swoole包

1.pecl install swoole
2.WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update.
需要更新:pecl channel-update pecl.php.net
3.查找下php.ini的位置:php -i |grep php.ini
4.然后在php.ini中增加:extension=swoole.so
5.重启下php:systemctl restart php
6.查看下是否已经成功加载了swoole:php -m | grep swoole

如果还没装上,推荐参考:https://www.cnblogs.com/phpwechat/p/8417331.html

使用LaravelS

1、安装laravels

composer require "hhxsv5/laravel-s:~3.3" -vvv

2、注册Service Provider config/app.php

'providers' => [
    //...
    Hhxsv5\LaravelS\Illuminate\LaravelSServiceProvider::class,
],

3、发布配置和二进制文件

php artisan laravels publish

4、修改配置config/laravels.php:监听的IP、端口等。

5、配置web.confg文件,我的是这样

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
upstream laravels {
    # 通过 IP:Port 连接
    server 127.0.0.1:5200 weight=5 max_fails=3 fail_timeout=30s;
    # 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能
    #server unix:/xxxpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s;
    #server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s;
    #server 192.168.1.2:5200 backup;
    keepalive 16;
}
server {
    listen 80;
    # 别忘了绑Host哟
    server_name laras.test;
    root /var/www/laras/public;
    #access_log /yyypath/log/nginx/$server_name.access.log  main;
    autoindex off;
    index index.html index.htm;
    # Nginx处理静态资源(建议开启gzip),LaravelS处理动态资源。
    location / {
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string;
        if (!-e $request_filename){  
            rewrite ^/(.*) /index.php last;  
        } 
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|bmp)$ {  
        access_log off;  
        root /var/www/laras/public;  
        expires 30d;  
        break; }
    # 当请求PHP文件时直接响应404,防止暴露public/*.php
    #location ~* \.php$ {
    #    return 404;
    #}
    # Http和WebSocket共存,Nginx通过location区分
    # !!! WebSocket连接时路径为/ws
    # Javascript: var ws = new WebSocket("ws://laravels.com/ws");
    location =/ws {
        proxy_http_version 1.1;
        # proxy_connect_timeout 60s;
        # proxy_send_timeout 60s;
        # proxy_read_timeout:如果60秒内被代理的服务器没有响应数据给Nginx,那么Nginx会关闭当前连接;同时,Swoole的心跳设置也会影响连接的关闭
        # proxy_read_timeout 60s;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header Server-Protocol $server_protocol;
        proxy_set_header Server-Name $server_name;
        proxy_set_header Server-Addr $server_addr;
        proxy_set_header Server-Port $server_port;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass http://laravels;
    }
    location @laravels {
        proxy_http_version 1.1;
        # proxy_connect_timeout 60s;
        # proxy_send_timeout 60s;
        # proxy_read_timeout 60s;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header Server-Protocol $server_protocol;
        proxy_set_header Server-Name $server_name;
        proxy_set_header Server-Addr $server_addr;
        proxy_set_header Server-Port $server_port;
        proxy_set_header Connection "";
        proxy_pass http://laravels;
    }
}

6、毫秒级定时任务 App\Jobs\Timer\TestCronJob

如laravels文档 我的是这样 仅用于测试
public function run()
{
    $this->client_send('123456789');
    Test::create(['num'=>'66']);
}
//namespace App\Services\WebSocketClientService
public function client_send($msg,$url='127.0.0.1',$port=5200)
{
        $client = new WebSocketClientService($url, $port);
        $client->connect();
        $client->send($msg);
         /*$data = @$client->recv();
        \Log::info($data);*/
        $client->close();
}

7、启用WebSocket服务器 App\Services\WebSocketService

如laravels文档 
https://github.com/hhxsv5/laravel-s/blob/master/README-CN.md#%E5%90%AF%E7%94%A8websocket%E6%9C%8D%E5%8A%A1%E5%99%A8

8、视图页面开启websocket

<script>
    var ws = new WebSocket("ws://"+window.location.host+"/ws?c="+"{{ csrf_token() }}");
    //成功连接websoc的时候
    ws.onopen = function(){
        console.log('ws onopen');
//        ws.send('client msg');
    };
    //连接错误的时候
    ws.onerror = function(){
        console.log('ws onerror');
    };
    ws.onmessage = function (data) {
        console.log(data);
    };
</script>

9、开启服务

重启nginx和php

systemctl restart nginx systemctl restart php-fpm

开启laravels

php bin/laravels starts

⬆️

写的不错,赞助一下主机费

扫一扫,用支付宝赞赏
扫一扫,用微信赞赏
2019-02-15 14:46

报错PHP Warning: PHP Startup: Unable to load dynamic library 'swoole.so'
解决:插件加载顺序的原因,应该把extension=swoole.so加到socket的加载后面,在php7.2中进入/etc/php.d下,在sockets.ini中将extension=swoole.so加到extension=sockets.so下面,后重启php-fpm