当前位置:   article > 正文

17.windows上安装kafka并简单应用

kafka 单机应用

参考:https://blog.csdn.net/yao8513277/article/details/81116718

1.单机版

1.1 安装jdk

1.2 安装zookeeper

https://my.oschina.net/springMVCAndspring/blog/3045210

1.3 安装window上安装kafka

1.3.1 下载

http://kafka.apache.org/downloads

7b5ae7e62e33f0afcb50e07e421714c6354.jpg

 

9a301287506d3f79dc5ab15187cd0c2d262.jpg

 

1.3.2 解压

33e7b0af626e921107297237bbf76d6bb44.jpg

1.3.3 建立一个空文件夹 logs

96df23d9a9dc26e91d8f61f327b48c1f58b.jpg

1.3.4 进入config目录,编辑 server.properties文件

(1)编辑log.dirs

2b522daf40feb9c91a3ec06ceaafd760047.jpg

(2)编辑zookeeper.connect

2ea625b2b4bbb3b6e36fd272a434519f55a.jpg

1.4 启动kafka

(1)启动zookeeper

0bc63939df7f23f9a05febe2a8e30118a8e.jpg

(2)启动kafka

  .\bin\windows\kafka-server-start.bat .\config\server.properties

 

 

69d5ebceb110501bd0396a27e21092e6c12.jpg

 

e88c24c8d36c67c7a7aee7404f6f2f86e44.jpg

 

2.kafka整合springboot

效果:

b0ef5bec10681cc324267d7af74da4ad399.jpg

2.1 生产者项目

2.1.1 创建 生产者项目

4f170331bcde26b9d9e22124773025bbc1d.jpg

5e59bc88e73d3aa8fcc5041e12a26654436.jpg

e0acfa35e79d17bf025b97ce71fa5860d72.jpg

 

b5933a3c8a69335a0fae2a19b1f7403fb5c.jpg

 

设置maven

e0d24447aec56e8617adbfac9ab8949924b.jpg

 

2..1.2 配置pom.xml

<!--1.web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 2.kafka -->
<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>
<!--3.lombook-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.18</version>
    <scope>provided</scope>
</dependency>
<!-- 4jackson -->
<!--4.1 定义了底层的streaming API和实现了Json特性 -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.3</version>
</dependency>
<!-- 4.2 实现了数据绑定和对象序列化,它依赖于streaming和annotations的包  -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.7.3</version>
</dependency>
<!-- 4.3 包含了标准的Jackson注解。本文暂不介绍 -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.7.3</version>
</dependency>

936dabcd4ee7371b8095a3b971949fece74.jpg

2.1.3 配置文件

dd8cd1f56b1d3eb4c9d834dde79070fc75e.jpg

 

2.1.4 编写代码

这里只简单的编写controller层代码

2.1.4.1 domain

4d032dbfc58136b1da40166614951957379.jpg

 

2.1.4.2 controller
@Resource
private KafkaTemplate<String, String> kafkaTemplate;

//发送消息方法
@ResponseBody
@RequestMapping("/send")
public void send(String msg) {
    Message message = new Message();
    message.setId(UUIDUtils.returnUuid());
    message.setMsg(msg);
    message.setSendTime(TimeUtils.getStringDate(new Date()));
    try {
        kafkaTemplate.send("kafka0620", JacksonUtils.objectToJson(message));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}

5f21624a0218a34d0b37c237399d8b8335e.jpg

 

2.2 消费者

(1)搭建项目 与生产者一模一样

(2)pom.xml相同

(3)配置文件

#项目端口
server.port=9000
#============== kafka ===================
# 指定kafka 代理地址,可以多个
spring.kafka.bootstrap-servers=localhost:9092

#=========== consumer  =======================
# 指定默认消费者group id
spring.kafka.consumer.group-id=test201906191305

spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.enable-auto-commit=true
spring.kafka.consumer.auto-commit-interval=100

# 指定消息key和消息体的编解码方式
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer

1512d2ec4138a36c7d222269a35cb45174c.jpg

 

(4) controller

@KafkaListener(topics = {"kafka0620"})
public void listen(ConsumerRecord<?, ?> record) {
    Optional<?> kafkaMessage = Optional.ofNullable(record.value());

    if (kafkaMessage.isPresent()) {

        Object message = kafkaMessage.get();
        System.err.println(record);
        System.err.println(message);

    }

}

e2361abd4c93d1cda6e976c7f9e5c2f65ea.jpg

2.3 源码

链接:https://pan.baidu.com/s/1zhbepbSKHuuDtnY9Z5DiKw 
提取码:8c59 

转载于:https://my.oschina.net/u/4132381/blog/3064078

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/1007736
推荐阅读
相关标签
  

闽ICP备14008679号