Spring Boot Kafka
2024-10-13 18:34:02
1、创建集群
http://kafka.apache.org/documentation/#quickstart
有一句我觉得特别重要: For Kafka, a single broker is just a cluster of size one.
1.1、命令行操作
#解压文件
tar -zxf kafka_2.-1.1..tgz
cd kafka_2.-1.1. #启动Zookeerper
bin/zookeeper-server-start.sh config/zookeeper.properties #启动Kafka
bin/kafka-server-start.sh config/server.properties &
cp config/server.properties config/server-.properties
cp config/server.properties config/server-.properties config/server-.properties:
broker.id=
listeners=PLAINTEXT://:
log.dir=/tmp/kafka-logs- config/server-.properties:
broker.id=
listeners=PLAINTEXT://:
log.dir=/tmp/kafka-logs- bin/kafka-server-start.sh config/server-.properties &
bin/kafka-server-start.sh config/server-.properties & #创建集群
bin/kafka-topics.sh --create --zookeeper localhost: --replication-factor --partitions --topic myTopic #查看主题
bin/kafka-topics.sh --describe --zookeeper localhost: --topic myTopic
1.2、图形化界面操作
除了命令行以为,也可以通过kafka-manager查看
2、Spring Boot集成Kafka
2.1、引入Maven依赖
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
2.2、配置
spring:
kafka:
bootstrap-servers: 10.123.52.76:9092,10.123.52.76:9093,10.123.52.76:9094
consumer:
group-id: myGroup
2.3、收发消息
package com.cjs.boot.message; import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component; @Slf4j
@Component
public class MyListener { @KafkaListener(topics = "myTopic")
public void processMessage2(String content) {
log.info("【Received Message From 'myTopic'】: {}", content);
} }
package com.cjs.boot.controller; import com.cjs.boot.response.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Controller;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView; @Controller
@RequestMapping("/message")
public class MessageController extends BaseController { @Autowired
private KafkaTemplate<String, String> kafkaTemplate; @GetMapping("/add.html")
public ModelAndView add() {
return new ModelAndView("message/add");
} @PostMapping("/send.json")
@ResponseBody
public RespResult send(String text) {
ListenableFuture<SendResult<String, String>> result = kafkaTemplate.send("myTopic", String.valueOf(System.currentTimeMillis()), text);
return RespResult.success();
} }
2018-05-04 12:36:59.736 INFO 7552 --- [nio-8080-exec-2] o.a.kafka.common.utils.AppInfoParser : Kafka version : 1.0.1
2018-05-04 12:36:59.736 INFO 7552 --- [nio-8080-exec-2] o.a.kafka.common.utils.AppInfoParser : Kafka commitId : c0518aa65f25317e2018-05-04 12:36:59.830 INFO 7552 --- [ntainer#0-0-C-1] com.cjs.boot.message.MyListener : 【Received Message From 'myTopic'】: 大家好啊
2018-05-04 12:37:24.107 INFO 7552 --- [ntainer#0-0-C-1] com.cjs.boot.message.MyListener : 【Received Message From 'myTopic'】: 吃饭啦
2.4、截图
最新文章
- c#生成二维码
- 清华学堂 Range
- POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)
- Week12(11月28日)
- MVC 小案例 -- 信息管理
- Windows7 x64 跨平台开发环境安装配置
- python 错误记录
- extremecomponents
- es2018(es9)前瞻
- 【THUWC2017】随机二分图(动态规划)
- 2018.4.27 java容器
- 2、JDBC-CURD
- maven配置jdk1.8环境
- plsql developer连接Oracle报错ORA-12154: TNS:could not resolve the connect identifier specified
- [内核驱动] VS2012+WDK 8.0 Minifilter实现指定扩展名文件拒绝访问
- mybatis结合mysql批量操作及查询sql
- apache2.4配置多个端口对应多个目录
- keras—多层感知器识别手写数字算法程序
- ASL测试 课题测试博客
- 在JTextField中监听回车键,并执行相应按钮