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、截图

最新文章

  1. c#生成二维码
  2. 清华学堂 Range
  3. POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)
  4. Week12(11月28日)
  5. MVC 小案例 -- 信息管理
  6. Windows7 x64 跨平台开发环境安装配置
  7. python 错误记录
  8. extremecomponents
  9. es2018(es9)前瞻
  10. 【THUWC2017】随机二分图(动态规划)
  11. 2018.4.27 java容器
  12. 2、JDBC-CURD
  13. maven配置jdk1.8环境
  14. plsql developer连接Oracle报错ORA-12154: TNS:could not resolve the connect identifier specified
  15. [内核驱动] VS2012+WDK 8.0 Minifilter实现指定扩展名文件拒绝访问
  16. mybatis结合mysql批量操作及查询sql
  17. apache2.4配置多个端口对应多个目录
  18. keras—多层感知器识别手写数字算法程序
  19. ASL测试 课题测试博客
  20. 在JTextField中监听回车键,并执行相应按钮

热门文章

  1. 程序员大牛 Jeff Atwood 的两本中文书
  2. Android初级教程理论知识(第三章测试&amp;数据存储&amp;界面展现)
  3. 如何在Cocos2D游戏中实现A*寻路算法(五)
  4. Unity UGUI基础之Image
  5. iOS中 MediaPlayer framework实现视频播放 韩俊强的博客
  6. pandas小记:pandas基本设置
  7. Ajax数据返回格式问题解决
  8. 我眼中的Linux设备树(四 中断)
  9. iOS数据解析UI_14
  10. pig的grunt中shell命令不稳定,能不用尽量不用