• spring boot 集成kafka


    1、先解决依赖

    springboot相关的依赖我们就不提了,和kafka相关的只依赖一个spring-kafka集成包

    <dependency>
                <groupId>org.springframework.kafka</groupId>
                <artifactId>spring-kafka</artifactId>
                <version>2.2.0.RELEASE</version>
    </dependency>

     这里我们先把配置文件展示一下

    复制代码
    #============== kafka producer===================
    spring.kafka.producer.bootstrap-servers=localhost:9092
    spring.kafka.producer.retries=1
    spring.kafka.producer.batch-size=16384
    spring.kafka.producer.buffer-memory=33554432
    spring.kafka.producer.properties.max.requst.size=2097152
    spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
    spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
    #=============== consumer =======================
    spring.kafka.consumer.bootstrap-servers=localhost:9092
    spring.kafka.consumer.group-id=0
    spring.kafka.consumer.auto-offset-reset=earliest
    spring.kafka.consumer.enable-auto-commit=true
    spring.kafka.consumer.auto-commit-interval=100
    #=======set comsumer max fetch.byte 2*1024*1024=============
    spring.kafka.consumer.properties.max.partition.fetch.bytes=2097152
    复制代码

    2、Kafka producer 

    复制代码
    @Order(value = 1)
    @Component
    @Slf4j
    public class Producer implements CommandLineRunner {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Override
    public void run(String... strings) throws Exception {
    while (true) {
    log.info("kafka的消息");
    kafkaTemplate.send("test", "zhugezifang");
    log.info("发送kafka成功.");
    Thread.sleep(5000);
    }

    }
    }
    复制代码

    3、kafka consumer

    复制代码
    @Component
    @Slf4j
    public class Consumer {

    @KafkaListener(topics = {"test"})
    public void listen(ConsumerRecord<?, ?> record) {
    log.info("topic:{}, offset:{}, value:{}", record.topic(), record.offset(), record.value());
    }
    }
    复制代码

     

  • 相关阅读:
    适配器模式
    控制器的显示注入
    自定义过滤器
    配置路由
    JavaScript判断浏览器类型及版本(新增IE11)
    路径别名
    判断是否是IE浏览器和是否是IE11
    页面视图中的按钮操作指向
    安卓开发环境搭建与环境变量设置
    html中iframe子页面与父页面元素的访问以及js变量的访问
  • 原文地址:https://www.cnblogs.com/zgzf/p/10819891.html
Copyright © 2020-2023  润新知