centos 使用docker搭建Gearman任务分发系统 ,Gearman的安装和使用

第一步,centos 安装 docker,拉取docker centos 镜像(此处不赘述)

https://docs.docker.com/install/linux/docker-ce/centos/

docker -v

docker ps  (查看docker的容器状态)

docker pull centos(docker安装ubuntu最新镜像)
docker images (查看本地所有镜像)

 

[root@izwz9c4rb2kbs0ggxd67hiz ~]# docker run -d -it –name=gearman-container centos:latest /bin/bash(name前面双横线)

docker ps

docker exec -it f49a2392164e /bin/bash

f49a2392164e(CONTAINER ID)

 

以centos运行docker容器

第二步,搭建Gearman任务分发系统

yum update

[root@f49a2392164e /]# yum install gearmand
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* extras: mirrors.163.com
* updates: mirrors.cn99.com
No package gearmand available.
Error: Nothing to do

yum没有找到对应依赖包,更新epel第三方软件库,运行命令:
yum install -y epel-release

更新完epel第三方软件库后,再次尝试使用yum命令安装对应的软件包

yum install gearmand

remi — linux下比较好用的安装源

yum install wget

在centOS下下载源

su
cd /etc/yum.repos.d
wget http://rpms.famillecollet.com/enterprise/remi.repo

安装php扩展

yum install –enablerepo=remi –enablerepo=remi-php56 php-pecl-gearman

或升级:
yum –enablerepo=remi-php55,remi update php*

查看扩展是否安装成功

php -m | grep gearman

 

worker.php

<?php
    $worker= new GearmanWorker();
    $worker->addServer("127.0.0.1", 4730);  
    $worker->addFunction("title", "title_function");
    while ($worker->work());

    function title_function($job){
        return "你请求的数据:" . $job->workload() . " 请求时间:" . date('Y-m-d H:i:s');
        //return ucwords(strtolower($job->workload()));
    }
?>

 

client.php

 

<?php
    $client= new GearmanClient();
    $client->addServer("127.0.0.1", 4730);
    print $client->do("title", json_encode(
        [
            'username'=>'jack',
            'email'=>'jack@foxmail.com',
        ]
    ));
    print "\n";
?>

 

测试:

启动服务

$ sudo gearmand -d

启动worker

$ php worker.php &
[1] 15135

启动client

$ php client.php 
你请求的数据:{"username":"jack","email":"jack@foxmail.com"} 请求时间:2017-07-10 23:25:30

参考:
https://segmentfault.com/a/1190000000494087
https://www.ibm.com/developerworks/cn/opensource/os-php-gearman/
http://gearman.org/getting-started/#client
http://php.net/manual/zh/book.gearman.php

https://www.jianshu.com/p/c7a55dee5796