Gearman是一个支持分布式的任务分发框架。
Gearman Job Server:Gearman核心程序,以守护进程形式运行在后台。
Gearman Client:可以理解为任务的收件员,比如我要在后台执行一个发送邮件的任务,可以在程序中调用一个Gearman Client并传入邮件的信息,然后就可以将执行结果立即展示给用户,而任务本身会慢慢在后台运行。
Gearman Worker:任务的真正执行者,一般需要自己编写具体逻辑并通过守护进程方式运行。
Gearman Worker接收到Gearman Client传递的任务内容后,会按顺序处理。
首先利用mysql UDF(通过了lib_mysqludf_json和gearman-mysql-udf的组合实现)在mysql中的数据发生改变时触动触发器将数据传入Gearman中,这时的mysql相当于Gearman的clinet。然后运行自己编写的php程序作为worker,将Gearman中的数据传到Redis中去,这时的Redis相当于是Gearman的consumer。
lib_mysqludf_json UDF 库函数将关系数据映射为 JSON 格式。通常,数据库中的数据映
射为 JSON 格式,是通过程序来转换的。
[root@server4 ~]# yum install -y mariadb-devel
[root@westos_student73 Desktop]# scp lib_mysqludf_json-master.zip [email protected]:/root
[root@server4 ~]# ls
lib_mysqludf_json-master.zip test.sql
[root@server4 ~]# yum install -y unzip[root@server4 ~]# unzip lib_mysqludf_json-master.zip
[root@server4 ~]# cd lib_mysqludf_json-master/
[root@server4 lib_mysqludf_json-master]# ls
lib_mysqludf_json.c lib_mysqludf_json.so README.md
lib_mysqludf_json.html lib_mysqludf_json.sql[root@server4 lib_mysqludf_json-master]# yum install -y gcc
[root@server4 lib_mysqludf_json-master]# ls
[root@server4 lib_mysqludf_json-master]# gcc $(mysql_config --cflags) -shared -fPIC -o lib_mysqludf_json.so lib_mysqludf_json.c
[root@server4 plugin]# pwd
/usr/lib64/mysql/plugin
[root@server4 plugin]# mysqlMariaDB [(none)]> show global variables like 'plugin_dir'; ## 查看 mysql 的模块目录:
+---------------+--------------------------+
| Variable_name | Value |
+---------------+--------------------------+
| plugin_dir | /usr/lib64/mysql/plugin/ |
+---------------+--------------------------+[root@server4 plugin]# cd
[root@server4 ~]# cd lib_mysqludf_json-master/
[root@server4 lib_mysqludf_json-master]# ls
lib_mysqludf_json.c lib_mysqludf_json.so README.md
lib_mysqludf_json.html lib_mysqludf_json.sql
[root@server4 lib_mysqludf_json-master]# cp lib_mysqludf_json.so /usr/lib64/mysql/plugin/ ##拷贝 lib_mysqludf_json.so 模块
[root@server4 lib_mysqludf_json-master]# cd /usr/lib64/mysql/plugin/
[root@server4 plugin]# ls
[root@server4 plugin]# mysql
MariaDB [(none)]> CREATE FUNCTION json_object RETURNS STRING SONAME 'lib_mysqludf_json.so'; ##注册 UDF 函数
MariaDB [(none)]> select * from mysql.func;
将mysql数据映射成json数据,json 数据是一种跨平台的导入导出场景,然后将数据放到redis上来进行同步
这个插件是用来管理调用 Gearman 的分布式的队列。
Gearman MySQL UDF in Launchpad
[root@westos_student73 Desktop]# scp libgearman-1.1.12-18.el7.x86_64.rpm [email protected]:/root
[root@westos_student73 Desktop]# scp gearmand-1.1.12-18.el7.x86_64.rpm [email protected]:/root
[root@westos_student73 Desktop]# scp libevent-devel-2.0.21-4.el7.x86_64.rpm [email protected]:/root
[root@server4 ~]# tar zxf gearman-mysql-udf-0.6.tar.gz
[root@server4 ~]# ls
gearmand-1.1.12-18.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm test.sql
gearman-mysql-udf-0.6.tar.gz lib_mysqludf_json-master
libevent-devel-2.0.21-4.el7.x86_64.rpm lib_mysqludf_json-master.zip
[root@server4 ~]# yum install -y libgearman-* libevent-*
[root@server4 ~]# cd gearman-mysql-udf-0.6/
[root@server4 gearman-mysql-udf-0.6]# ls
aclocal.m4 ChangeLog config.h.in configure.ac libgearman_mysql_udf Makefile.am NEWS
AUTHORS config configure COPYING m4 Makefile.in README
[root@server4 gearman-mysql-udf-0.6]# ./configure --libdir=/usr/lib64/mysql/plugin/ --with-mysql[root@server4 gearman-mysql-udf-0.6]# make && make install
[root@server4 gearman-mysql-udf-0.6]# cd /usr/lib64/mysql/plugin/
[root@server4 plugin]# ls[root@server4 plugin]# mysql
MariaDB [(none)]> CREATE FUNCTION gman_do_background RETURNS STRING SONAME
-> 'libgearman_mysql_udf.so';MariaDB [(none)]> CREATE FUNCTION gman_servers_set RETURNS STRING SONAME
-> 'libgearman_mysql_udf.so';MariaDB [(none)]> select * from mysql.func;
+--------------------+-----+-------------------------+----------+
| name | ret | dl | type |
+--------------------+-----+-------------------------+----------+
| json_object | 0 | lib_mysqludf_json.so | function | ##转换json 数据
| gman_do_background | 0 | libgearman_mysql_udf.so | function | ##指定信息发送的地点
| gman_servers_set | 0 | libgearman_mysql_udf.so | function | ##指定gman_servers位置
在server2中:
[root@westos_student73 Desktop]# scp gearmand-1.1.12-18.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.r php-pecl-gearman-1.1.2-1.el7.x86_64.rpm [email protected]:/root
[root@server2 ~]# ls
[root@server2 ~]# yum install -y gearmand-1.1.12-18.el7.x86_64.rpm libgearman-1.1.12-18.el7.x86_64.rpm
[root@server2 ~]# systemctl start gearmand.service
[root@server2 ~]# netstat -antlp ##开启4730端口
在server4中:
MariaDB [(none)]> SELECT gman_servers_set('172.25.10.2:4730'); ##指定gearmand的服务信息
[root@server4 plugin]# cd
[root@server4 ~]# vim test.sql
[root@server4 ~]# cat test.sql
use test;
#CREATE TABLE `test` (`id` int(7) NOT NULL AUTO_INCREMENT, `name` char(8) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#INSERT INTO `test` VALUES (1,'test1'),(2,'test2'),(3,'test3'),(4,'test4'),(5,'test5'),(6,'test6'),(7,'test7'),(8,'test8'),(9,'test9');DELIMITER $$
CREATE TRIGGER datatoredis AFTER UPDATE ON test FOR EACH ROW BEGIN
SET @RECV=gman_do_background('syncToRedis', json_object(NEW.id as `id`, NEW.name as `name`));
END$$
DELIMITER ;[root@server4 ~]# mysql < test.sql
[root@server4 ~]# mysql
MariaDB [(none)]> SHOW TRIGGERS FROM test; ##查看触发器
[root@westos_student73 Desktop]# scp worker.php [email protected]:/root
[root@server2 ~]# ls
[root@server2 ~]# vim worker.php
[root@server2 ~]# php -m | grep redis
redis
[root@server2 ~]# php -m | grep gearman
[root@server2 ~]# yum install -y php-pecl-gearman-1.1.2-1.el7.x86_64.rpm
[root@server2 ~]# php -m | grep gearman
gearman
[root@server2 ~]# systemctl reload php-fpm[root@server2 ~]# mv worker.php /usr/local
[root@server2 ~]# which php
/usr/bin/php
[root@server2 ~]# php /usr/local/worker.php &
[1] 3762
[root@server2 ~]# ps ax
测试:
在server3 中
MariaDB [(none)]> use test
MariaDB [test]> update test set name='westos' where id=1;
[root@server3 ~]# redis-cli
127.0.0.1:6379> get 1
"test1"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> get 3
"test3"
127.0.0.1:6379> get 1
"westos"
127.0.0.1:6379> get 2
"test2"
127.0.0.1:6379> get 3
"test3"
访问http://172.25.10.2/test.php
在数据库中:
MariaDB [test]> update test set name='redhat' where id=1;
MariaDB [test]> select * from test;
异步同步,当我们更新mysql时,由触发器直接把任务发给4730(gearmand),由gearmand分发给worker,worker可以由任何语言编写,然后更新redis中数据