CentOS的Gearman安裝

背景:用PHP作一些簡單的上傳是沒有任何的問題,可是要作斷點上傳好像也是沒有大問題,但要是併發的切片加斷點上傳可能就會有問題了喲。
第一個問題是合併問題:若是一上傳就合併,PHP老半天不返回是一個方面(是PHP超時仍是Nginx超時?),這樣勢必會形成客戶端因沒返回,無法再啓動新的上傳進程,若是用php的追加寫文件內存太大還會退出的可能,用上gearmand後出現異步同時寫,可能加快了傳輸效率的同時IO可能會比較密集(假如同一時間併發上傳的人多的狀況。)。
第二個問題是前後問題:若是併發時出現了傳一半時最後一片先上傳上來了怎麼辦?若是直接追加進文件是會不按順序出錯的,若是放在那兒無論,那啥時候管,這也是一個問題。
第三個問題是鎖的問題:併發時若是同時兩片到了,像PHP這種如何去結合鎖的功能呢?據我所知有memcache的add函數是原子操做,能夠利用,那就設計到鎖多久,是否是又回到第一個問題,返回時間得浪費多長時間呢。
基於上面三點,看來PHP作這活不是太靠譜,因而引出了gearman這個模型,至關於異步處理,再集合PHP的memcache鎖(memcache放內存,可能得用ttserver才行,協議同樣的。),PHP上傳邏輯處理,Nginx上傳插件,纔有可能作一個較爲靠譜且穩定的分片,斷點加多進程(多線程)上傳服務搭建。

1、從源碼安裝gearmand遇到的各類外部代碼版本及yum版本過低的致使各類編譯不過的問題,歷程至關的麻煩,特別是boost和這個libevent默認的yum install很是低,都給編譯了,把rpm所強制刪除了,最後才成功,再就是g++編譯版本,得export後才能編譯,不然一堆問題,都在下面有描寫。
出現第一個問題及處理辦法:
libgearman-server/plugins/queue/sqlite/instance.cc: In member function 'bool gearmand::queue::Instance::_sqlite_prepare(const std::string&, sqlite3_stmt**)':
libgearman-server/plugins/queue/sqlite/instance.cc:125: error: 'sqlite3_prepare_v2' was not declared in this scope
libgearman-server/plugins/queue/sqlite/instance.cc: In member function 'gearmand_error_t gearmand::queue::Instance::init()':
libgearman-server/plugins/queue/sqlite/instance.cc:224: error: 'SQLITE_OPEN_READWRITE' was not declared in this scope
libgearman-server/plugins/queue/sqlite/instance.cc:224: error: 'SQLITE_OPEN_CREATE' was not declared in this scope
libgearman-server/plugins/queue/sqlite/instance.cc:224: error: 'sqlite3_open_v2' was not declared in this scope
make[1]: *** [libgearman-server/plugins/queue/sqlite/libgearman_server_libgearman_server_la-instance.lo] Error 1
將隊列存放在sqlite3或postgresql。這貨是用的sqlite存隊列啊:
yum search sqlite
yum install sqlite.x86_64 sqlite-devel.x86_64
仍是不行,參考:
安裝:https://justwinit.cn/post/7709/ 後,
最後在編譯Gearman時帶上 --with-sqlite3=/usr/local/sqlite3,告訴編譯器應該使用這個新的sqlite便可。


出現第二個問題及處理辦法:
./libgearman-1.0/gearman.h:53:23: error: cinttypes: No such file or directory
命令:
yum install gcc44 gcc44-c++ libstdc++44-devel -y
而後在環境變量里加入:
export CC=/usr/bin/gcc44 or export CC=/usr/bin/gcc
export CXX=/usr/bin/g++44
保存退出後執行:
source /etc/profile
刪除gearmand-0.34文件夾從新進行編譯.
從新進行編譯後執行make這步......
在後面有詳細的說明,能夠不source直接設置環境變量,由於編譯後也不必定要這個版本的gcc的。



