http://blog.haohtml.com/archives/14496
實現(xiàn)功能:用go實現(xiàn)消息隊列的寫入與讀取(打算用在發(fā)送郵件服務(wù))
環(huán)境工具:
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 下載到服務(wù)器,解壓到/usr/local/go目錄,再設(shè)置一下系統(tǒng)變量就可以了.
設(shè)置系統(tǒng)變量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
執(zhí)行命令 #source /etc/profile 使環(huán)境變量生效.
設(shè)置項目環(huán)境變量GOPATH
下面我們需要設(shè)置開發(fā)項目使用的環(huán)境變量GOPATH的路徑.可直接在命令行下執(zhí)行下面的命令,也可以將下面的命令寫入/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里,然后再執(zhí)行 #source /etc/profile 命令,使環(huán)境變量生效.
到此為止,go的配置基本上已經(jīng)完成.可以用 go env 命令查看相關(guān)的配置信息.

二.安裝zeromq
參考:http://blog.haohtml.com/archives/13798,這里使用的是zeromq 2.3.4版本
三.安裝gozmq
如果沒有安裝git的話,先安裝一下.參考:http://blog.haohtml.com/archives/10093
這里用的是https://github.com/alecthomas/gozmq提供的庫.
如果出現(xiàn)
"# 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目錄給用戶環(huán)境變量PKG_CONFIG_PATH即可.
四.程序開發(fā)
直接將https://github.com/alecthomas/gozmq提供的兩段代碼寫到zmqServer.go 和 zmqClient.go兩個文件即可.(也可以直接下載提供的實例https://github.com/alecthomas/gozmq/tree/master/examples)
這個時候開兩個終端.一個執(zhí)行服務(wù)端,一個執(zhí)行客戶端.
server端:
注意這個時候,不會有什么東西輸出的,因為客戶商沒有任何寫入操作.下面執(zhí)行客戶端.注意看一下server端的變化
客戶端:
這個時候會發(fā)現(xiàn)客戶端有10個寫入操作(生產(chǎn)者),而服務(wù)端有10個輸出操作(消費者).說明一切正常,否則請檢查上面的操作是否有誤.


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