標籤:gearman安裝與使用 gearmand-1.1.12 分布式訊息佇列 gearman
1 Gearman簡介
1.1 概況 Gearman是一個用來把工作委派給其他機器、分布式的調用更適合做某項工作的機器、並發的做某項工作在多個調用間做負載平衡、或用來在調用其它語言的函數的系統。
1.2 組成 Gearman是一個分發任務的程式架構,由三部分組成:
1)Gearman client:提供gearman client API給應用程式調用。API可以使用C,PHP,PERL,MYSQL UDF等待呢個語言,它是請求的發起者。
2)Gearman job server:將用戶端的請求分發到各個gearman worker的調度者,相當於中央控制器,但它不處理具體商務邏輯。
3)Gearman worker:提供gearman worker API給應用程式調用,具體負責用戶端的請求,並將處理結果返回給用戶端。
1.3 應用
Mogilefs的Distributed File System的核心就是用gearman實現的。
這個軟體的應用情境很多,比如視頻網站的視頻處理,分布式Tlog,電子郵件處理,檔案同步處理,圖片處理等等,只要是可以放開,不影響體驗和響應的場 景,需要並行進行大量計算和處理的程式都是可以的。Yahoo在60或更多的伺服器上使用gearman每天處理600萬個作業。新聞彙總器digg構建 了一個相同規模的gearman網路,每天可處理400000個作業。
Gearman不但可以做為任務分發,還可以做為應用方面的負載平衡。可以讓worker放在不同的一堆伺服器上,也可以啟動放在同一個cpu的多個核 上。比如,應用視頻轉換程式,不希望web伺服器來處理視頻格式轉換,這時,可以在這一堆伺服器上進行任務分發,在上面載入worker處理視頻格式,對 外的web伺服器就不會被視頻轉換過程影響。而且擴充方便,加一台伺服器到任務調度中心,註冊成worker即可,這時job server會在請求到來的時候,將請求發送給閒置worker。還可以運行多個job server,組成ha架構,如果一個job server當掉了,client和worker會自動遷移到另一台job server上。
1.4 工作原理圖
2 運行過程
一個Gearman請求的處理過程涉及三個角色:Client -> Job -> Worker。
Client:請求的發起者,可以是 C,PHP,Perl,MySQL UDF 等等。
Job:請求的調度者,用來負責協調把 Client 發出的請求轉寄給合適的 Work。
Worker:請求的處理者,可以是 C,PHP,Perl 等等。
因為 Client,Worker 並不限制用一樣的語言,所以有利於多語言多系統之間的整合。
甚至我們通過增加更多的 Worker,可以很方便的實現應用程式的分布式負載平衡架構。
3 Gearman下載 1)官網
http://gearman.org/
2)官網下載
https://launchpad.net/gearmand
3)官網使用嚮導
http://gearman.org/getting-started/
3)本次安裝用到的所有軟體(安裝環境為CentOS-6.5)
http://download.csdn.net/detail/clevercode/8698699
4 Gearman安裝
4.1 安裝linux必備常用庫
Linux中必備常用支援庫的安裝:http://blog.csdn.net/clevercode/article/details/45438401
4.2 安裝gearmand依賴的庫 # yum install -y boost-devel gperf libevent-devel libuuid-devel
4.3 安裝gearmand服務 1)解壓
# cd /usr/local/src/gearman
# tar xzf gearmand-1.1.12.tar.gz
2)配置
# cd gearmand-1.1.12
# ./configure
3)編譯
# make
4)安裝
# make install
5)安裝成功圖,輸入
# gearman
5 安裝php擴充 1)安裝phpize
# yum install -y php-devel
2)解壓
# cd /usr/local/src/gearman
# tar xzf gearman-1.1.2.tgz
3)配置
# cd gearman-1.1.2
# phpize
# ./configure
4)編譯
# make
5)安裝
# make install
6)安裝成功
出現“Installing shared extensions: /usr/lib64/php/modules/”表示安裝成功,/usr/lib64/php/modules/是gearman.so擴充的目錄。
7)配置(加入擴充)
# vi /usr/local/php5/etc/php.ini
extension="gearman.so"
8)查配置是否成功
# vi test.php
<?php
print gearman_version() . "\n";
?>
執行php test.php後,出現1.1.12表示安裝成功
# php test.php
1.1.12
6 Gearman啟動停止
1) 建立日誌/data0/logs/gearmand.log
# touch /data0/logs/gearmand.log
2)啟動
# /usr/local/sbin/gearmand -d -u root -L 192.168.142.130 --log-file=/data0/logs/gearmand.log
3)參數詳解
-b,--backlog= 儲備的監聽串連數量
-d, --daemon 後台運行
-f, --file-descriptors= 檔案描述符的數量
-h, --help 協助
-j, --job-retries= 在ob server移除不可用job之前啟動並執行次數,防止不斷運行導致其他可用worker崩潰。預設沒有限制
-l, -log-file= 記錄檔存放位置(預設記錄最簡單日誌)
-L, --listen= 監聽的IP,預設全部接受
-p, --port= 指定監聽連接埠
-P, --pid-file= 指定進程ID寫入位置
-r, --protocol= 載入協議模組
-q, --queue-type= 指定持久化隊列
-t, --threads= 使用的I/9線程數量。預設為0
-u, --user= 啟動後,切換到指定使用者
-v, --verbose 增加一級詳細程度
-V, --version 顯示版本資訊
4)查是否運行
# ps axu | grep gearmand
5)查看監聽連接埠
# netstat -anp | grep 4730
6)停止,直接kill掉進程。
7 Gearman使用
7.1 建立Worker 建立worker.php,建立一個發送郵件的Worker端。代碼如下
<?php$worker= new GearmanWorker();$worker->addServer('192.168.142.130', '4730');$worker->addFunction("sendMail", "my_sendmail_function");while ($worker->work());function my_sendmail_function($job){ // 接收資料 $tmp = $job->workload(); $receiveArr = unserialize($tmp); $from = $receiveArr['from']; $to = $receiveArr['to']; $subject = $receiveArr['subject']; $content = $receiveArr['content']; //發送郵件 //.... return $subject.' sendmail OK';}?>
7.2 啟動Worker端
如果處理的資料量大,可以執行以下指令碼多次,即啟動多個Worker端。
# nohup php worker.php > tmp.txt &
7.3 建立Client(阻塞模式,需要等待返回結果才結束) 建立一個client.php。do()方法是阻塞模式,必須等待worker端返回結果,程式才能停止。(返回:hello Gearman sendmail OK)
<?php$client= new GearmanClient();$client->addServer('192.168.142.130', '4730');$job = array();$job['from'] = 'CleverCode';$job['to'] = 'Gearman';$job['subject'] = 'hello Gearman';$job['content'] = 'hello Gearman:this is from GearmanClient';$job = serialize($job);//等到worker端返回結果,才會結束。$ret = $client->do("sendMail", $job);echo $ret."\r\n";?>
7.4 建立Client(非阻塞,不用等結果) 建立一個client2.php。doBackground()不用等待worker端返回結果,程式就結束了。
<?php$client= new GearmanClient();$client->addServer('192.168.142.130', '4730');$job = array();$job['from'] = 'CleverCode';$job['to'] = 'Gearman';$job['subject'] = 'hello Gearman';$job['content'] = 'hello Gearman:this is from GearmanClient';$job = serialize($job);//不等待返回結果,就會結束$ret = $client->doBackground("sendMail", $job);echo $ret."\r\n";?>
8 Gearman管理 輸入以下命令,查看4730連接埠情況。
# (echo "status" ; sleep 2 ) | telnet 192.168.142.130 4730
1)欄位說明:"已知註冊的任務" "正在啟動並執行任務" "隊列中的任務" "可用的 Worker".
2)sendMail 0 0 1,註冊的任務名為 sendMail,0 個正常在運行,隊列為空白,有一個可用的 Worker.
著作權聲明:
1)原創作品,出自"CleverCode的部落格",轉載時請務必註明以下原創地址,否則追究著作權法律責任。
2)原創地址:http://blog.csdn.net/clevercode/article/details/45718735(轉載務必註明該地址)。
3)部落格專欄地址(Linux常用軟體安裝與配置):http://blog.csdn.net/column/details/linuxsoftwareinstall.html(持續增加,關注請收藏)。
4)歡迎大家關注我部落格更多的精彩內容:http://blog.csdn.net/CleverCode。
Linux中Gearman安裝與使用,分布式訊息佇列(CentOS-6.5:gearmand-1.1.12)