Gearman Service Connection PHP java

Source: Internet
Author: User

In the actual work, you will encounter two problems

(1) The existing system wants to integrate a development component, and the SDK for that component does not have an existing language version.

(2) A function in the system is very resource-intensive, and it is best to use other machines to handle it.

This article describes the use of Gearman to implement PHP calls to Java. Gearman is a network-mode call, so it can also be used for task distribution.

Gearman's official website is

http://gearman.org/

, the Gearman system is mainly divided into 3 parts, server, client, worker.

One, server installation

Download server, the official website is available in three language versions of server. http://gearman.org/download/

We choose the C language version, for, Https://launchpad.net/gearmand

Currently available for download is gearmand-1.1.12.tar.gz.

Before compiling, you need to install the library issues that are used.

(1) Yum install Boost-devel-y

(2) Yum install Gprep-y

(3) Yum install Libevent-devel-y

(4) Yum install Libuuid-devel-y

Unzip gearmand-1.1.12, perform

./Configuremake makeinstall

Once the installation is complete, run at the command line to start a job server locally and wait for client and worker connections.

Gearmand-d

Second, install the PHP extension

Install Gearman PHP extensions with PECL. It is recommended to use the default installation when installing Gearman server, which makes it easier to find the required header files and link files when installing PHP extensions.

Install Gearman

Edit PHP.ini, add

extension= "Gearman.so"

Command line test php-m |grep Gearman

PHP Test Code

worker.php

 <? PHP   $worker  = new   Gearmanworker ();   $worker ->addserver (' 127.0.0.1 ', 4730  $worker ->addfunction (' reverse ', ' my_reverse_function ' 

client.php

<? PHP $client New gearmanclient (); $client->addserver (' 127.0.0.1 ', 4730); Echo $client-do (' reverse ', ' Hello world! '), ' \ n ';? >

Run worker.php first, the program will not end and will always run.

Runs the client and returns the result of the run.

Gearman provides a command line detection tool (to install NC).

1 " (echo status; sleep 0.1) | NC 127.0.0.1 4730 "

, the reverse function is registered on Gearman, the number of workers is 1 (3rd column), client 0 (1th column), and the request for processing is 0 (column 2nd).

Third, install the Java extension

The official website provides two Java extensions, Java Gearman server, Gearman java. When I use Gearman Java, the transfer parameters feel inconvenient, and then use Gearman server.

Download Gearman Server, address

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

Maven compiles. Note, I did not have Maven, the following is my operation process

(a) Download maven, unzip

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

(b) Modify the /etc/profile file to add the maven/bin directory to the path path. Source/etc/profile.

MVN--vesion

Enter the Java service directory to execute the following command

MVN Package

Compile for a long time, after the successful compilation, the source directory will be generated under the target directory. Note that many modules are downloaded during the compilation process and can be tried several times if the compilation fails.

Where Java-gearman-service-0.7.0-snapshot.jar is the need for documentation.

Create a new/root/workspace/gearman project directory, creating a folder Com/jfjb/gearman. Create a Echoworker.java file,

 PackageCom.jfjb.gearman;ImportOrg.gearman.Gearman;Importorg.gearman.GearmanFunction;ImportOrg.gearman.GearmanFunctionCallback;ImportOrg.gearman.GearmanServer;ImportOrg.gearman.GearmanWorker;/*** The echo worker polls jobs from a job server and execute the ECHO function. * The Echo worker illustrates Setup a basic worker*/ Public classEchoworkerImplementsgearmanfunction {/**The echo function name*/         Public Static FinalString echo_function_name = "ECHO"; /**The host address of the job server*/         Public Static FinalString echo_host = "localhost"; /**The port number the job server is listening on*/         Public Static Final intEcho_port = 4730;  Public Static voidMain (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,NewEchoworker ()); /** Tell the worker which it may communicate with the This job server*/worker.addserver (server); } @Override Public byte[] Work (String function,byte[] data, Gearmanfunctioncallback callback)throwsException {/** The Work method performs the Gearman function. In this case, the echo * function simply returns the data it received*/                returndata; }}

Add the required jar to the/root/workspace/gearman, 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

Run

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

At this point, using the command line to view Gearman registered Work,echo function is echoworker registered to the server on the function.

Create a new PHP file clientjava.php to call "echo"

<? PHP $client New gearmanclient (); $client->addserver (' 127.0.0.1 ', 4730); Echo $client-do (' echo ', ' Hello world! '), ' \ n ';? >

The test process is complete.

Appendix, because the author installs all the software on a new system. Record some of the settings.

One, PHP installation

Yum install-y php

Yum Install-y php-devel

Second, PECL installation

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

PHP Go-pear.phar

Third, PHP background run

Setsid PHP worker.php

Iv. Installation of JDK

RPM-IVH jdk-7u2-linux-i586.rpm

View commands

Java-version

Javac-version

Modify /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

V. Installation of Eclipse

Download the eclipse jar file.

The version of Eclipse i downloaded is 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

After decompression, the console enters the Eclipse folder and commands ./eclipse start Eclipse

Gearman Service Connection PHP java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.