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

(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  "(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 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.-SNAPSHOT.jar com/jfjb/gearman/EchoWorker.java

运行

java -cp java-gearman-service-0.7.-SNAPSHOT.jar:slf4j-api-1.6..jar:slf4j-simple-1.6..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=$JAVA_HOME/bin:$PATH

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

五、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

gearman服务连接php java的更多相关文章

  1. 【Elasticsearch】【WEB】java web服务连接es elasticsearch始终报错,无法正常连接使用的错误解决历程

    前情提要: web服务往华为云上迁移 ================内网的好环境,相关配置=================== 1.web服务关于ES的集群配置如下: elasticAddress ...

  2. 服务器启动socket服务报错 java.net.BindException:Cannot assign requested address

    错误信息:  2017-06-13 11:18:00,865 [GateServer.java:82][ERROR]:启动服务出错了java.net.BindException: Cannot ass ...

  3. PS用户配置服务连接出错

    PS用户配置服务连接出错 [2019-07-30 14:58:25.475]-[INFO ]-[xxxx.profileserver.service.ProfileServerImpl:1430][1 ...

  4. Linux下部署ASP.NET服务连接oracle遇到的问题记录

    一.如何卸载MONO Q:mono是linux系统上跨平台软件,卸载它有两种方式: 1.知道mono安装路径,安装原来的路径直接覆盖安装(最为简单): 2.不知道mono安装路径,首先通过sudo f ...

  5. ArcGIS SDE 10.1 for Postgresql 服务连接配置

    去年写了ArcGIS 10.1 如何连接Postgresql 数据库(http://blog.csdn.net/arcgis_all/article/details/8202709)当时采用的也是Ar ...

  6. idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost connection toMySQL server at

    我是使用navicat的windows端 连接centos下mysql服务器 第一次常规连接mysql正常,idea启动服务连接mysql后 Navicat连接mysql就报错2013-Lost co ...

  7. SSH服务连接

    SSH基本概述 SSH是一个安全协议,在进行数据传输时,会对数据包进行加密处理,加密后在进行数据传输.确保了数据传输安全. SSH服务 ssh: secure shell, protocol, 22/ ...

  8. VisualVM连接远程Java进程

    jstatd是一个RMI(Remove Method Invocation)的server应用,用于监控jvm的创建和结束,并且提供接口让监控工具(如VisualVM)可以远程连接到本机的jvms . ...

  9. 利用VisualVM监测Azure云服务中的Java应用

    在做Java开发的时候,我们需要在上线之前对程序进行压力测试,对程序的性能进行全面的监控,了解JVM的CPU,内存,GC,classes,线程等等信息,或者在程序上线运行的过程当中以便于诊断问题或者对 ...

随机推荐

  1. 汇编_指令_REP MOVESB 和 CLD

    先说说MOVSB(MOVe String Byte):即字符串传送指令,这条指令按字节传送数据.通过SI和DI这两个寄存器控制字符串的源地址和目标地址,比如DS:SI这段地址的N个字节复制到ES:DI ...

  2. 找到一篇关于2.4/5G信道的新介绍

    关于部分手机无法搜索到5G wifi信号的解决方法第一次在论坛发基础理论贴,希望能普及关于5G wifi的基础知识.         发此贴的原因是基于本人突然发现:MX3刷了3.4.1系统后,搜索不 ...

  3. PG覆盖率检查

    覆盖率检查 需要gcov和lcov工具,gcov在gcc中自带,lcov需要自行下载安装 重新编译 ./configure --prefix=`pwd`/install --with-perl --w ...

  4. 关于微信小程序的一些看法和理解

    最近做了几个小时的调研,微信小程序 微信小程序是可以理解成在微信中的APP,他的目标是APP的替代者,由于目前的APP主要区分安卓和IOS,或者其他平台, 那么微信小程序的平台在微信,在任何一个手机系 ...

  5. Pthreads n 体问题

    ▶ <并行程序设计导论>第六章中讨论了 n 体问题,分别使用了 MPI,Pthreads,OpenMP 来进行实现,这里是 Pthreads 的代码,分为基本算法和简化算法(引力计算量为基 ...

  6. 「小程序JAVA实战」小程序的横向视频和页面拦截(59)

    转自:https://idig8.com/2018/09/24/xiaochengxujavashizhanxiaochengxudehengxiangshipinheyemianlanjie58/ ...

  7. Git----分支管理01

    分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN. 如果两个平行宇宙互不干扰,那对现在的你也没有啥影响,不过,在某个时间点,两个平行宇宙合 ...

  8. c#正则获取html里面a标签href的值

    获取单个a中href的值: string str = "<a href=\"http://www.itsve.com\">下载</a>" ...

  9. 【Git】二、安装配置

    一.Git安装 Linux $ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \ libz-dev libssl-dev $ ap ...

  10. 126. Word Ladder II( Queue; BFS)

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...