天天看點

資料庫程序間通信解決方案

中國廣東省深圳市龍華新區民治街道溪山美地

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寫了一個守護程序用于處理資料庫送過來的資料

啟動守護程序

監控日志,因為守護程序沒有輸出,完成人戶後寫入日志。

開始推送任務

現在看看日志的變化

我們再将上面的例子使用觸發器進一步優化

測試

日志變化

我們可以采用主從資料庫,将任務放在專用的從庫上執行

我們可以建立很多個管道,用于做不同的工作,例如插入一個任務,更新一個任務,發短信一個任務,處理模闆與靜态化一個任務等等。