Gearmand Usagephp
相關連接mysql
系統版本git
CentOS 2.6.32
備註github
能夠先直接安裝 Gearmand, 看缺什麼再選擇下面的進行安裝!
Install gccsql
yum install gcc-c++
安裝boostbootstrap
wget https://github.com/boostorg/build/archive/2014.10.tar.gz ./bootstrap.sh ./b2 install
安裝qperf測試
yum install gperf.x86_64
安裝libevent libevent-develui
yum -y install libevent libevent-devel
安裝libuuid.net
http://sourceforge.net/projects/libuuid/files/latest/download wget http://downloads.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz tar zxvf libuuid-1.0.3.tar.gz ./configure make & make install
安裝Gearmand
wget https://launchpad.net/gearmand/1.2/1.1.11/+download/gearmand-1.1.11.tar.gz tar zxvf gearmand-1.1.11.tar.gz cd gearmand-1.1.11 ./configure make & make install
安裝gearman的PHP擴展
wget http://pecl.php.net/get/gearman-1.1.2.tgz phpize ./configure make & make install
啓動 Job Server
gearmand -d [root@localhost www]# ps -ef | grep gearmand root 19109 1 0 01:06 ? 00:00:00 /usr/local/sbin/gearmand -d root 22185 3160 0 03:19 pts/1 00:00:00 grep gearmand
命令行使用
[worker] [root@localhost www] gearman -w -f wc -- wc -l [client] [root@localhost www] gearman -f wc < /etc/passwd 26
PHP腳本使用
[worker.php]
<?php $worker = new GearmanWorker(); $worker->addServer(); $worker->addFunction("strtoupper", "my_func"); while ($worker->work()); function my_func($job) { return strtoupper($job->workload()); }
[client.php]
<?php
$client = new GearmanClient(); $client->addServer(); $string = $client->doNormal("strtoupper", "hello world"); echo $string;
[執行]
[root@localhost www]php worker.php [root@localhost www]php client.php HELLO WORLD
Gearmand 使用 Mysql 做爲隊列存儲
[安裝Mysql]
yum install Mysql
[啓動Gearmand]
gearmand -d -q mysql --mysql-host=localhost --mysql-port=3306 --mysql-user=root \ --mysql-password=123456 --mysql-db=test --mysql-table=gearman_queue
[查看數據表]
mysql> show tables; +-------------------+ | Tables_in_gearman | +-------------------+ | gearman_queue | +-------------------+ 1 row in set (0.00 sec)
[測試]
#addTask: gearman -f testqueue -b xx00 mysql> select * from gearman_queue; +--------------------------------------+---------------+----------+------+-------------+ | unique_key | function_name | priority | data | when_to_run | +--------------------------------------+---------------+----------+------+-------------+ | 75f18bac-2f95-11e5-87c3-cb400fa24a6b | testqueue | 1 | xx00 | 0 | +--------------------------------------+---------------+----------+------+-------------+ 1 row in set (0.00 sec) #doJob:gearman -f testqueue -w mysql> select * from gearman_queue; Empty set (0.00 sec)
Done!