系統間通訊方式之(Kafka的叢集方案介紹1)(二十)__Kafka

來源:互聯網
上載者:User
4、Kafka及特性

Apache Kafka最初由LinkedIn貢獻,目前它是Apache下的一個頂級開源項目。Apache Kafka設計的首要目標是解決LinkedIn網站中海量的使用者操作行為記錄、頁面瀏覽記錄,後繼的Apache Kafka版本也都是將“滿足高資料輸送量”作為版本最佳化的首要目標。為了達到這個目標,Apache Kafka甚至在其他功能方面上做了一定的犧牲,例如:訊息的事務性。如果您的系統需要進行單位時間內大量的資料擷取工作,那麼可以考慮在您的系統設計方案中加入Apache Kafka。 4-1、Kafka叢集安裝 4-1-1、安裝環境介紹

Apache Kafka的安裝過程非常簡單。為了節約篇幅我不準備像介紹Apache ActiveMQ那樣,專門花費筆墨來介紹它的單機(單服務節點)安裝過程和最簡單的生產者、消費者的編碼過程。而是換一種思路:

直接介紹Apache Kafka多節點叢集的安裝過程,並且在這個Apache Kafka叢集中為新的Topic劃分多個分區,示範Apache Kafka的訊息負載平衡原理。可能在這個過程中,我會使用一些您還不太瞭解的詞語(或者某些操作您暫時不會理解其中的原因),但是沒有關係,您只需要按照我給出的步驟一步一步的做——這些詞語和操作會在後文被逐一解釋。

首先我們列出將要安裝的Kafka叢集中需要的服務節點,以及每個服務節點在其中的作用:

節點位置 節點作用
192.168.61.139 Apache Kafka Brocker 1
192.168.61.138 Apache Kafka Brocker 2
192.168.61.140 zookeeper server

在這個Apache Kafka叢集安裝的示範執行個體中,我們準備了兩個Apache Kafka的Brocker服務節點,並且使用其中一個節點充當zookeeper的運行節點。

Apache Kafka叢集需要使用Zookeeper服務進行協調工作,所以安裝Apache Kafka前需要首先安裝和運行Zookeeper服務。由於這邊文章主要介紹的是Apache Kafka的工作原理,所以怎樣安裝和使用Zookeeper的內容就不再進行贅述了,不清楚的讀者可以參考我另一篇文章:《hadoop系列:zookeeper(1)——zookeeper單點和叢集安裝》。這裡我們運行zookeeper只是使用了zookeeper服務的單節點工作模式,如果您需要在實際生產環境運行Apache Kafka叢集,那麼zookeeper clusters的服務節點數量至少應該是3個(且使用不同的物理機)。 4-1-2、Kafka叢集安裝過程 首先我們在192.168.61.140的伺服器上安裝Zookeeper以後,直接啟動zookeeper即可:

