Gearman task assignments

Source: Internet
Author: User
Tags ip number

Gearman implementing multi-database data synchronization

Test environment: Windows (MySQL) + virtual machines (Ubuntu + MySQL) + PHP

1:gearman's official documentation can be learned Gearman, installing Gearman in Ubuntu:

sudo apt-get update

sudo apt-get upgrade

sudo apt-get install gcc autoconf Bison Flex libtool make Libboost-all-dev Libcurl4-openssl-dev Curl Libevent-dev memcache D Uuid-dev Libsqlite3-dev Libmysqlclient-dev

2: Download Gearman

wget https://launchpad.net/gearmand/1.2/1.1.5/+download/gearmand-1.1.5.tar.gz

3: Unzip the installation:

Tar xvzf gearmand-1.1.5.tar.gz
CD gearmand-1.1.5
./configure
Make
Make install

If there are errors based on the error prompt: sudo apt-get install .... such as

4: Install Gearman via PECL (default to your Ubuntu already installed PHP):

sudo apt-get install Php-pear
sudo pecl install Gearman
sudo gedit/etc/php5/cli/php.ini

Add extension= "gearman.so" at the end of the file;

5: Terminal input: php--info | grep "Gearman support" appears gearman support = + enabled means success!

6: Start Gearmand:sudo gearmand-d &

Note: This may cause an error: Gearmand:could not open log file "/var/log/gearmand.log", from "/HOME/CJ", switching to stderr. (Permission denied)

Workaround:

Mkdir-p/usr/local/var/log/
cd/usr/local/var/log/
Touch Gearmand.log

Start again: Gearmand, by Ps-ef | grep Gearmand View the Gearman process.

7:php Testing (Multi-server database data synchronization)

(1) Environment: Install MySQL in host windows, set database login name: CW and password: cw123;

Virtual machine ubuntu install MySQL, set database login name: CU and Password: cu123.

Create a table for each of the two databases: fb_table (table name).

(2) Server: servercj.php: (content)

      

1     2 3<?PHP4 5 $worker=NewGearmanworker ();6 $worker->addserver ("127.0.0.1", 4730);7 $worker->addserver ("192.168.238.2", 4730);8 9 $worker->addfunction (' func1 ',function(Gearmanjob$job){Ten  One $str=$job-workload (); A $datas= Json_decode ($str,true); -  - $key=$datas["Key"]; the $lang=$datas["Lang"]; - $texts=$datas["Text"]; - $kid=$datas["Aid"]; -  + $con=mysql_connect("192.168.238.2", "CW", "cw123"); - if(!$con) + { A  die(' Could not connect: '.Mysql_error()); at } - $sqls= "SELECT count (1) from fb_table where albumkey= '".$key."‘"; - $count=mysql_query($sqls,$mycon); - if($count>0){ - $STRSQL= "Update fb_table set albumkey="$key', langcode= '$lang', texts= '$texts', albumid= '$kid‘"; - } in Else{ - $STRSQL= "INSERT into fb_table (Albumkey,langcode,texts,albumid) VALUES ('$key‘,‘$lang‘,‘$texts‘,‘$kid‘)"; to } +  - $result=mysql_query($STRSQL,$mycon); the Sleep(3); * return' The user requested ('.$result.‘) Is Func1 '; $ });Panax Notoginseng  - $worker->addfunction (' Func2 ',function(Gearmanjob$job){ the $str=$job-workload (); + $datas= Json_decode ($str,true); A  the $key=$datas["Key"]; + $lang=$datas["Lang"]; - $texts=$datas["Text"]; $ $kid=$datas["Aid"]; $  - $con=mysql_connect("127.0.0.1", "cu", "cu123"); - if(!$con) the { -  die(' Could not connect: '.Mysql_error());Wuyi } the $sqls= "SELECT count (1) from fb_table where albumkey= '".$key."‘"; - $count=mysql_query($sqls,$mycon); Wu if($count>0){ - $STRSQL= "Update fb_table set albumkey="$key', langcode= '$lang', texts= '$texts', albumid= '$kid‘"; About } $ Else{ - $STRSQL= "INSERT into fb_table (Albumkey,langcode,texts,albumid) VALUES ('$key‘,‘$lang‘,‘$texts‘,‘$kid‘)"; - } -  A $result=mysql_query($STRSQL,$mycon); +  the Sleep(3); - return' The user ('.$result.‘) Is Func2 '; $ }); the  the  the  while($worker-Work ()); the  -  
View Code

(3) Client: clientcj.php: (content)

1<?PHP2 3 $client=Newgearmanclient ();4 $client->addserver ("127.0.0.1", 4730); 5 6 $userInfo=$friends=NULL;7 8 $client->setcompletecallback (function(Gearmantask$task,$context) Use(&$userInfo, &$friends) {9     Switch($context) {Ten          Casefunc1 One             $userInfo=$task-data (); A              Break; -          Case' Func2 ' -             $friends=$task-data (); the              Break; -     } - }); -  + $data=Array("Key" = "Abcdds", "lang" = "en", "text" = "hellows", "Aid" =>3); - $datas=json_encode ($data); +  A $client->addtask (' func1 ',$datas, ' func1 '); at $client->addtask (' Func2 ',$datas, ' Func2 '); -  - Echo"Fetching...\n"; - $start=Microtime(true); - $client-runtasks (); - $totaltime=Number_format(Microtime(true) -$start, 2); in  - Echo"Got User info in:$totaltimeSeconds:\n "; to Var_dump($userInfo,$friends);
View Code

Note: in SERVERCJ $worker->addserver ("192.168.238.2", 4730); the IP number of the computer (the IP address of Windows) is the database that links to Windows.

(4) Run: In terminal input: PHP/**/servercj.php & (WHERE * * is the SERVERCJ storage path); Run server

In open another terminal, enter: PHP/**/clientcj.php run the client.

To verify that the data was successfully inserted into the database: in Ubuntu, go to database query the newly created table fb_table, found that a piece of data was inserted, the same as the Windows host database as much as a single piece of data.

Summary: The implementation of PHP using Gearman to two servers to insert data into the database, to achieve data synchronization. Of course, PHP can do the same, but it's much slower.

    

Gearman task assignments

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.