出現第三個問題的處理辦法:
ibgearman-server/plugins/queue/mysql/queue.cc:430: error: 'mysql_error' was not declared in this scope
libgearman-server/plugins/queue/mysql/queue.cc: In function 'gearmand_error_t _mysql_queue_replay(gearman_server_st*, void*, gearmand_error_t (*)(gearman_server_st*, void*, const char*, size_t, const char*, size_t, const void*, size_t, gearman_job_priority_t, int64_t), void*)':
libgearman-server/plugins/queue/mysql/queue.cc:446: error: 'MYSQL_RES' was not declared in this scope
libgearman-server/plugins/queue/mysql/queue.cc:446: error: 'result' was not declared in this scope
libgearman-server/plugins/queue/mysql/queue.cc:447: error: 'MYSQL_ROW' was not declared in this scope

[root@test gearmand-1.1.12]# ./configure --help|grep mysql
  --with-mysql=[ARG]      use MySQL client library [default=yes], optionally
                          specify path to mysql_config
              Full path to mysql_config program

看樣子是想用mysql作隊列queue的:


加上仍是報錯,去了得了,不用mysql作隊列,有sqlite足夠了,
--without-mysql就行了,來自:
https://bugs.launchpad.net/gearmand/+bug/1327038 說的:


出現第四個問題的解決辦法:
/home/xiangdong/software/gearmand-1.1.12/bin/gearadmin.cc:129: undefined reference to `boost::program_options::options_description::m_default_line_length'
/home/xiangdong/software/gearmand-1.1.12/bin/gearadmin.cc:129: undefined reference to `boost::program_options::options_description::options_description(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)'
bin/gearadmin.o: In function `boost::program_options::basic_command_line_parser<char>::run()':
/usr/local/include/boost/program_options/detail/parsers.hpp:107: undefined reference to `boost::program_options::detail::cmdline::get_canonical_option_prefix()'
bin/gearadmin.o: In function `boost::program_options::basic_command_line_parser<char>::extra_parser(boost::function1<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&>)':
/usr/local/include/boost/program_options/detail/parsers.hpp:77: undefined reference to `boost::program_options::detail::cmdline::set_additional_parser(boost::function1<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&>)'
collect2: ld returned 1 exit status
make[1]: *** [bin/gearadmin] Error 1
make[1]: Leaving directory `/home/xiangdong/software/gearmand-1.1.12'


[root@test gearmand-1.1.12]# ./configure --help|grep boost
  --with-boost[=ARG]      use Boost library from a standard location
  --with-boost-libdir=LIB_DIR
                          Force given directory for boost libraries. Note that
                          fails and you know exactly where your boost
  --with-boost-program-options[=special-lib]
                          use the program options library from boost - it is
                          --with-boost-program-options=boost_program_options-gcc-mt-1_33_1

指定boost的庫:
從這兒得知在這兒呢:  http://justwinit.cn/post/7706/
系統默認會將include拷貝到/usr/local/include/boost/中
將lib拷貝到/usr/local/lib下
yum install boost-devel  [實踐失敗]
回頭瞭解下boost的結構,rpm包是啥樣的,以下所示,版本號1.33,有多是由於lddconfig裏默認配置指向這個舊版本了,以下:
vi /etc/ld.so.conf


[root@test ~]# rpm -ql boost-1.33.1-16.el5_9
/usr/lib64/*.so.*  //各類so文件
/usr/lib/*.so.*     //各類so文件
rpm -ql boost-devel-1.33.1-16.el5_9
/usr/include/boost  裏面是各類分類目錄,裏面是hpp頭文件
algorithm/
archive/
assign/
bind/
compatibility
config/
devel包裏還有靜態a文件及動態so文件:
/usr/lib/libboost_test_exec_monitor.a
/usr/lib/libboost_test_exec_monitor.so

以下實踐試試:
cd /home/xiangdong/software/boost_1_57_0
./bootstrap.sh --prefix=/usr/local/boost-1.57
vi tools/build/v2/user-config.jam
文件尾部增長(沒找到,直接./b2 ./b2 install 便可!):
using mpi  
./b2
./b2 install


Boost headers version >= 1.39.0
[root@localdomain gearmand-1.1.12]# yum search boost
[root@localdomain gearmand-1.1.12]# yum install boost.x86_64
[root@localdomain gearmand-1.1.12]# yum install yum install boost-devel.x86_64
舊的yum源,直接乾死,用源碼裝個試試:



出現第五個問題的解決辦法:
/home/xiangdong/software/gearmand-1.1.12/libgearman-server/gearmand_con.cc:677: undefined reference to `event_initialized'
collect2: ld returned 1 exit status
libevent版本太老了,更新新的版本:
安裝最新版本的libevent,並執行如下操做:(備份老版本的/usr/lib64/libevent.so)
ln -s /usr/lib64/libevent-2.0.so.5 /usr/lib64/libevent.so
刪除老舊的libevent的rpm包後,安裝最新的方法實踐以下:
[root@test software]# cp  /usr/lib64/libevent.so /usr/lib64/libevent.so.bak.old
[root@test libevent-2.0.21-stable]# tar -zxvf libevent-2.0.21-stable.tar.gz
[root@test libevent-2.0.21-stable]# cd libevent-2.0.21-stable
[root@test libevent-2.0.21-stable]# ./configure --prefix=/usr/local/libevent-2.0.21-stable


