gearman服務鏈接php java

在實際工做中,會碰到兩個問題php

(1)現有系統想集成一個開發組件,而該組件的SDK卻沒有現有語言版本。java

(2)系統中的一項功能很是耗費資源,最好能利用其它機器來處理。linux

本文介紹gearman的使用,實現PHP調用JAVA。gearman是支持網絡方式調用,所以也能夠用來實現任務分發。git

gearman的官方網站爲github

http://gearman.org/apache

 

如圖所示,gearman系統主要分爲3個部分,server、client、worker。api

1、server安裝網絡

下載 server,官方網站上提供了三個語言版本的server。http://gearman.org/download/eclipse

咱們選擇C語言版本,下載地址爲,https://launchpad.net/gearmandmaven

目前提供下載的是gearmand-1.1.12.tar.gz。

在編譯以前,須要先安裝用到的庫問題,

(1)yum install boost-devel -y

(2)yum install gprep -y

(3)yum install libevent-devel -y

(4)yum install libuuid-devel -y

解壓gearmand-1.1.12,執行

 

./configure
make
make install

安裝完成後,在命令行運行,就能夠在本地就啓動一個Job server ,等待client 和worker 鏈接。

 

gearmand -d

 2、安裝PHP擴展

利用pecl安裝gearman php擴展。建議在安裝gearman server的時候採用默認安裝,這樣在安裝php 擴展的時候就比較容易找到須要的頭文件、連接文件。

pecl install gearman

編輯php.ini,添加

extension="gearman.so"

命令行測試 php -m |grep gearman

PHP 測試代碼

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()); } ?>

client.php

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

首先運行worker.php,程序不會結束,會一直運行。

運行client,返回運行的結果。

gearman提供了一個命令行檢測工具(要安裝nc)。

watch -n 1 "(echo status; sleep 0.1) | nc 127.0.0.1 4730"

如圖,在gearman上註冊了reverse函數,worker數量爲1(第3列),client 0(第1列),等待處理的請求也爲0(第2列)。

3、安裝JAVA擴展

官方網站提供了兩個java擴展,java gearman server 、gearman java。筆者在用gearman java的時候,傳遞參數以爲不方便,就改用gearman server了。

下載gearman server,地址

https://github.com/gearman/java-service

maven編譯。注,筆者也沒有過maven,下面是個人操做過程

(a)下載maven ,解壓

https://maven.apache.org/download.cgi

(b)修改/etc/profile文件,將maven/bin目錄添加到path路徑上。source /etc/profile。

mvn --vesion

 進入java service 目錄執行下面的命令

mvn package

編譯的時間比較長,編譯成功後,會在源碼目錄下生成一個target目錄。注,在編譯過程當中會下載不少模塊,若是編譯失敗能夠多試幾回。

其中 java-gearman-service-0.7.0-snapshot.jar就是須要文件。

 新建/root/workspace/gearman工程目錄,建立文件夾 com/jfjb/gearman。建立EchoWorker.java文件,

package com.jfjb.gearman; import org.gearman.Gearman; import org.gearman.GearmanFunction; import org.gearman.GearmanFunctionCallback; import org.gearman.GearmanServer; import org.gearman.GearmanWorker; /** * The echo worker polls jobs from a job server and execute the echo function. * * The echo worker illustrates how to setup a basic worker */
public class EchoWorker implements GearmanFunction { /** The echo function name */
        public static final String ECHO_FUNCTION_NAME = "echo"; /** The host address of the job server */
        public static final String ECHO_HOST = "localhost"; /** The port number the job server is listening on */
        public static final int ECHO_PORT = 4730; public static void main(String... args) { /* * Create a Gearman instance */ Gearman gearman = Gearman.createGearman(); /* * Create the job server object. This call creates an object represents * a remote job server. * * Parameter 1: the host address of the job server. * Parameter 2: the port number the job server is listening on. * * A job server receives jobs from clients and distributes them to * registered workers. */ GearmanServer server = gearman.createGearmanServer( EchoWorker.ECHO_HOST, EchoWorker.ECHO_PORT); /* * Create a gearman worker. The worker poll jobs from the server and * executes the corresponding GearmanFunction */ GearmanWorker worker = gearman.createGearmanWorker(); /* * Tell the worker how to perform the echo function */ worker.addFunction(EchoWorker.ECHO_FUNCTION_NAME, new EchoWorker()); /* * Tell the worker that it may communicate with the this job server */ worker.addServer(server); } @Override public byte[] work(String function, byte[] data, GearmanFunctionCallback callback) throws Exception { /* * The work method performs the gearman function. In this case, the echo * function simply returns the data it received */
                return data; } }

 在/root/workspace/gearman中添加須要的jar, java-gearman-service-0.7.0-snapshot.jar,slf4j-api-1.6.4.jar,slf4j-simple-1.6.4.jar。

javac -cp java-gearman-service-0.7.0-SNAPSHOT.jar com/jfjb/gearman/EchoWorker.java

運行

java -cp java-gearman-service-0.7.0-SNAPSHOT.jar:slf4j-api-1.6.4.jar:slf4j-simple-1.6.4.jar:/root/workspace/gearman3 com/jfjb/gearman/EchoWorker

此時,利用命令行查看gearman註冊的work,echo函數就是EchoWorker註冊到server上的函數。

新建一個PHP文件clientjava.php,用來調用「echo」

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

 

 測試過程完畢。

附錄,因爲做者是在一臺全新系統上安裝所有軟件的。將一些設置記錄下。

1、php安裝

yum install -y php

yum install -y php-devel

2、pecl安裝

wget http://pear.php.net/go-pear.phar

php go-pear.phar 

3、php後臺運行

setsid php worker.php

4、jdk 安裝

rpm -ivh  jdk-7u2-linux-i586.rpm 

查看命令

java -version

javac -version

修改 /etc/profile 

export JAVA_HOME=/usr/java/jdk1.7.0_21 

export PATH=$JAVA_HOME/bin:$PATH 

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar

5、eclipse安裝

下載eclipse jar文件。

我下載的eclipse版本爲eclipse-jee-luna-SR2-linux-gtk.tar.gz

http://mirrors.opencas.cn/eclipse//technology/epp/downloads/release/luna/SR2/eclipse-jee-luna-SR2-linux-gtk.tar.gz

解壓後,控制檯進入eclipse文件夾,命令 ./eclipse啓動eclipse

相關文章
相關標籤/搜索