在文章(【事件中心 Azure Event Hub】使用Logstash消費EventHub中的event時遇見的幾種異常(TimeoutException, ReceiverDisconnectedException))中,介紹了使用Logstash連接配接EventHub時,遇見的兩種異常,但是對于如何在Linux環境中安裝Logstash,并且配置EventHub設定,啟動等,則包含在目前文章中。
安裝Logstash
首先在Logstash中選擇需要的版本,使用wget下載下傳到目前目錄中,如目前使用的版本為最新7.9.1(https://www.elastic.co/downloads/logstash)
wget https://artifacts.elastic.co/downloads/logstash/logstash-7.9.1.tar.gz //下載下傳logstash-7.9.1.tar.gz
tar xzvf logstash-7.9.1.tar.gz //解壓logstash-7.9.1.tar.gz到目前目錄
進入Logstash的檔案夾中
cd logstash-7.9.1
啟動logstash(Logstash的啟動檔案在安裝的版本檔案夾中bin目錄中)
bin/logstash -e 'input { stdin {} } output { stdout {} }'
啟動成功後的結果如:
添加EventHub配置檔案
準備條件:
- 建立好EventHub中的執行個體(EventHub Namespace -> EventHub Instance), 在Event Hub Instance中擷取連接配接字元串(Share Access Policy)
- 為Logstash單獨建立一個新的消費組
- 建立好一個Storage Account, 并複制出連接配接字元串(Azure Portal-> Blob Storage account -> Access keys.)
建立配置檔案(如eventhub.conf), 并使用VIM指令編輯内容
echo ""->eventhub.conf
vim eventhub.conf //打開eventhub.conf并編輯内容
在VIM編輯頁面,在鍵盤輸入I,是檔案進入編輯模式,複制如下内容(關鍵點替換為準備條件中的内容)
input {
azure_event_hubs {
event_hub_connections => ["Endpoint=sb://xxxx.servicebus.chinacloudapi.cn/;SharedAccessKeyName=test;SharedAccessKey=xxxxxxxx=;EntityPath=xxxxxx"]
threads => 8
decorate_events => true
consumer_group => "xxxx"
storage_connection => "DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxxxxxx=;EndpointSuffix=core.chinacloudapi.cn"
}
} output { stdout {
}
}
然後按住Esc,輸入 :wq,然後回車儲存内容并退出vim視窗。
使用 -f 并指定配置檔案,再次啟動Logstash,驗證已經連接配接到EventHub并接受Event。以上步驟,完成的指令如下:
root@lblinuxtest01:/logstash-7.9.1# echo ""->eventhub.conf //建立新檔案
root@lblinuxtest01:/logstash-7.9.1# ls
CONTRIBUTORS LICENSE.txt config eventhubtest.conf logs modules x-pack
Gemfile NOTICE.TXT data eventhubwithstorage.conf logstash-core tools
Gemfile.lock bin eventhub.conf lib logstash-core-plugin-api vendor
root@lblinuxtest01:/logstash-7.9.1# vim eventhub.conf //編輯檔案内容
root@lblinuxtest01:/logstash-7.9.1# ./bin/logstash -f eventhub.conf //使用新的配置檔案,啟動logstash
啟動結果如下:
參考資料
Logstash的Azure Event Hub Plugin文檔: https://www.elastic.co/guide/en/logstash/current/plugins-inputs-azure_event_hubs.html
當在複雜的環境中面臨問題,格物之道需:濁而靜之徐清,安以動之徐生。 雲中,恰是如此!