<!DOCTYPE html><html><head><title>Kafka 入门教程之一: 安装</title><meta charset='utf-8'><link href='https://cdn.maxiang.io/res-min/themes/marxico.css' rel='stylesheet'><style>
.note-content  {font-family: 'Helvetica Neue', Arial, 'Hiragino Sans GB', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, Song, sans-serif;}
</style></head><body><div id='preview-contents' class='note-content'>
                        
                   

<h2 id="kafka-入门教程之一-安装">Kafka 入门教程之一: 安装</h2>

<h4 id="1-官网下载软件">1.  官网下载软件</h4>
<p><a href="http://kafka.apache.org/" target="_blank">http://kafka.apache.org/</a> </p>

<p><img longdesc="./1543296020347.png" alt="Alt text" title="" type="image/png" src="https://img-blog.csdnimg.cn/2022010705415375303.png" class=""></p>

<h4 id="2-安装单节点kafka">2.  安装单节点kafka</h4>

<h5 id="a-创建用户kafka">a) 创建用户kafka</h5>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[root@tjtestrac1 ~]<span class="hljs-comment"># useradd kafka </span>
</div><div class="hljs-line">[root@tjtestrac1 ~]<span class="hljs-comment"># passwd kafka</span>
</div><div class="hljs-line">Changing password <span class="hljs-keyword">for</span> user kafka.
</div><div class="hljs-line">New password: 
</div><div class="hljs-line">BAD PASSWORD: The password is shorter than <span class="hljs-number">8</span> characters
</div><div class="hljs-line">Retype new password: 
</div><div class="hljs-line">passwd: all authentication tokens updated successfully.
</div></code></pre>

<h4 id="b-解压软件包">b)  解压软件包</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 ~]$ ls
</div><div class="hljs-line">kafka_2.<span class="hljs-number">12</span>-<span class="hljs-number">2.1</span>.<span class="hljs-number">0</span>.tgz
</div><div class="hljs-line">[kafka@tjtestrac1 ~]$ tar -xvf kafka_2.<span class="hljs-number">12</span>-<span class="hljs-number">2.1</span>.<span class="hljs-number">0</span>.tgz
</div></code></pre>

<h4 id="c-配置环境变量-kafkahome-和-javahome">c)  配置环境变量 kafka_home 和 java_home</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 ~]$ vi .bash_profile
</div><div class="hljs-line">export KAFKA_HOME=/home/kafka/kafka_2.<span class="hljs-number">12</span>-<span class="hljs-number">2.1</span>.<span class="hljs-number">0</span>
</div><div class="hljs-line">export JAVA_HOME=/u02/cassandra/java/jdk1.<span class="hljs-number">8.0</span>_162
</div><div class="hljs-line">PATH=<span class="hljs-variable">$PATH:</span><span class="hljs-variable">$HOME</span>/.local/bin:<span class="hljs-variable">$HOME</span>/bin:<span class="hljs-variable">$JAVA_HOME</span>/bin:<span class="hljs-variable">$KAFKA_HOME</span>/bin [kafka@tjtestrac1 ~]$ source .bash_profile 
</div></code></pre>

<h4 id="3-启动zookeeper-kafka-组件">3.  启动zookeeper, kafka 组件</h4>

<h4 id="a-为zookeeper-创建文件夹">a)  为zookeeper 创建文件夹</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 config]$ mkdir -p /home/kafka/zk 
</div></code></pre>

<h4 id="b-修改kafka-内置的zk-配置文件">b)  修改kafka 内置的zk 配置文件</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 config]$ vi zookeeper.properties 
</div><div class="hljs-line">dataDir=/home/kafka/zk
</div><div class="hljs-line"><wbr>
</div><div class="hljs-line"><span class="hljs-comment"># the port at which the clients will connect</span>
</div><div class="hljs-line">clientPort=<span class="hljs-number">2181</span>
</div><div class="hljs-line"><span class="hljs-comment"># disable the per-ip limit on the number of connections since this is a non-production config</span>
</div><div class="hljs-line">maxClientCnxns=<span class="hljs-number">0</span>
</div></code></pre>

<h4 id="c-启动zk">c)  启动zk</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">kafka@tjtestrac1 config]$ zookeeper-server-start.sh <span class="hljs-variable">$KAFKA_HOME</span>/config/zookeeper.properties &amp; 
</div></code></pre>

<h4 id="d-查看后台进程">d)  查看后台进程</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 ~]$ jps
</div><div class="hljs-line"><span class="hljs-number">30323</span> QuorumPeerMain
</div><div class="hljs-line"><span class="hljs-number">30660</span> Jps
</div></code></pre>

<h4 id="4-启动kafka">4.  启动kafka</h4>

<h4 id="a-为kafka-创建文件夹">a)  为kafka 创建文件夹</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 ~]$ mkdir -p /home/kafka/kf
</div></code></pre>

<h4 id="b-修改kafka-的配置文件">b)  修改kafka 的配置文件</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 config]$ vi server.properties 
</div><div class="hljs-line">log.dirs=/home/kafka/kf 
</div></code></pre>

<h4 id="c-启动kafka-进程">c)  启动kafka 进程</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 config]$ kafka-server-start.sh <span class="hljs-variable">$KAFKA_HOME</span>/config/server.properties &amp; 
</div></code></pre>

