這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
nsq使用的是源碼編譯安裝。
使用源碼編譯安裝的時候建立先建一個單獨的gopath防止與原有的程式混淆
使用的環境是ubuntu14
1、先安裝git:apt-get install git
2、go環境與gopath設定直接跳過自行百度
3、安裝godep:go get github.com/kr/godep
4、安裝assert:go get github.com/bmizerany/assert
5、我是直接把nsq的原始碼下載下來,gopath下:src/github.com/nsqio/nsq下
6、一般使用godep安裝是不會成功的,需要把Godeps檔案刪除重建(這裡感覺主要作用是一步一步來解決nsq需要的依賴包):rm Godeps
7、重建,會提示缺少包這時就需要用go get把相應的包下載來下來:godep save ./...
8、如果提示缺少golang.org/下的包需要去https://github.com/golang下找把下載下來放到github.com/golang下:git clone https://github.com/golang/sys.git
9、依賴都找好後,沒有用godep安裝,而是用:make&make install
10、會在源碼的根目錄下建立一個build,在這個目錄下有服務命令,同時也自動安裝下/usr/local/bin目錄下
搭建nsqMessage Service器:
nsqlookupd:主要用於管理nsqd與topic的資訊。可以選擇性啟動,直接啟動:nohup ./nsqlookupd >> nsqlookupd.log 2>&1 &
nsqd:訊息處理的服務,用於接收轉寄訊息。可以單獨直接啟動,一般都會通過-lookupd-tcp-address參數指定nsqlookupd來完成管理。-deflate參數指定是否放棄一些特定的訊息預設是true,true時會丟失一些資訊,一般手動指定為false。啟動nsqd服務:nohup ./nsqd --lookupd-tcp-address=127.0.0.1:4160 -deflate=false >> nsqd.log 2>&1 &
nsqadmin:服務web管理介面,需要指定lookupd;啟動方式:nohup ./nsqadmin -lookupd-http-address=127.0.0.1:4161 >> nsqd.log 2>&1 &
nsqMessage Service的預設資訊:
nsqlookupd:
-http-address:監聽的http協議連接埠,預設是:0.0.0.0:4161
-tcp-address:監聽的tcp協議連接埠,預設是:0.0.0.0:4160
nsqd:
-deflate:是否放棄一些特定的訊息預設是true,如果設定為false可以保證不丟失訊息。如:只啟動發送訊息的程式時,會把訊息儲存起來,等接收訊息的程式啟動時會把這些訊息發送到相應的Consumer
-data-path:資料檔案存放位置
-http-address:Message Service的http監聽連接埠,預設是:0.0.0.0:4151
-http-client-connect-timeout:設定http連線逾時時間長度,預設2秒
-http-client-request-timeout:設定http請求逾時時間長度,預設5秒
-https-address:設定nsqd監聽的https地址,預設是:0.0.0.0:4152
-lookupd-tcp-address:指定管理nsqd的nsqlookupd的tcp監聽地址
-tcp-address:指定Message Service器的tcp監聽連接埠,預設是:0.0.0.0:4150
nsqadmin:
-http-address:指定管理nsqd web服務的地址與連接埠,預設是:0.0.0.0:4171
-http-client-connect-timeout:設定http連線逾時時間長度,預設2秒
-http-client-request-timeout:設定http請求逾時時間長度,預設5秒
-lookupd-http-address:指定nsqlookupd管理的地址
nsqd操作命令:
1、建立一個topic(post提交):http://172.16.10.79:4151/topic/create?topic=name
2、刪除一個topic(post提交):http://172.16.10.79:4151/topic/delete?topic=name
3、在topic下建立一個channel(post提交):http://172.16.10.79:4151/channel/create?topic=name&channel=name
4、在topic下刪除一個channel(post提交):http://172.16.10.79:4151/channel/delete?topic=name&channel=name
5、清空一個topic(post提交):http://172.16.10.79:4151/topic/empty?topic=name
6、在topic下清空一個channel(post提交):http://172.16.10.79:4151/channel/empty?topic=name&channel=name
nsqd的http命令使用curl有一個限制,如果有兩個參數以上,&符號後的資訊會識別不了,一般我用的是程式通過http的post請求來完成。如下:
str := "http://172.16.10.79:4151/channel/create?topic=dubing&channel=binge"
resp, err := http.Post(str,
"application/x-www-form-urlencoded",
nil)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
}
fmt.Println(string(body))
golang用戶端來完成發送與接收訊息(使用"github.com/nsqio/go-nsq"包)
func Producer() {
p, err := nsq.NewProducer("172.16.10.79:4150", nsq.NewConfig())
if err != nil {
fmt.Println(err)
panic(err)
}
for {
if err := p.Publish("test", []byte("hello NSQ!!!"+time.Now().String())); err != nil {
fmt.Println(err)
panic(err)
}
time.Sleep(time.Second * 1)
}
}
type ConsumerT struct{}
func (self *ConsumerT) HandleMessage(msg *nsq.Message) error {
fmt.Println(string(msg.Body))
return nil
}
func Consumer() {
c, err := nsq.NewConsumer("test", "ch", nsq.NewConfig())
if err != nil {
fmt.Println(err)
panic(err)
}
//這裡也可以直接使用函數
/*c.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error {
fmt.Println(string(msg.Body))
return nil
}))*/
c.AddHandler(&ConsumerT{})
if err := c.ConnectToNSQD("172.16.10.79:4150"); err != nil {
fmt.Println(err)
panic(err)
}
<-c.StopChan
}
func main() {
waiter := sync.WaitGroup{}
waiter.Add(1)
go Consumer()
go Producer()
waiter.Wait()
}