天天看点

数据库进程间通信解决方案

中国广东省深圳市龙华新区民治街道溪山美地

518131

+86 13113668890

+86 755 29812080

$date: 2013-12-16 13:34:20 +0800 (thu, 16 may 2013) $

摘要

<a></a>

我的系列文档

<a target="_top" href="http://netkiller.github.io/architect/index.html">netkiller architect 手札</a>

<a target="_top" href="http://netkiller.github.io/developer/index.html">netkiller developer 手札</a>

<a target="_top" href="http://netkiller.github.io/php/index.html">netkiller php 手札</a>

<a target="_top" href="http://netkiller.github.io/python/index.html">netkiller python 手札</a>

<a target="_top" href="http://netkiller.github.io/testing/index.html">netkiller testing 手札</a>

<a target="_top" href="http://netkiller.github.io/cryptography/index.html">netkiller cryptography 手札</a>

<a target="_top" href="http://netkiller.github.io/linux/index.html">netkiller linux 手札</a>

<a target="_top" href="http://netkiller.github.io/centos/index.html">netkiller centos 手札</a>

<a target="_top" href="http://netkiller.github.io/freebsd/index.html">netkiller freebsd 手札</a>

<a target="_top" href="http://netkiller.github.io/security/index.html">netkiller security 手札</a>

<a target="_top" href="http://netkiller.github.io/version/index.html">netkiller version 手札</a>

<a target="_top" href="http://netkiller.github.io/www/index.html">netkiller web 手札</a>

<a target="_top" href="http://netkiller.github.io/monitoring/index.html">netkiller monitoring 手札</a>

<a target="_top" href="http://netkiller.github.io/storage/index.html">netkiller storage 手札</a>

<a target="_top" href="http://netkiller.github.io/mail/index.html">netkiller mail 手札</a>

<a target="_top" href="http://netkiller.github.io/shell/index.html">netkiller shell 手札</a>

<a target="_top" href="http://netkiller.github.io/network/index.html">netkiller network 手札</a>

<a target="_top" href="http://netkiller.github.io/database/index.html">netkiller database 手札</a>

<a target="_top" href="http://netkiller.github.io/postgresql/index.html">netkiller postgresql 手札</a>

<a target="_top" href="http://netkiller.github.io/mysql/index.html">netkiller mysql 手札</a>

<a target="_top" href="http://netkiller.github.io/nosql/index.html">netkiller nosql 手札</a>

<a target="_top" href="http://netkiller.github.io/ldap/index.html">netkiller ldap 手札</a>

<a target="_top" href="http://netkiller.github.io/cisco/index.html">netkiller cisco ios 手札</a>

<a target="_top" href="http://netkiller.github.io/h3c/index.html">netkiller h3c 手札</a>

<a target="_top" href="http://netkiller.github.io/multimedia/index.html">netkiller multimedia 手札</a>

<a target="_top" href="http://netkiller.github.io/docbook/index.html">netkiller docbook 手札</a>

<a target="_top" href="http://netkiller.github.io/oss/index.html">netkiller 开源软件 手札</a>

目录

你是否有这样的需求:

你需要监控访问网站的ip,当同一个ip地址访问次数过多需要做出处理,例如拉黑,直接丢进iptables 防火墙规则连中。你的做法只能每个一段时间查询一次数据库,并且判断是否满足拉黑需求?

你是否需要监控某些数据发生变化,并通知其他程序作出处理。例如新闻内容修改后,需要立即做新页面静态化处理,生成新的静态页面

你使用数据库做队列,例如发送邮件,短信等等。你要通知发送程序对那些手机或者短线发送数据

需要让数据库与其他进程通信,传递信号

例如,发送短信这个需求,你只要告诉发短信的机器人发送的手机号码即可,机器人永远守候那哪里,只要命令一下立即工作。

监控数据库变化的需求原理类似,我们需要有一个守护进程等待命令,一旦接到下达命令便立即生成需要的静态页面

这里所提的方案是采用fifo(first in first out)方案,通过管道相互传递信号,使两个进程协同工作,这样的效率远比定时任务高许多。fifo是用于操作系统内部进程间通信,如果跨越操作系统需要使用socket,还有一个新名词mq(message queue).

这里只做fifo演示, 将本程序改为socket方案,或者直接集成成熟的mq也是分分钟可以实现。

我开发了几个 udf, 共4个 function

udf

fifo_create(pipename)

创建管道.成功返回true,失败返回flase.

fifo_remove(pipename)

删除管道.成功返回true,失败返回flase.

fifo_read(pipename)

读操作.

fifo_write(pipename,message)

写操作 pipename管道名,message消息正文.

有了上面的function后你就可以在begin,commit,rollback 直接穿插使用,实现在事物处理期间做你爱做的事。也可以用在触发器与event定时任务中。

编译udf你需要安装下面的软件包

<a target="_top" href="https://github.com/netkiller/mysql-fifo-plugin">https://github.com/netkiller/mysql-fifo-plugin</a>

编译udf,最后将so文件复制到 /usr/lib/mysql/plugin/

装载

卸载

插件有很多种用法,这里仅仅一个例

我们假设有一个demo这样的表,我使用shell写了一个守护进程用于处理数据库送过来的数据

启动守护进程

监控日志,因为守护进程没有输出,完成人户后写入日志。

开始推送任务

现在看看日志的变化

我们再将上面的例子使用触发器进一步优化

测试

日志变化

我们可以采用主从数据库,将任务放在专用的从库上执行

我们可以创建很多个管道,用于做不同的工作,例如插入一个任务,更新一个任务,发短信一个任务,处理模板与静态化一个任务等等。