gearman服务连接php java

在实际工作中,会碰到两个问题

(1)现有系统想集成一个开发组件,而该组件的SDK却没有现有语言版本。

(2)系统中的一项功能非常耗费资源,最好能利用其它机器来处理。

本文介绍gearman的使用,实现PHP调用JAVA。gearman是支持网络方式调用,因此也可以用来实现任务分发。

gearman的官方网站为

http://gearman.org/

如图所示,gearman系统主要分为3个部分,server、client、worker。

一、server安装

下载 server,官方网站上提供了三个语言版本的server。http://gearman.org/download/

我们选择C语言版本,下载地址为,https://launchpad.net/gearmand

目前提供下载的是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
二、安装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列)。

三、安装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 serverbr/>
/
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"; ?>

测试过程完毕。
附录,由于作者是在一台全新系统上安装全部软件的。将一些设置记录下。
一、php安装
yum install -y php
yum install -y php-devel
二、pecl安装
wget http://pear.php.net/go-pear.phar

php go-pear.phar

三、php后台运行

setsid php worker.php

四、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= J A V A H O M E / b i n : JAVA_HOME/bin: PATH

export CLASSPATH=.: J A V A H O M E / l i b / d t . j a r : JAVA_HOME/lib/dt.jar: JAVA_HOME/lib/tool.jar

五、eclipse安装

下载eclipse jar文件。

转载于https://www.cnblogs.com/jbexploit/p/4579843.html

猜你喜欢

转载自blog.csdn.net/weixin_44767512/article/details/88576515