http://blog.haohtml.com/archives/14496
實現功能:用go實現消息隊列的寫入與讀取(打算用在發送郵件服務)
環境工具:
Centos 64X 6.4
zeromq 3.2.4:zeromq.org
golang:http://golang.org/
一.安裝golang(http://golang.org/doc/install)
這一步很簡單,只需要從http://code.google.com/p/go/downloads 下載到服務器,解壓到/usr/local/go目錄,再設置一下系統變量就可以了.
設置系統變量GOROOT
Add /usr/local/go/bin
to the PATH
environment variable. You can do this by adding this line to your /etc/profile
(for a system-wide installation) or $HOME/.profile
:
export PATH=$PATH:/usr/local/go/bin
執行命令 #source /etc/profile 使環境變量生效.
設置項目環境變量GOPATH
下面我們需要設置開發項目使用的環境變量GOPATH的路徑.可直接在命令行下執行下面的命令,也可以將下面的命令寫入/etc/profile(或者 $HOME/.profile)中,這樣下次使用的時候變量不會丟失.參考:http://golang.org/doc/code.html
$ mkdir $HOME/go
For convenience, add the workspace's bin subdirectory to your PATH:
也要以將上面兩條命令寫到/etc/profile里,然后再執行 #source /etc/profile 命令,使環境變量生效.
到此為止,go的配置基本上已經完成.可以用 go env 命令查看相關的配置信息.

二.安裝zeromq
參考:http://blog.haohtml.com/archives/13798,這里使用的是zeromq 2.3.4版本
三.安裝gozmq
如果沒有安裝git的話,先安裝一下.參考:http://blog.haohtml.com/archives/10093
這里用的是https://github.com/alecthomas/gozmq提供的庫.
如果出現
"# pkg-config --cflags libzmq libzmq libzmq libzmqPackage libzmq was not found in the pkg-config search path.Perhaps you should add the directory containing `libzmq.pc'to the PKG_CONFIG_PATH environment variableNo package 'libzmq' foundPackage libzmq was not found in the pkg-config search path.Perhaps you should add the directory containing `libzmq.pc'to the PKG_CONFIG_PATH environment variableNo package 'libzmq' foundPackage libzmq was not found in the pkg-config search path.Perhaps you should add the directory containing `libzmq.pc'to the PKG_CONFIG_PATH environment variableNo package 'libzmq' foundPackage libzmq was not found in the pkg-config search path.Perhaps you should add the directory containing `libzmq.pc'to the PKG_CONFIG_PATH environment variableNo package 'libzmq' foundexit status 1"
錯誤,提示沒有在pkgconfig路徑找到libzmq.pc文件,主要是pkgconfig路徑的問題,只要配置一下pkgconfig目錄給用戶環境變量PKG_CONFIG_PATH即可.
四.程序開發
直接將https://github.com/alecthomas/gozmq提供的兩段代碼寫到zmqServer.go 和 zmqClient.go兩個文件即可.(也可以直接下載提供的實例https://github.com/alecthomas/gozmq/tree/master/examples)
這個時候開兩個終端.一個執行服務端,一個執行客戶端.
server端:
注意這個時候,不會有什么東西輸出的,因為客戶商沒有任何寫入操作.下面執行客戶端.注意看一下server端的變化
客戶端:
這個時候會發現客戶端有10個寫入操作(生產者),而服務端有10個輸出操作(消費者).說明一切正常,否則請檢查上面的操作是否有誤.


至于下面的操作,看開發者怎么使用了.
posted on 2016-09-22 17:33
思月行云 閱讀(2417)
評論(0) 編輯 收藏 引用 所屬分類:
Golang 、
分布式\MQ