laravel切换到swoole问题总结

环境 名称 版本 PHP 7.4.9 Swoole 4.5.2 LaravelS(目前项目用的这个工具) 3.7.8 Laravel Framework [local] 7.26.1 因为切换到swoole之后的问题数量非常多,因此以下简单按问题类型来记录 第一类问题:静态变量 如果一个静态变量参与了.=,+=,*=,/=,-=类似的运算就需要格外小心了,如果处理不当,它的值会不断变得不可预期,应谨慎使用 第二类问题:常量 在某个流程处理完成后,发送event事件通知其他listener前初始化了一个常量 !defined('CONSTANT_NAME') && define('CONSTANT_NAME', 'value'); 用于后续listener(此处是同步的listener)可以直接获取这个常量的值,这在fpm模式下完全正常,因为每次请求完成都会释放资源 在swoole模式下会导致第一次请求之后的其他请求无法重新初始化这个常量,因此需要谨慎使用这种写法 参考解决方法:可通过类成员属性或实时取值的方式代替 调试总结 调试过程中遇到时有时无的问题,先把swoole的dispatch_mode设置为4(ip_hash),保证每次调试请求分配到同一个worker上,方便复现问题 laravel框架切换到swoole之后遇到的常见问题大多是单例引起的(也需要重点检查构造方法,静态变量,全局常量),单例问题可通过每次请求重新注册单例解决;如果无法通过全局配置批量处理单例问题,可使用new代替laravel的App::make,并在使用完实例后及时unset掉

August 28, 2020 · 1 min · 28 words

laravel基于redis的分布式秒杀系统

场景 本文暂不讨论前端页面,cdn在秒杀上的性能优化,只关注从用户请求到达web服务器开始直至秒杀完成在redis中生成订单结束这个阶段的实现,后续还需要使用redis队列异步生成mysql订单实现数据的持久化 实现 为了方便测试结果,当前本地的测试环境如下: web服务器 使用了openresty监听本地的80端口,并代理到3台负载均衡服务器,由负载均衡服务器调用php-fpm实际处理所有请求的业务 nginx.conf中加入 upstream test { server localhost:16888; server localhost:16889; server localhost:16890; } conf.d/default.conf中加入 server { listen 80; server_name localhost; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://test; } } server { listen 16888; server_name localhost; access_log /var/log/nginx/balancer-1.access.log main; error_log /var/log/nginx/balancer-1.error.log warn; root /data/www/community/public; location / { access_by_lua_block{ -- request header方便后台分辨请求来源服务器 ngx.req.set_header('balancer', 'balancer-1') -- response header方便客户端查看当前请求由哪个服务器处理(仅测试用) ngx.header['balancer'] = 'balancer-1' } try_files $uri $uri/ /index.php?$query_string; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; index index.html index.htm index.php; } location ~ \.php$ { fastcgi_pass php72:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { listen 16889; server_name localhost; access_log /var/log/nginx/balancer-2.access.log main; error_log /var/log/nginx/balancer-2.error.log warn; root /data/www/community/public; location / { access_by_lua_block{ ngx.req.set_header('balancer', 'balancer-2') ngx.header['balancer'] = 'balancer-2' } try_files $uri $uri/ /index.php?$query_string; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; index index.html index.htm index.php; } location ~ \.php$ { fastcgi_pass php72:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { listen 16890; server_name localhost; access_log /var/log/nginx/balancer-3.access.log main; error_log /var/log/nginx/balancer-3.error.log warn; root /data/www/community/public; location / { access_by_lua_block{ ngx.req.set_header('balancer', 'balancer-3') ngx.header['balancer'] = 'balancer-3' } try_files $uri $uri/ /index.php?$query_string; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; index index.html index.htm index.php; } location ~ \.php$ { fastcgi_pass php72:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 同时开放本地的16888,16889,16890接口 ...

March 8, 2019 · 2 min · 415 words