kafka(卡夫卡生产者)的使用。

发送JSON数据请查看:kafka发送JSON消息,接受JSON消息
一,创建生产者 MyProducer.java

package com.kuxingseng.lbw.mq;

import org.apache.kafka.clients.producer.*;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.errors.RetriableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import static com.kuxingseng.lbw.mq.MyProducer.IP;
import static com.kuxingseng.lbw.mq.MyProducer.PORT;

public class MyProducer {
    private final static Logger logger = LoggerFactory.getLogger(MyProducer.class);

    private String TOPIC = "LBW_EPM"; //kafka主题 把消息发布到这个主题 自定义
    protected static String IP = "192.168.199.128";//kafka 消息系统 IP
    protected static String PORT = "9092";   //kafka端口号

    private Properties properties = MyProperties.getProperties();//kafka消费者配置
    private static MyProducer producer = new MyProducer();

    public static MyProducer getInstance() {  //单例
        if (producer == null) {
            producer = new MyProducer();
        }
        return producer;
    }

    public void send() {
        Producer<String, String> producer = new KafkaProducer<>(properties);
        KafkaData data = new KafkaData(0);
        List<Map> listMap = new ArrayList<>();
        Map map = new HashMap();
        map.put("keyTest", "我是测试消息");
        listMap.add(map);
        data.setListMap(listMap);
        ProducerRecord record = new ProducerRecord<String, KafkaData>(TOPIC, data);
        producer.send(record, new Callback() {
            @Override
            public void onCompletion(RecordMetadata recordMetadata, Exception e) {
                if (e == null) {
                    logger.info("kafka send successful");
                } else {
                	//对kafka可重试异常,消息第一次发送失败再次重试。
                    if (e instanceof RetriableException) { 
                        //处理可重试异常
                        try {
                            logger.error("kafka send fail Retry sending.");
                            Thread.sleep(3000);
                            MyProducer.getInstance().send();
                        } catch (InterruptedException e1) {
                            logger.error("kafka error :", e1);
                        }
                    } else {//对于kafka来说,不可重试异常,抛出。
                        throw new KafkaException("kafka server message error.");
                    }
                }
            }
        });
        //关闭 以免造成资源泄露
        producer.close();
    }
}
//内部类的使用
class MyProperties {
    public static Properties getProperties() {
        Properties props = new Properties();
        //集群地址,多个服务器用 逗号 ","分隔
        props.put("bootstrap.servers", IP + ":" + PORT);
        //key 的序列化,此处以字符串为例,使用kafka已有的序列化类
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        //发送字符串可以使用StringSerializer这个序列化器
//        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        //使用自定义序列化器 发送自定义数据/json数据
        props.put("value.serializer", "com.kuxingseng.lbw.mq.KafkaEntityDataSerializer");
        props.put("request.required.acks", "1");
        return props;
    }
}

二,创建POJO KafkaData.java 发送自定义数据/json数据。

package com.kuxingseng.lbw.mq;

import java.util.List;
import java.util.Map;

public class KafkaData {
    private List<Map> listMap;
    private int code = 0;

    public KafkaData() {
    }

    public KafkaData(int code) {
        this.code = code;
    }

    public KafkaData(List<Map> listMap, int code) {
        this.listMap = listMap;
        this.code = code;
    }

    public List<Map> getListMap() {
        return listMap;
    }

    public void setListMap(List<Map> listMap) {
        this.listMap = listMap;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "KafkaData{" +
                "listMap=" + listMap +
                ", code=" + code +
                '}';
    }

}

三,创建 自定义KafkaEntityDataSerializer.java 序列化器。这里用的到【props.put("value.serializer", "com.kuxingseng.lbw.mq.KafkaEntityDataSerializer");

package com.kuxingseng.lbw.mq;


import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.serialization.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class KafkaEntityDataSerializer implements Serializer {

    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    private String encoding = "UTF8";
    private ObjectMapper mapper;

    @Override
    public void configure(Map map, boolean b) {
        mapper = new ObjectMapper();
    }

    @Override
    public byte[] serialize(String s, Object data) {
        try {
            return mapper.writeValueAsString(data).getBytes(encoding);
        } catch (Exception e) {
            logger.error("My Deserializer error:", e);
        }
        return null;
    }

    @Override
    public void close() {

    }


}

验证消息的接收,需要实现kafka消费者/使用脚本监听Topic主题查看消息。kafka消费者的使用
实现kafka消费者验证:
在这里插入图片描述

Logo

Kafka开源项目指南提供详尽教程,助开发者掌握其架构、配置和使用,实现高效数据流管理和实时处理。它高性能、可扩展,适合日志收集和实时数据处理,通过持久化保障数据安全,是企业大数据生态系统的核心。

更多推荐