您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman常用组件-GlobalData变量共享组件-GlobalDataClient-全部用法在php-fpm环境也可以使用
发布时间:2021-12-12 23:08:17编辑:雪饮阅读()
上文:http://www.gaojiupan.cn/manshenghuo/chengxurensheng/4584.html中并沒有驗證官網所說的可以在php-fpm中也可以使用的情況。
那麽我的理解就是globalData的服務端仍舊可以是命令行,運行著:
[root@localhost workerman]# /usr/local/php734/bin/php -c /usr/local/php734/lib/php/php.ini server.php start
Workerman[server.php] start in DEBUG mode
-------------------------------------------- WORKERMAN ---------------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
--------------------------------------------- WORKERS ----------------------------------------------
proto user worker listen processes status
tcp root globalDataServer frame://127.0.0.1:2207 1 [OK]
----------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
我們這裏要將服務端和客戶端分開,按我所理解的,則服務端要在命令行中啓動的。
而客戶端就像是常見http後端可以直接網頁訪問的那種傳統架構。
那麽現在客戶端脚本修改如:
<?php
require_once __DIR__ . '/vendor/autoload.php';
// 初始化一个全局的global data client
$global = new \GlobalData\Client('127.0.0.1:2207');
echo "<br/> isset: <br/>";
var_export(isset($global->abc));
$global->abc = array(1,2,3);
echo "<br/> isset: <br/>";
var_export(isset($global->abc));
echo "<br/> abc: <br/>";
var_export($global->abc);
unset($global->abc);
echo "<br/> isset: <br/>";
var_export(isset($global->abc));
echo "<br/> abc add: <br/>";
var_export($global->add('abc', 10));
echo "<br/> abc add after value: <br/>";
var_export($global->abc);
echo "<br/> abc increment: <br/>";
var_export($global->increment('abc', 2));
echo "<br/> abc increment after value: <br/>";
var_export($global->abc);
echo "<br/> abc cas: <br/>";
//CAS是单词compare and set的缩写,意思是指在set之前先比较该值有没有变化(原子),只有在没变的情况下才对其赋值。
var_export($global->cas('abc', 12, 18));
echo "<br/> abc cas after value: <br/>";
var_export($global->abc);
?>
然後就是說這個脚本連同其所在目錄其它文件全部複製到php-fpm的站點中(php-fpm一般是和nginx結合的,這裏以寶塔環境爲例。)
[root@localhost workerman]# ls -l /www/wwwroot/www.fpm.com/
total 72
-rwxr-xr-x 1 www www 479 Dec 12 22:50 404.html
-rwxr--r-- 1 root root 1103 Nov 29 22:15 ca.crt
-rwxr--r-- 1 root root 963 Nov 29 22:13 ca.key
-rwxr--r-- 1 root root 2267 Nov 28 18:59 client.php
-rwxr--r-- 1 root root 148 Dec 12 18:36 composer.json
-rwxr--r-- 1 root root 9195 Dec 12 18:36 composer.lock
-rwxr-xr-x 1 www www 917 Dec 12 22:50 index.html
drwxr-xr-x 2 root root 32 Dec 12 22:53 Protocols
-rwxr--r-- 1 root root 944 Apr 6 2012 server.crt
-rwxr--r-- 1 root root 785 Nov 29 22:16 server.csr
-rwxr--r-- 1 root root 887 Apr 6 2012 server.key
-rwxr--r-- 1 root root 175 Dec 12 19:35 server.php
-rwxr--r-- 1 root root 1047 Dec 12 23:03 start.php
-rwxr--r-- 1 root root 1364 Nov 28 16:29 telnet.php
-rwxr--r-- 1 root root 1013 Nov 28 19:55 textclient.php
-rwxr--r-- 1 root root 412 Nov 29 21:56 udpClient.php
drwxr-xr-x 5 root root 70 Dec 12 22:53 vendor
-rwxr--r-- 1 root root 1303 Nov 29 22:38 websocket.php
[root@localhost workerman]# tree /www/wwwroot/www.fpm.com/
/www/wwwroot/www.fpm.com/
├── 404.html
├── ca.crt
├── ca.key
├── client.php
├── composer.json
├── composer.lock
├── index.html
├── Protocols
│ └── MyTextProtocol.php
├── server.crt
├── server.csr
├── server.key
├── server.php
├── start.php
├── telnet.php
├── textclient.php
├── udpClient.php
├── vendor
│ ├── autoload.php
│ ├── composer
│ │ ├── autoload_classmap.php
│ │ ├── autoload_files.php
│ │ ├── autoload_namespaces.php
│ │ ├── autoload_psr4.php
│ │ ├── autoload_real.php
│ │ ├── autoload_static.php
│ │ ├── ClassLoader.php
│ │ ├── installed.json
│ │ ├── installed.php
│ │ ├── InstalledVersions.php
│ │ ├── LICENSE
│ │ └── platform_check.php
│ ├── psr
│ │ └── http-message
│ │ ├── CHANGELOG.md
│ │ ├── composer.json
│ │ ├── LICENSE
│ │ ├── README.md
│ │ └── src
│ │ ├── MessageInterface.php
│ │ ├── RequestInterface.php
│ │ ├── ResponseInterface.php
│ │ ├── ServerRequestInterface.php
│ │ ├── StreamInterface.php
│ │ ├── UploadedFileInterface.php
│ │ └── UriInterface.php
│ └── workerman
│ ├── globaldata
│ │ ├── composer.json
│ │ ├── README.md
│ │ ├── src
│ │ │ ├── Client.php
│ │ │ └── Server.php
│ │ └── test
│ │ ├── bench.php
│ │ ├── start.php
│ │ └── test.php
│ ├── http-client
│ │ ├── composer.json
│ │ ├── README.md
│ │ └── src
│ │ ├── Client.php
│ │ ├── ConnectionPool.php
│ │ ├── Emitter.php
│ │ ├── Request.php
│ │ └── Response.php
│ ├── psr7
│ │ ├── CHANGELOG.md
│ │ ├── composer.json
│ │ ├── LICENSE
│ │ ├── README.md
│ │ └── src
│ │ ├── AppendStream.php
│ │ ├── BufferStream.php
│ │ ├── CachingStream.php
│ │ ├── DroppingStream.php
│ │ ├── FnStream.php
│ │ ├── functions_include.php
│ │ ├── functions.php
│ │ ├── InflateStream.php
│ │ ├── LazyOpenStream.php
│ │ ├── LimitStream.php
│ │ ├── MessageTrait.php
│ │ ├── MultipartStream.php
│ │ ├── NoSeekStream.php
│ │ ├── PumpStream.php
│ │ ├── Request.php
│ │ ├── Response.php
│ │ ├── Rfc7230.php
│ │ ├── ServerRequest.php
│ │ ├── StreamDecoratorTrait.php
│ │ ├── Stream.php
│ │ ├── StreamWrapper.php
│ │ ├── UploadedFile.php
│ │ ├── UriNormalizer.php
│ │ ├── Uri.php
│ │ └── UriResolver.php
│ ├── workerman
│ │ ├── Autoloader.php
│ │ ├── composer.json
│ │ ├── Connection
│ │ │ ├── AsyncTcpConnection.php
│ │ │ ├── AsyncUdpConnection.php
│ │ │ ├── ConnectionInterface.php
│ │ │ ├── TcpConnection.php
│ │ │ └── UdpConnection.php
│ │ ├── Events
│ │ │ ├── EventInterface.php
│ │ │ ├── Event.php
│ │ │ ├── Ev.php
│ │ │ ├── Libevent.php
│ │ │ ├── React
│ │ │ │ ├── Base.php
│ │ │ │ ├── ExtEventLoop.php
│ │ │ │ ├── ExtLibEventLoop.php
│ │ │ │ └── StreamSelectLoop.php
│ │ │ ├── Select.php
│ │ │ └── Swoole.php
│ │ ├── Lib
│ │ │ ├── Constants.php
│ │ │ └── Timer.php
│ │ ├── MIT-LICENSE.txt
│ │ ├── Protocols
│ │ │ ├── BinaryTransfer.php
│ │ │ ├── Frame.php
│ │ │ ├── Http
│ │ │ │ ├── Chunk.php
│ │ │ │ ├── mime.types
│ │ │ │ ├── Request.php
│ │ │ │ ├── Response.php
│ │ │ │ ├── ServerSentEvents.php
│ │ │ │ ├── Session
│ │ │ │ │ ├── FileSessionHandler.php
│ │ │ │ │ └── RedisSessionHandler.php
│ │ │ │ └── Session.php
│ │ │ ├── Http.php
│ │ │ ├── JsonInt.php
│ │ │ ├── JsonNL.php
│ │ │ ├── ProtocolInterface.php
│ │ │ ├── Text.php
│ │ │ ├── TextTransfer.php
│ │ │ ├── Websocket.php
│ │ │ ├── Ws.php
│ │ │ └── XmlProtocol.php
│ │ ├── README.md
│ │ ├── Timer.php
│ │ └── Worker.php
│ └── workerman.log
└── websocket.php
22 directories, 127 files
那麽最後客戶端訪問如:
可以看到這樣確實證實了官網說的在php-fpm的環境中也是可以運行的,也是ok的。
关键字词:GlobalData,workerman,GlobalDataClient,php-fpm,客戶端,服務端,變量,共享