ls -al /usr/lib | grep libevent
rpm -qa|grep libevent
libevent-1.4.13-1
libevent-devel-1.4.13-1
libevent-devel-1.4.13-1
libevent-1.4.13-1

rpm -e --allmatches --nodeps libevent-1.4.13-1 libevent-devel-1.4.13-1
[root@test libevent-2.0.21-stable]# rpm -e --allmatches --nodeps libevent-1.4.13-1 libevent-devel-1.4.13-1
[root@test libevent-2.0.21-stable]#

卸載後看還有沒:
[root@test libevent-2.0.21-stable]# ls -al /usr/lib | grep libevent  
lrwxrwxrwx   1 root root       31 Dec 30 17:17 libevent-1.1a.so.1 -> /usr/lib/libevent-1.1a.so.1.0.2
-rw-r--r--   1 root root    31596 Jan  7  2007 libevent-1.1a.so.1.0.2

rm -Rf  /usr/lib/libevent-1.1a.so.1.0.2 /usr/lib/libevent-1.1a.so.1 //刪除乾淨
[root@test libevent-2.0.21-stable]# rm -Rf  /usr/lib/libevent-1.1a.so.1.0.2 /usr/lib/libevent-1.1a.so.1
[root@test libevent-2.0.21-stable]#

cd /home/xiangdong/software/libevent-2.0.21-stable  //最新安裝包
./configure --prefix=/usr   //libevent會安裝到 /usr/lib 或 /usr/local/lib 下
#./configure --prefix=/usr/local/libevent-2.0.21-stable
[root@test libevent-2.0.21-stable]# ./configure --prefix=/usr && make && make install
測試libevent是否安裝成功:ls -al /usr/lib | grep libevent(或 ls -al /usr/local/lib | grep libevent)
ls -al /usr/lib | grep libevent
lrwxrwxrwx   1 root root       21 Dec 31 09:44 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rwxr-xr-x   1 root root  1065819 Dec 31 09:44 libevent-2.0.so.5.1.9
-rw-r--r--   1 root root  1678970 Dec 31 09:44 libevent.a
lrwxrwxrwx   1 root root       26 Dec 31 09:44 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9

若是libevent的安裝目錄爲/usr/local/lib下,則還須要創建 libevent-1.4.so.2 到 /usr/lib 的軟鏈接,這樣其餘程序運行時才能找到libevent庫:ln -s /usr/local/lib/libevent-1.4.so.2  /usr/lib/libevent-1.4.so.2

ls /usr/lib/libevent-2.0.so.5
/usr/lib/libevent-2.0.so.5

幹掉舊的libevent的殘留的動態so文件:
ls -al /usr/lib64 | grep libevent
lrwxrwxrwx   1 root root       19 Dec 31 09:40 libevent-1.4.so.2 -> libevent.so.bak.old
-rwxr-xr-x   1 root root   104296 Dec 31 09:22 libevent.so.bak.old
[root@test libevent-2.0.21-stable]# rm -Rf /usr/lib64/libevent-1.4.so.2
參考來自:http://blog.sina.com.cn/s/blog_4b93170a0100mbm9.html

編譯成功結果以下:


啓動gearmand的方法:
[root@test gearmand-1.1.12]# whereis gearmand
gearmand: /usr/local/sbin/gearmand
[root@test ~]# mkdir -p /usr/local/gearman/log

ls /usr/local/sqlite3/bin/
sqlite3