zkServer.sh start
1 您可以在Apache Kafka的官網下載V0.8.X版本的安裝包(http://kafka.apache.org/downloads.html),請不要下載V0.9.X版本的安裝包,因為V0.9.X版本中消費者端的配置屬性發生了相當的變化。我們本章節的講解將基於V0.8.1.1版本,並且全部針對V0.8.X版本相容的配置屬性(https://www.apache.org/dyn/closer.cgi?path=/kafka/0.8.1.1/kafka_2.10-0.8.1.1.tgz)。

您可以直接使用wget命令,也可以通過瀏覽器(或者第三方軟體)下載:

wget https://www.apache.org/dyn/closer.cgi?path=/kafka/0.8.1.1/kafka_2.10-0.8.1.1.tgz
1 下載後,運行命令進行壓縮檔的解壓操作:
tar -xvf ./kafka_2.10-0.8.1.1.tgz
1

筆者習慣將可運行軟體放置在/usr目錄下,您可以按照您自己的操作習慣或者您所在團隊的規範要求放置解壓後的目錄(正式環境下不建議使用root帳號運行Kafka):

mv /root/kafka_2.10-0.8.1.1 /usr/kafka_2.10-0.8.1.1/
1 Apache Kafka所有的管理命令都存放在安裝路徑下的./bin目錄中。所以,如果您希望後續管理方便就可以設定一下環境變數:
export PATH=/usr/kafka_2.10-0.8.1.1/bin:$PATH#記得在/etc/profile檔案的末尾加入相同的設定
1 2 Apache Kafka的設定檔存放在安裝路徑下的./config目錄下。如下所示:
-rw-rw-r--. 1 root root 1202 4月  22 2014 consumer.properties-rw-rw-r--. 1 root root 3828 4月  22 2014 log4j.properties-rw-rw-r--. 1 root root 2217 4月  22 2014 producer.properties-rw-rw-r--. 1 root root 5322 4月  28 23:32 server.properties-rw-rw-r--. 1 root root 3326 4月  22 2014 test-log4j.properties-rw-rw-r--. 1 root root  995 4月  22 2014 tools-log4j.properties-rw-rw-r--. 1 root root 1023 4月  22 2014 zookeeper.properties
1 2 3 4 5 6 7

如果您進行的是Apache Kafka叢集安裝,那麼您只需要關心“server.properties”這個設定檔(其他設定檔的作用,我們後續會討論到)。

其中目錄下有一個zookeeper.properties不建議使用。之所以有這個設定檔,是因為Kafka中帶有一個zookeeper運行環境,如果您使用Kafka中的“zookeeper-server-start.sh”命令啟動這個內建zookeeper環境,才會用到這個設定檔。 開始編輯server.properties設定檔。這個設定檔中預設的配置項就有很多,但是您不必全部變更。下面我們列舉了更改後的設定檔情況,其中您需要主要關心的屬性使用中文進行了說明(當然原有的注釋也會進行保留):

# The id of the broker. This must be set to a unique integer for each broker.# 非常重要的一個屬性,在Kafka叢集中每一個brocker的id一定要不一樣,否則啟動時會報錯broker.id=2# The port the socket server listens onport=9092# Hostname the broker will bind to. If not set, the server will bind to all interfaces#host.name=localhost# The number of threads handling network requestsnum.network.threads=2# The number of threads doing disk I/O# 故名思議,就是有多少個線程同時進行磁碟IO操作。# 這個值實際上並不是設定得越大效能越好。# 在我後續的“儲存”專題會講到,如果您提供給Kafka使用的檔案系統物理層只有一個磁頭在工作# 那麼這個值就變得沒有任何意義了num.io.threads=8# The send buffer (SO_SNDBUF) used by the socket serversocket.send.buffer.bytes=1048576# The receive buffer (SO_RCVBUF) used by the socket serversocket.receive.buffer.bytes=1048576# The maximum size of a request that the socket server will accept (protection against OOM)socket.request.max.bytes=104857600# A comma seperated list of directories under which to store log files# 很多開發人員在使用Kafka時,不重視這個屬性。# 實際上Kafka的工作效能絕大部分就取決於您提供什麼樣的檔案系統log.dirs=/tmp/kafka-logs# The default number of log partitions per topic. More partitions allow greater# parallelism for consumption, but this will also result in more files across the brokers.num.partitions=2# The number of messages to accept before forcing a flush of data to disk# 從Page Cache中將訊息正式寫入磁碟上的閥值:以待轉儲訊息數量為依據#log.flush.interval.messages=10000# The maximum amount of time a message can sit in a log before we force a flush# 從Page Cache中將訊息正式寫入磁碟上的閥值:以轉儲間隔時間為依據#log.flush.interval.ms=1000# The minimum age of a log file to be eligible for deletion# log訊息資訊儲存時間長度,預設為168個小時log.retention.hours=168# A size-based retention policy for logs. Segments are pruned from the log as long as the remaining# segments don't drop below log.retention.bytes.# 預設為1GB,在此之前log檔案不會執行刪除策略# 實際環境中,由於磁碟空間根本不是問題,並且記憶體空間足夠大。所以筆者會將這個值設定的較大,例如100GB。#log.retention.bytes=1073741824# The maximum size of a log segment file. # When this size is reached a new log segment will be created.# 預設為512MB,當達到這個大小,Kafka將為這個Partition建立一個新的分段檔案log.segment.bytes=536870912# The interval at which log segments are checked to see if they can be deleted according # to the retention policies# 檔案刪除的保留原則,多久被檢查一次(單位毫秒)# 實際生產環境中,6-12小時檢查一次就夠了log.retention.check.interval.ms=60000# By default the log cleaner is disabled and the log retention policy will default to just delete segments after their retention expires.# If log.cleaner.enable=true is set the cleaner will be enabled and individual logs can then be marked for log compaction.log.cleaner.enable=false############################# Zookeeper ############################## Zookeeper connection string (see zookeeper docs for details).# root directory for all kafka znodes.# 到zookeeper的串連資訊,如果有多個zookeeper服務節點,則使用“,”進行分割# 例如:127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002zookeeper.connect=192.168.61.140:2181# Timeout in ms for connecting to zookeeper# zookeeper連線逾時時間zookeeper.connection.timeout.ms=1000000
1 2 3 4 5 6 7 8 9 10

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.