栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 整合kafka 入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 整合kafka 入门

springoot 整合kafka 入门

文章目录
  • springoot 整合kafka 入门
  • 简单说明
    • @KafkaListener
    • 配置类
      • 消费者
      • 生产者
      • 监听器
      • 测试类
      • 控制台
    • 消息是对象的话

 
    org.springframework.boot
    spring-boot-starter-web


    org.springframework.kafka
    spring-kafka

spring:
  kafka:
    bootstrap-servers: localhost:9092
简单说明 @KafkaListener
//可以监听多个主题
@KafkaListener(topice = "topic1,topic2....")

//通过 @Header 来获取当前消息 来自的分支
@KafkaListener(topice = "topic", groupId = "groupid")
void listen( @Payload String message,
           	 @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partiton){
  message....;
}

效果

    // 指定 接收的分区消息
    @KafkaListener(groupId = "test-consumer",
                    topicPartitions = @TopicPartition(topic = "test",
                            partitionOffsets = {
                                @PartitionOffset( partition = "0",initialOffset = "0")
                            }))
    //不指定 initialOffset的写法
    public void listen(@Payload String message,
                       @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int partition) {
        log.info("接收消息: {},partition:{}", message, partition);
    }
配置类

目前内容中使用的都是 String 简单类型的消息设置

# linux 创建 topic
./kafka-topics.sh --create --zookeeper 119.91.60.82:2181 --replication-factor 1 --partitions 1 --topic test
Created topic test.
消费者
package com.lfweixiao.demo.config;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;

import java.util.HashMap;


 
@EnableKafka
@Configuration
public class KafkaConsumerConfig {

    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;
    @Value("${spring.kafka.consumer.group-id}")
    private String consumerGroupId;
    @Value("${spring.kafka.consumer.auto-offset-reset}")
    private String autoOffsetReset;

    @Bean
    public ConsumerFactory consumerFactory() {
        HashMap props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,// 服务地址
                bootstrapServers);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, //消费组
                consumerGroupId);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, //消息策略
                autoOffsetReset);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, //key 串行化转换
                StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,//value 串行化转换
                StringDeserializer.class);
        return new DefaultKafkaConsumerFactory<>(props);
    }

    @Bean
    public ConcurrentKafkaListenerContainerFactory kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        return factory;
    }
}
生产者
package com.lfweixiao.demo.config;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

import java.util.HashMap;


@Configuration
public class KafkaProducerConfig {

    //读取服务地址
    @Value("${spring.kafka.bootstrap-servers}")
    private String bootstrapServers;

    @Bean
    public ProducerFactory producerFactory() {
        HashMap configProps = new HashMap<>();
        configProps.put(
                ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,//服务地址
                bootstrapServers);
        configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,//序列化 key
                StringSerializer.class);
        configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, //序列化 value
                StringSerializer.class);
        return new DefaultKafkaProducerFactory<>(configProps);
    }

    // 主要使用这个对象来发送消息
    @Bean
    public KafkaTemplate kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
}
监听器
package com.lfweixiao.demo.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;


@Component
public class KafkaMessageListener {

    public static final Logger log = LoggerFactory.getLogger(KafkaMessageListener.class);

    //监听 主题 test  分组为 test-consumer 的消息
    @KafkaListener(topics = "test", groupId = "test-consumer")
    public void listen(String message) {
        log.info("消息接收{}",message);
    }

}
测试类
package com.lfweixiao.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class SendMessageController {

    public static final Logger log = LoggerFactory.getLogger(SendMessageController.class);

    //发送消息的模版,key -value 都是string 类型
    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping("send/{message}")
    public void send(@PathVariable("message")String message){
        //kafkaTemplate.send("test01",message);//send 是异步方法
        ListenableFuture> future = kafkaTemplate.send("test", message);
        future.addCallback(new ListenableFutureCallback>() {
            @Override
            public void onFailure(Throwable ex) {
                log.error("消息:{}发送失败,原因:{}",message,ex.getMessage());
            }

            @Override
            public void onSuccess(SendResult result) {
                log.info("消息:{}发送成功,offset=[{}]",message,result.getRecordMetadata().offset());
            }
        });
    }
}
控制台

消息是对象的话
  • 是体力继承 Serializable
  • 转换成json。或者在设置里面设置 json格式传输
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号