*特別提醒,(mysql性能上可能在作隊列上並不比sqlite強到哪兒去)
使用sqlite更加簡單方便 :
/usr/local/sbin/gearmand -l /usr/local/gearman/log/trace2.log --verbose INFO -p 4830 -q libsqlite3 --libsqlite3-db /usr/local/sqlite/bin/gearman --libsqlite3-table gearman_queue -d
很遺憾,我到如今也沒能編譯出對MySQL的持久化

[root@test ~]# /usr/local/sbin/gearmand -l /usr/local/gearman/log/trace2.log --verbose INFO -p 4830 -q libsqlite3 --libsqlite3-db /usr/local/sqlite/bin/gearman --libsqlite3-table gearman_queue -d
/usr/local/sbin/gearmand: Error while initializing the queue : libsqlite3

原來是sqlite的位置不對,修改路徑後就OK了:

啓動成功:
[root@test sqlite3]# /usr/local/sbin/gearmand -l /usr/local/gearman/log/trace2.log --verbose INFO -p 4830 -q libsqlite3 --libsqlite3-db /usr/local/sqlite3/bin/gearman --libsqlite3-table gearman_queue -d
[root@test sqlite3]#
端口存在了,說明是真的啓動成功了:
[root@test sqlite3]# netstat -atlunp|grep 4830
tcp        0      0 0.0.0.0:4830                0.0.0.0:*                   LISTEN      17820/gearmand      
tcp        0      0 :::4830                     :::*                        LISTEN      17820/gearmand  

也就是說gearmand啓動時候本身創建一個sqlite的表,以下:
[root@test sqlite3]# cat /usr/local/sqlite3/bin/gearman
SQLite format 3@  ''blegearman_queuegearman_queueCREATE TABLE gearman_queue ( unique_key TEXT, function_name TEXT, priority INTEGER, data BLOB, when_to_run INTEGER, PRIMARY KEY (unique_key, function_name))9M'indexsqlite_autoindex_gearman_queue_1gearman_queue

通過一陳折騰,說明這個gearmand對boost庫的要求高,對libevent的版本也要求高,不是能通過yum install能解決得了的,得本身安裝後指定,再就是對編譯器的版本也是有要求的,也就是說相對來講其安裝比較苛刻一些,如Lamp架構相比較的話,難度要高一些。
——————————————————————————————————————————————————————————————
總之,一大堆問題,這貨竟然還這麼流行據說Gearman最初用於LiveJournal的圖片resize功能,因爲圖片resize須要消耗大量計算資 源,所以須要調度到後端多臺服務器執行,完成任務以後返回前端再呈現到界面。,看來廣大分發系統還真得靠它啊,其它各類小問題不斷,參考:
http://www.111cn.net/sys/CentOS/65334.htm

2、PHP的gearmand擴展編譯及遇到問題解決實踐以下:
PHP的gearmand折騰擴展,這塊試了好幾個版本的擴展要麼configure不過去,要麼是lib裏卻啥,但最後仍是以當前最新的版本gearman-1.1.2.tgz給解決了:

./configure --with-php-config=/usr/local/php/bin/php-config
出現以下所示的配置錯誤:
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
checking for gawk... gawk
checking whether to enable gearman support... yes, shared
found in /usr/local
checking for gearman_create in -lgearman... no
configure: error: wrong libgearman version or lib not found