<h4 id="d-查看后台进程-1">d)  查看后台进程</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 config]$ jps
</div><div class="hljs-line"><span class="hljs-number">30323</span> QuorumPeerMain
</div><div class="hljs-line"><span class="hljs-number">4739</span> Kafka
</div><div class="hljs-line"><span class="hljs-number">5414</span> Jps
</div></code></pre>

<h4 id="5-创建一个主题topic">5.  创建一个主题topic</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 config]$ kafka-topics.sh --create --zookeeper localhost:<span class="hljs-number">2181</span> --replication-factor <span class="hljs-number">1</span> --partitions <span class="hljs-number">1</span> --topic test
</div><div class="hljs-line"><wbr>
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">27</span>:<span class="hljs-number">47</span>,<span class="hljs-number">517</span>] INFO Accepted socket connection from /<span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>:<span class="hljs-number">58228</span> (org.apache.zookeeper.server.NIOServerCnxnFactory)
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">27</span>:<span class="hljs-number">47</span>,<span class="hljs-number">519</span>] INFO Client attempting to establish new session at /<span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>:<span class="hljs-number">58228</span> (org.apache.zookeeper.server.ZooKeeperServer)
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">27</span>:<span class="hljs-number">47</span>,<span class="hljs-number">521</span>] INFO Established session <span class="hljs-number">0</span>x10052ce304e0001 with negotiated timeout <span class="hljs-number">30000</span> <span class="hljs-keyword">for</span> client /<span class="hljs-number">127.0</span>.<span class="hljs-number">0.1</span>:<span class="hljs-number">58228</span> (org.apache.zookeeper.server.ZooKeeperServer)
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">27</span>:<span class="hljs-number">47</span>,<span class="hljs-number">802</span>] INFO Got user-level KeeperException when processing sessionid:<span class="hljs-number">0</span>x10052ce304e0001 type:setData cxid:<span class="hljs-number">0</span>x4 zxid:<span class="hljs-number">0</span>x1f txntype:-<span class="hljs-number">1</span> reqpath:n/a Error Path:/config/topics/test Error:KeeperErrorCode = NoNode <span class="hljs-keyword">for</span> /config/topics/test (org.apache.zookeeper.server.PrepRequestProcessor)
</div><div class="hljs-line">Created topic <span class="hljs-string">"test"</span>.
</div></code></pre>

<h4 id="列出主题">列出主题:</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 config]$ kafka-topics.sh --list --zookeeper localhost:<span class="hljs-number">2181</span>
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">29</span>:<span class="hljs-number">31</span>,<span class="hljs-number">251</span>] INFO Accepted socket connection from /<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">1</span>:<span class="hljs-number">64976</span> (org.apache.zookeeper.server.NIOServerCnxnFactory)
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">29</span>:<span class="hljs-number">31</span>,<span class="hljs-number">253</span>] INFO Client attempting to establish new session at /<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">1</span>:<span class="hljs-number">64976</span> (org.apache.zookeeper.server.ZooKeeperServer)
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">29</span>:<span class="hljs-number">31</span>,<span class="hljs-number">255</span>] INFO Established session <span class="hljs-number">0</span>x10052ce304e0002 with negotiated timeout <span class="hljs-number">30000</span> <span class="hljs-keyword">for</span> client /<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">1</span>:<span class="hljs-number">64976</span> (org.apache.zookeeper.server.ZooKeeperServer)
</div><div class="hljs-line">test
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">29</span>:<span class="hljs-number">31</span>,<span class="hljs-number">302</span>] INFO Processed session termination <span class="hljs-keyword">for</span> sessionid: <span class="hljs-number">0</span>x10052ce304e0002 (org.apache.zookeeper.server.PrepRequestProcessor)
</div><div class="hljs-line">[<span class="hljs-number">2018</span>-<span class="hljs-number">11</span>-<span class="hljs-number">22</span> <span class="hljs-number">17</span>:<span class="hljs-number">29</span>:<span class="hljs-number">31</span>,<span class="hljs-number">303</span>] INFO Closed socket connection <span class="hljs-keyword">for</span> client /<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">0</span>:<span class="hljs-number">1</span>:<span class="hljs-number">64976</span> which had sessionid <span class="hljs-number">0</span>x10052ce304e0002 (org.apache.zookeeper.server.NIOServerCnxn)
</div></code></pre>

<h4 id="6-启动生产者来发送数据">6.  启动生产者来发送数据</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line">[kafka@tjtestrac1 ~]$ kafka-console-producer.sh --broker-list localhost:<span class="hljs-number">9092</span> --topic test
</div><div class="hljs-line">&gt;jason 
</div><div class="hljs-line">&gt;comm on
</div><div class="hljs-line">&gt;test message
</div></code></pre>

<h4 id="7-启动消费者来接收数据">7.  启动消费者来接收数据</h4>

<pre class="prettyprint hljs-dark"><code class="language-powershell hljs"><div class="hljs-line"> [kafka@tjtestrac1 ~]$ kafka-console-consumer.sh --bootstrap-server localhost:<span class="hljs-number">9092</span> --topic test --from-beginning
</div><div class="hljs-line"><wbr>
</div><div class="hljs-line">[kafka@tjtestrac1 ~]$ kafka-console-producer.sh --broker-list localhost:<span class="hljs-number">9092</span> --topic test
</div><div class="hljs-line">&gt;jason 
</div><div class="hljs-line">&gt;comm on
</div><div class="hljs-line">&gt;test message 
</div><div class="hljs-line">&gt;next message
</div></code></pre></div></body></html>