SSM Maven 项目中使用kafka实时接收指定topic中的消息

本文详细介绍了如何在SSM Maven项目中集成Kafka,包括Zookeeper和Kafka伪集群的安装部署,以及如何创建Producer和Consumer实现消息的实时接收。在配置过程中,特别提到了一个在配置Consumer时遇到的异常问题及其解决方法。
摘要由CSDN通过智能技术生成

大致的内容就是:zookeeper 和 kafka 伪集群的安装和部署,然后使用监听机制实时读取订阅的消息。

首先是zookeeper伪集群的安装
首先是到官网下载 zookeeper 我使用的是3.4.14的版本。

第一步:建立一个集群安装的目录,就叫zookeeper。其次,在这个目录的下面解压三份ZooKeeper,形成3个节点,每一个目录中的ZooKeeper就代表一个节点。
这样就形成了如下的安装目录结构:
/zookeeper
|----zookeeper1/
|----zookeeper2/
|----zookeeper3/

第二步: 在3个节点目录中分别建立data目录、logs目录和myid文件。
新建目录data:mkdir data
新建目录logs:mkdir logs
新建文件myid:touch data/myid
myid文件的内容是节点在集群中的编号,zookeeper1节点的编号就写成1,后边的zookeeper2的编号是2,zookeeper3的编号就是3。
按照同样的方法,依次在zookeeper2和zookeeper3上都建立以上目录和文件。

第三步:添加配置文件
cd 到 conf 目录下
cp zoo_sample.cfg zoo.cfg 将zoo_sample.cfg 重命名为 zoo.cfg

添加如下的配置文件信息。

tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/kafka/zookeeper/zookeeper1/data
dataLogDir=/kafka/zookeeper/zookeeper1/logs
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=127.0.0.1:8880:7770
server.2=127.0.0.1:8881:7771
server.3=127.0.0.1:8882:7772

针对不同zookeeper 只需要修改 dataDir dataLogDir 目录地址就可以了。
然后如果是要搭建真正的集群 只需要把server1,2,3 后面的地址改为对应的服务IP地址就可以了。

配置文件中配置项的含义:

  1. tickTime: zookeeper中使用的基本时间单位,毫秒值,比如可以设为1000,那么基本时间单位就是1000ms,也就是1s。
  2. initLimit: Follower在启动过程中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值