問題一:
gearman的PHP擴展時出現configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.警告信息。
警告信息說明須要安裝 re2c
執行下面命令安裝:
wget http://sourceforge.net/projects/re2c/files/re2c/0.13.5/re2c-0.13.5.tar.gz/download
2014-12-31 11:01:59 (999 KB/s) - `re2c-0.13.5.tar.gz' saved [782725/782725]
tar -zxvf re2c-0.13.5.tar.gz
cd re2c-0.13.5
./configure && make && make install //安裝成功,警告獲得瞭解決,第二個依舊存在。
它的意思是好像沒有找到lib,不對是找到了沒有找到裏面的某個叫gearman_create的函數,這個就奇怪了,找下:
[root@test /]# find . -name "*gearman*.so*"
./usr/local/lib/libgearman.so.8.0.0
./usr/local/lib/libgearman.so
./usr/local/lib/libgearman.so.8

[root@test /]# vi /etc/ld.so.conf
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib                                                                                                                                                                                            
/usr/lib64
/usr/local/lib
/usr/local/lib
有這個位置的啊。
[root@test /]# ldconfig  //從新lib的位置載入。

[root@test gearman-0.4.0]# phpize
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
[root@test gearman-0.4.0]# ./configure --with-php-config=/usr/local/php/bin/php-config

問題依舊,去官網看一下唄,http://gearman.org/download/:
文字摘錄:PHP
There are two PHP client/worker libraries, one which is a pure PHP extension and one which wraps the libgearman C library.
Gearman Extension
A PHP extension that wraps the libgearman C library.
Gearman PHP Extension (1.0.2) (Source)
官方的PHP擴展高V1.0.2,我這個是gearman-0.4.0太舊了,好像還有更高的gearman-1.1.2.tgz,http://pecl.php.net/package/gearman,放上官網這個上去從新試下了:
tar -zxvf gearman-1.0.2.tgz
cd gearman-1.0.2
[root@test gearman-1.0.2]# phpize
./configure --with-php-config=/usr/local/php/bin/php-config   //這個沒有問題
[root@test gearman-1.0.2]# make && make install
/home/xiangdong/software/gearman-1.0.2/php_gearman.c: In function ‘zm_startup_gearman’:
/home/xiangdong/software/gearman-1.0.2/php_gearman.c:4638: error: ‘GEARMAN_MAGIC_TEXT’ undeclared (first use in this function)
/home/xiangdong/software/gearman-1.0.2/php_gearman.c:4638: error: (Each undeclared identifier is reported only once
仍是差啥麼子玩意,換個更新的看下,gearman-1.1.2.tgz 實踐OK了,以下:
Libraries have been installed in:
   /home/xiangdong/software/gearman-1.1.2/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `LD_LIBRARY_PATH' environment variable
     during execution
   - add LIBDIR to the `LD_RUN_PATH' environment variable
     during linking
   - use the `-Wl,--rpath -Wl,LIBDIR' linker flag
   - have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

[root@test gearman-1.1.2]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
真在的:
ls  /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/gearman.so
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/gearman.so
在php.ini里加上這個擴展就成了。Successful....EOF


[root@test gearman-1.1.2]# php -v
PHP 5.3.10 (cli) (built: Feb  3 2012 14:04:56)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    with eAccelerator v0.9.6.1, Copyright (c) 2004-2010 eAccelerator, by eAccelerator

[root@test gearman-1.1.2]# vi /usr/local/php/etc/php.ini
extension = "gearman.so"  

[root@test gearman-1.1.2]# php -m|grep gearman
gearman



3、代碼編寫task的分發及client的及簡單代碼實踐OK及其狀況實踐Ok:
cd /usr/local/gearman/
[root@test gearman]# mkdir codesTest
[root@test gearman]# cd codesTest/
[root@test codesTest]# vi myworker.php


以守護進程方式啓動worker:
# nohup php myworker.php >/dev/null 2>&1 &
[root@test codesTest]# nohup php myworker.php >/dev/null 2>&1 &
[1] 31238

一個發送任務處理請求的client:
vi  myclient.php
[root@test codesTest]# vi  myclient.php


運行myclient.php:
[root@test codesTest]# php myclient.php "Jose"
hello, Jose
H:test.local:2

一個發出並行處理任務請求的例子tasksclient:
[root@test codesTest]# vi  tasksclient.php


運行tasksclient:
[root@test codesTest]# php tasksclient.php
Completed task:: id :2 , handled result:hello, John
Completed task:: id :1 , handled result:hello, Jose

查看worker及隊列狀況:
(echo "workers"; sleep 0.1) | nc 192.168.109.8 4830
[root@test codesTest]#  (echo "workers"; sleep 0.1) | nc 192.168.109.8 4830
34 192.168.109.8 - : sayhello logMsg
35 192.168.109.8 - :
.
查看當前隊列狀況(顯示格式FUNCTION\tTOTAL\tRUNNING\tAVAILABLE_WORKERS):
(echo "status"; sleep 0.1) | nc 192.168.109.8 4830
[root@test codesTest]# (echo "status"; sleep 0.1) | nc 192.168.109.8 4830
sayhello        0       0       1
logMsg  0       0       1
.

監控php的守護進程(沒實踐), 能夠開多個,並同時監控:
關於Worker守護進程的啓動和監控
上面例子中直接啓動worker腳本做爲守護進程,沒法監控到worker進程是否存活.
使用Unix進程監控supervisord則可輕鬆解決這個問題.
將以下配置添加到supervisord的配置中,便可實現由supervisord來啓動和監控myworker.
編輯配置文件 vi /etc/supervisord.conf
[program:myworker]
command=/usr/local/php5415/bin/php myworker.php
process_name=%(program_name)s_%(process_num)02d
;修改numprocs便可同時開啓多個worker進程
numprocs=1
directory=/usr/local/gearman/codesTest
autostart=true
autorestart=true
user=gearmand
stopsignal=KILL
Posted in Daemon / Worker, Gearman.
Tagged gearman.

監控這塊看:
http://jingyan.baidu.com/article/375c8e198d1b1425f2a2290c.html

上面這塊的PHP擴展代碼及實踐來自:
http://www.51itstudy.com/30114.html
http://codego.net/384693/

_________下面是摘錄別的文章不必定本身配置時可徹底過得過,得按本身環境做配置安裝,只是用做參考!_______
Gearman的目的在於用PHP擴展分發,因而PHP擴展如何安裝:
tar zxvf gearman-1.1.1.tgz
cd gearman-1.1.1
/opt/local/php/bin/phpize
./configure --with-php-config=/opt/local/php/bin/php-config --with-gearman
make
make install
編輯 php.ini
vi php.ini
增長
extension = "gearman.so"
重啓php

啓動gearmand 服務
gearmand -L 10.6.0.6 -p 4730  -u root -l /var/log/gearmand.log -d
其餘參數請  gearmand --help

摘自:http://jicki.blog.51cto.com/1323993/1177487

一般,多語言多系統之間的集成是個大問題,通常來講,人們多半會採用WebService的方式來處理此類集成問題,但無論採用何種風格的WebService,如RPC風格,或者REST風格,其自己都有必定的複雜性。相比之下,Gearman也能實現相似的做用,並且更簡單易用。

一個Gearman請求的處理過程涉及三個角色:Client -> Job -> Worker。

Client:請求的發起者,能夠是C,PHP,Perl,MySQL UDF等等。
Job:請求的調度者,用來負責協調把Client發出的請求轉發給合適的Work。
Worker:請求的處理者,能夠是C,PHP,Perl等等。

由於Client,Worker並不限制用同樣的語言,因此有利於多語言多系統之間的集成。

甚至咱們經過增長更多的Worker,能夠很方便的實現應用程序的分佈式負載均衡架構。

下面看看如何安裝運行一個例子,條件所限,咱們把Client,Job,Worker三個角色運行在一臺服務器上:

安裝libevent:
wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz
tar zxvf libevent-1.4.12-stable.tar.gz
cd libevent-1.4.12-stable/
./configure --prefix=/usr
make && make install
/sbin/ldconfig
cd ../



安裝Gearman server and library:

wget http://launchpad.net/gearmand/trunk/0.9/+download/gearmand-0.9.tar.gz
tar zxvf gearmand-0.9.tar.gz
cd gearmand-0.9
./configure
make
make install
/sbin/ldconfig
cd ../


安裝Gearman PHP extension:

wget http://pecl.php.net/get/gearman-0.5.0.tgz
tar zxvf gearman-0.5.0.tgz
cd gearman-0.5.0
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-gearman
make
make install
cd ../

編輯php.ini配置文件加載相應模塊並使之生效:

extension = "gearman.so"

啓動Job:

gearmand -d

若是當前用戶是root的話,則須要這樣操做:

gearmand -d -u root

缺省會使用4730端口,下面會用到。

注意:若是找不到gearmand命令的路徑,別忘了用whereis gearmand確認。



咱們能夠用 ps 指令來查看啓動是否成功:

ps aux |grep gearman
編寫Worker:

worker.php文件內容以下:

<?php
$worker= new GearmanWorker();
$worker->addServer('127.0.0.1', 4730);
$worker->addFunction('reverse', 'my_reverse_function');

while ($worker->work());

function my_reverse_function($job)
{
    return strrev($job->workload());
}
?>

設置後臺運行work:

#php /var/www/html/worker.php &

編寫Client:

client.php文件內容以下:

<?php
$client= new GearmanClient();
$client->addServer('127.0.0.1', 4730);
echo $client->do('reverse', 'Hello World!'), "n";
?>

運行client:

#php /var/www/html/client.php

輸出:!dlroW olleH



首先, PHP Gearman Extension 提供了一個名爲GearmanClient 的類別,它可讓程序安排工做給 Job Server 。

而 addServer 方法表示要通知的是哪些 Job Server ,也就是說若是有多臺 Job Server 的話,就能夠透過 addServer 新增。

而後咱們將要呼叫哪一個 Worker 以及該 Worker 所須要的數據,利用 GearmanClient 的doBackground 方法傳送過去。 doBackground 方法顧名思義就是在背景執行, Client 在丟出需求後就能夠繼續處理其餘的程序。

=============

$client->doBackground('sendEmail', serialize($emailData));
echo "Email sending is done.n";

============

doBackground 方法的第一個參數是告訴 Job Server 要執行哪一個功能,而這個功能則是由 Worker 提供的;要注意是,這個參數只是識別用的,並非真正的函式名稱。而第二個參數是要傳給 Worker 的數據,它必須是個字符串;所以若是要傳送的是數組的話,咱們就要用 PHP 的 serialize 函式來對這些資料作串行化。

出於方便的考慮,Worker,Client使用的都是PHP,但這並不影響演示,實際應用中,你徹底能夠經過Gearman集成不一樣語言實現的Worker,Client。或許此時你還想了解前面提到的負載均衡功能:很簡單,只要增長多個Worker便可,你能夠按照worker.php的樣子多寫幾個相似的文件,並設置不一樣的返回值用以識別演示效果。而後依次啓動這幾個Worker文件,並屢次使用client.php去請求,你就會發現Job會把Client請求轉發給不一樣的Worker。

以守護進程啓動
gearmand -L 192.168.0.1 -p 4730 -u root -d

命令行工具

若是你以爲安裝PHP之類的東西太麻煩的話,你也能夠僅僅經過命令行工具來體驗Gearman的功能:

啓動Worker:gearman -w -f wc -- wc -l &
運行Client:gearman -f wc < /etc/passwd

具體能夠參考官方文檔。
摘自:http://www.codesky.net/article/201107/174139.html
安裝時會出現一個小問題:
configure出現如下錯誤 checking for gperf... no configure: error: could not find gperf. 解決辦法: #yum search gperf #yum install gperf.x86_64.
gperf.x86_64 : A perfect hash function generator.

Gearman 安裝使用 以及 問題處理  
http://deapge.blog.163.com/blog/static/1113114792013929380253


gearman.h:53:23: error: cinttypes: No such file or directory
tr1/cinttypes: No such file or directory
錯誤代碼:
make[1]: Entering directory `/usr/local/src/gearmand-0.38'
  CXX    libgearman/libgearman_libgearman_la-actions.lo
In file included from ./libgearman/common.h:44,
                 from libgearman/actions.cc:39:
./libgearman-1.0/gearman.h:53:27: error: tr1/cinttypes: No such file or directory
make[1]: *** [libgearman/libgearman_libgearman_la-actions.lo] Error 1
make[1]: Leaving directory `/usr/local/src/gearmand-0.38'
解決:[root@localdomain gearmand-0.38]# yum install gcc44 gcc44-c++
來自google快照:https://webcache.googleusercontent.com/search?q=cache:GhqVv51gppAJ:www.livingelsewhere.net/2012/08/22/gearman-instal/+&cd=1&hl=zh-CN&ct=clnk&gl=cn
google來自:http://www.livingelsewhere.net/2012/08/22/gearman-instal/

解決./libgearman-1.0/gearman.h:53:23: error: cinttypes: No such file or directory 實踐OK以下:
後補來自:http://blog.slogra.com/post-234.html
若是出現configure: error: Unable to find libevent,則輸入命令:
yum -y install libevent libevent-devel
而後從新configure.
沒有問題後再執行
make
這步出現./libgearman-1.0/gearman.h:53:27: error: tr1/cinttypes: No such file or directory錯誤,能夠推斷是gcc編譯器的問題,執行命令:
yum install gcc44 gcc44-c++ libstdc++44-devel -y
而後在環境變量里加入:
export CC=/usr/bin/gcc44 or export CC=/usr/bin/gcc
export CXX=/usr/bin/g++44
保存退出後執行:
source /etc/profile
刪除gearmand-0.34文件夾從新進行編譯.
從新進行編譯後執行make這步,如圖
點擊查看原圖
沒有報錯的話才能繼續執行
make install
若是沒有報錯的話,那麼恭喜你能夠繼續安裝gearman了.

看下yum後新的gcc在不在,在的:
yum install gcc44 gcc44-c++ libstdc++44-devel -y

Total download size: 4.1 M
Downloading Packages:
libstdc++44-devel-4.4.7-1.el5.i386.rpm      | 4.1 MB     00:00    
Transaction Test Succeeded
Running Transaction
  Installing     : libstdc++44-devel        1/1
Installed:
  libstdc++44-devel.i386 0:4.4.7-1.el5        
Complete!

方法二:
http://www.cnblogs.com/cenalulu/archive/2013/04/03/2998140.html
關鍵信息:error: tr1/cinttypes: No such file or directory

報錯緣由:libmemcached須要 gcc 4.2 以上版本纔可編譯,而centos 5.4 的gcc版本只有4.1 ,詳見:https://bugs.launchpad.net/libmemcached/+bug/1076181

解決方法:安裝gcc44的擴展包,詳見:http://gearman.info/build/centos5-8.html

解決步驟引用以下:

wget http://mirrors.ustc.edu.cn/fedora/epel/5/i386/epel-release-5-4.noarch.rpm
rpm -ivh epel-release-5-4.noarch.rpm
yum -y -q install boost141-devel
ln -s /usr/include/boost141/boost/ /usr/include/boost
export LDFLAGS="-L/usr/lib64/boost141"
export LD_LIBRARY_PATH=/usr/lib64/boost141:$LD_LIBRARY_PATH
yum -y -q install e2fsprogs-devel e2fsprogs
yum -y -q install gcc44 gcc44-c++
export CC="gcc44"
export CXX="g++44"

全部操做完成後,從新執行:

./configure --enable-memaslap && make && make install

安裝完成!!

實踐以下問題:
[root@test software]# rpm -ivh epel-release-5-4.noarch.rpm
rpm: error while loading shared libraries: libbeecrypt.so.6: cannot open shared object file: No such file or directory


http://pecl.php.net/package/gearman
http://sourceforge.net/projects/boost/?source=pdlp
提示沒有安裝boost libraries,那麼安裝之。
下載地址:http://sourceforge.net/projects/boost/?source=pdlp
關於boost的瞭解,能夠到官網:
http://www.boost.org/doc/libs/1_55_0/more/getting_started/index.html
http://www.boost.org/doc/libs/1_55_0/more/getting_started/windows.html

Boost庫的安裝步驟參照:(漫長的編譯過程……)
Linux下安裝gearman的方法:
https://justwinit.cn/post/7706/
————————————————————————————————————————
http://www.cnblogs.com/longcpp/archive/2012/06/06/2538251.html
還有http://m.blog.csdn.net/blog/yan420523/9096871(……暈暈呼呼)
http://www.cnblogs.com/cocowool/archive/2011/08/18/2144142.html
接着從新執行上面的./configure
又出現錯誤了:

http://www.cnblogs.com/youlechang123/archive/2013/10/22/3382425.html
用apt-get install gperf安裝了gperf,從新執行./configure,執行一段時間,又出錯了:
=====================================================================
重點參考:http://blog.chinaunix.net/uid-27177626-id-4325909.html

參照http://blog.slogra.com/post-234.html解決
用apt-get install libevent libevent-devel安裝缺乏的依賴,可是又出錯了:ubuntu E: Unable to locate package libevent
更新源:apt-get apdate  apt-get upgrade
仍是沒法安裝libevent,只有去官網下載,用源碼安裝方式安裝:http://libevent.org/
下載以後,./configure make make install
從新執行./configure,執行一段時間,又出錯:

用apt-get install libuuid沒法安裝,所以,只有去官網下載:
http://sourceforge.net/projects/libuuid/
用源碼安裝方式安裝。
而後,再次從新執行gearman目錄下的./configure。呼呼~~~~~,終於再也不出錯了,此次順利執行完畢。

Boost DownLoad:
http://jaist.dl.sourceforge.net/project/boost/boost/1.57.0/boost_1_57_0.zipphp

相關文章
相關標籤/搜索