springboot 连kafka 发,接消息

引入pom

<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.13</artifactId>
            <version>3.7.0</version>
        </dependency>

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

增加yml配置

spring:
  kafka:
    bootstrap-servers: 192.168.1.30:9092
    consumer:
      group-id: test
      ack-mode: manual
      # 生产者
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
#    zookeeper:
#      connect: 192.168.1.30:2181

消费者

java 复制代码
import com.alibaba.fastjson.JSONObject;
import com.race.APPControl.domain.AlarmMessage;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

/**
 * kafka消费者测试
 */
@Component
public class Consumer {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    //@KafkaListener用于监听多个或者单个topic
    @KafkaListener(topics =  {"topic-1"}, groupId = "test")
    public void listen3 (ConsumerRecord<String, String> record) throws Exception {
        //解析接收到的数据List<Foo>及将接收到的数据入库
        List<AlarmMessage> list =  JSONObject.parseArray(record.value(), AlarmMessage.class);

        long starttime = System.currentTimeMillis();
        String sql = "INSERT INTO `alarm_message` (`alarmKey`, `alarmThreshold`, `alarmPhoto`, `alarmId`, `alarmTime`, `alarmTags`, `cityCodeStart`, `cityCodeEnd`, `addressStart`, `addressEnd`, `activeTimeStart`, `activeTimeEnd`, `longitudeStart`, `longitudeEnd`, `latitudeStart`, `latitudeEnd`, `sjyCode`, `sjyName`, `sjyType`, `personDetail`, `carDetail`, `phoneDetail`, `taskId`, `taskName`, `taskType`, `userId`, `userName`, `deptCode`, `deptName`, `bkType`, `targetType`, `bkLevel`, `hasCorrelation`, `correlationKey`, `colleagues`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?)";

        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {

            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                AlarmMessage foo = list.get(i);
                ps.setString(1, UUID.randomUUID().toString().replaceAll("-",""));
                ps.setString(2, foo.getAlarmThreshold());
                ps.setString(3, foo.getAlarmPhoto());
                ps.setString(4, foo.getAlarmId());
                ps.setString(5, foo.getAlarmTime());
                ps.setString(6, foo.getAlarmTags());
                ps.setString(7, foo.getCityCodeStart());
                ps.setString(8, foo.getCityCodeEnd());
                ps.setString(9, foo.getAddressStart());
                ps.setString(10, foo.getAddressEnd());
                ps.setString(11, foo.getActiveTimeStart());
                ps.setString(12, foo.getActiveTimeEnd());
                ps.setString(13, foo.getLongitudeStart());
                ps.setString(14, foo.getLongitudeEnd());
                ps.setString(15, foo.getLatitudeStart());
                ps.setString(16, foo.getLatitudeEnd());
                ps.setString(17, foo.getSjyCode());
                ps.setString(18, foo.getSjyName());
                ps.setString(19, foo.getSjyType());
                ps.setString(20, foo.getPersonDetail());
                ps.setString(21, foo.getCarDetail());
                ps.setString(22, foo.getPhoneDetail());
                ps.setString(23, foo.getTaskId());
                ps.setString(24, foo.getTaskName());
                ps.setString(25, foo.getTaskType());
                ps.setString(26, foo.getUserId());
                ps.setString(27, foo.getUserName());
                ps.setString(28, foo.getDeptCode());
                ps.setString(29, foo.getDeptName());
                ps.setString(30, foo.getBkType());
                ps.setString(31, foo.getTargetType());
                ps.setString(32, foo.getBkLevel());
                ps.setString(33, foo.getHasCorrelation());
                ps.setString(34, foo.getCorrelationKey());
                ps.setString(35, foo.getColleagues());
            }

            @Override
            public int getBatchSize() {
                System.err.println("接收数据条数:"+list.size());
                return list.size();
            }
        });

        long endtime = System.currentTimeMillis();
        System.out.println("foo结束!耗费时间为"+(endtime-starttime)/1000+"s:::msg:");

        System.out.printf("topic = %s, partition = %s, offset = %d, value = %s \n", record.topic(), record.partition(), record.offset(), record.value());
    }

}

工具类

java 复制代码
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import com.race.APPControl.domain.AlarmMessage;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.errors.TimeoutException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;

@Component
public class KafkaSendUtils {

    @Autowired
    private KafkaTemplate<Object, Object> kafkaTemplate;

    // 异步
    public void sendAnsyc(final List<AlarmMessage> foo) {
        ListenableFuture<SendResult<Object, Object>> future = kafkaTemplate.send("topic-1", foo);
        future.addCallback(new ListenableFutureCallback<SendResult<Object, Object>>() {
            @Override
            public void onSuccess(SendResult<Object, Object> result) {
                System.out.println("异步发送消息成功:" + result);
            }

            @Override
            public void onFailure(Throwable ex) {
                System.out.println("异步发送消息失败:"+ ex.getMessage());
            }
        });

    }

    // 同步
    public void sendSync(final List<AlarmMessage> foo) throws Exception {
        ProducerRecord<Object, Object> producerRecord = new ProducerRecord<>("topic-1", foo);
        try {
            kafkaTemplate.send(producerRecord).get(10, TimeUnit.SECONDS);
            System.out.println("同步发送成功");
        }
        catch (ExecutionException e) {
            System.out.println("同步发送消息失败:"+ e.getMessage());
        }
        catch (TimeoutException | InterruptedException e) {
            System.out.println("同步发送消息失败:"+ e.getMessage());
        }
    }
}
复制代码
controller
java 复制代码
import java.util.List;

import com.race.APPControl.domain.dto.AjaxNetResult;
import com.race.APPControl.domain.AlarmMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 测试kafka生产者
 */
@RestController
@RequestMapping("kafka")
public class KafkaProducerController {
    @Autowired
    private KafkaSendUtils kafkaSendUtils;

    @Autowired
    KafkaTemplate kafkaTemplate;

    @PostMapping("/ansyc")
    public AjaxNetResult send(@RequestBody List<AlarmMessage> list) throws Exception{

     	kafkaSendUtils.sendAnsyc(list);//**异步发送**
//     	kafkaSendUtils.sendSync(list);//**同步发送**
//      kafkaTemplate.send("topic-1", list);
        return AjaxNetResult.success();
    }
}

bean

java 复制代码
public class AlarmMessage {
    private String alarmKey;
    private String alarmThreshold;
    private String alarmPhoto;
    private String alarmId;
    private String alarmTime;
    private String alarmTags;
    private String cityCodeStart;
    private String cityCodeEnd;
    private String addressStart;
    private String addressEnd;
    private String activeTimeStart;
    private String activeTimeEnd;
    private String longitudeStart;
    private String longitudeEnd;
    private String latitudeStart;
    private String latitudeEnd;
    private String sjyCode;
    private String sjyName;
    private String sjyType;
    private String personDetail;
    private String carDetail;
    private String phoneDetail;
    private String taskId;
    private String taskName;
    private String taskType;
    private String userId;
    private String userName;
    private String deptCode;
    private String deptName;
    private String bkType;
    private String targetType;
    private String bkLevel;
    private String hasCorrelation;
    private String correlationKey;
    private String colleagues;


    public String getAlarmKey() {
        return alarmKey;
    }

    public void setAlarmKey(String alarmKey) {
        this.alarmKey = alarmKey;
    }

    public String getAlarmThreshold() {
        return alarmThreshold;
    }

    public void setAlarmThreshold(String alarmThreshold) {
        this.alarmThreshold = alarmThreshold;
    }

    public String getAlarmPhoto() {
        return alarmPhoto;
    }

    public void setAlarmPhoto(String alarmPhoto) {
        this.alarmPhoto = alarmPhoto;
    }

    public String getAlarmId() {
        return alarmId;
    }

    public void setAlarmId(String alarmId) {
        this.alarmId = alarmId;
    }

    public String getAlarmTime() {
        return alarmTime;
    }

    public void setAlarmTime(String alarmTime) {
        this.alarmTime = alarmTime;
    }

    public String getAlarmTags() {
        return alarmTags;
    }

    public void setAlarmTags(String alarmTags) {
        this.alarmTags = alarmTags;
    }

    public String getCityCodeStart() {
        return cityCodeStart;
    }

    public void setCityCodeStart(String cityCodeStart) {
        this.cityCodeStart = cityCodeStart;
    }

    public String getCityCodeEnd() {
        return cityCodeEnd;
    }

    public void setCityCodeEnd(String cityCodeEnd) {
        this.cityCodeEnd = cityCodeEnd;
    }

    public String getAddressStart() {
        return addressStart;
    }

    public void setAddressStart(String addressStart) {
        this.addressStart = addressStart;
    }

    public String getAddressEnd() {
        return addressEnd;
    }

    public void setAddressEnd(String addressEnd) {
        this.addressEnd = addressEnd;
    }

    public String getActiveTimeStart() {
        return activeTimeStart;
    }

    public void setActiveTimeStart(String activeTimeStart) {
        this.activeTimeStart = activeTimeStart;
    }

    public String getActiveTimeEnd() {
        return activeTimeEnd;
    }

    public void setActiveTimeEnd(String activeTimeEnd) {
        this.activeTimeEnd = activeTimeEnd;
    }

    public String getLongitudeStart() {
        return longitudeStart;
    }

    public void setLongitudeStart(String longitudeStart) {
        this.longitudeStart = longitudeStart;
    }

    public String getLongitudeEnd() {
        return longitudeEnd;
    }

    public void setLongitudeEnd(String longitudeEnd) {
        this.longitudeEnd = longitudeEnd;
    }

    public String getLatitudeStart() {
        return latitudeStart;
    }

    public void setLatitudeStart(String latitudeStart) {
        this.latitudeStart = latitudeStart;
    }

    public String getLatitudeEnd() {
        return latitudeEnd;
    }

    public void setLatitudeEnd(String latitudeEnd) {
        this.latitudeEnd = latitudeEnd;
    }

    public String getSjyCode() {
        return sjyCode;
    }

    public void setSjyCode(String sjyCode) {
        this.sjyCode = sjyCode;
    }

    public String getSjyName() {
        return sjyName;
    }

    public void setSjyName(String sjyName) {
        this.sjyName = sjyName;
    }

    public String getSjyType() {
        return sjyType;
    }

    public void setSjyType(String sjyType) {
        this.sjyType = sjyType;
    }

    public String getPersonDetail() {
        return personDetail;
    }

    public void setPersonDetail(String personDetail) {
        this.personDetail = personDetail;
    }

    public String getCarDetail() {
        return carDetail;
    }

    public void setCarDetail(String carDetail) {
        this.carDetail = carDetail;
    }

    public String getPhoneDetail() {
        return phoneDetail;
    }

    public void setPhoneDetail(String phoneDetail) {
        this.phoneDetail = phoneDetail;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public String getTaskType() {
        return taskType;
    }

    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDeptCode() {
        return deptCode;
    }

    public void setDeptCode(String deptCode) {
        this.deptCode = deptCode;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public String getBkType() {
        return bkType;
    }

    public void setBkType(String bkType) {
        this.bkType = bkType;
    }

    public String getTargetType() {
        return targetType;
    }

    public void setTargetType(String targetType) {
        this.targetType = targetType;
    }

    public String getBkLevel() {
        return bkLevel;
    }

    public void setBkLevel(String bkLevel) {
        this.bkLevel = bkLevel;
    }

    public String getHasCorrelation() {
        return hasCorrelation;
    }

    public void setHasCorrelation(String hasCorrelation) {
        this.hasCorrelation = hasCorrelation;
    }

    public String getCorrelationKey() {
        return correlationKey;
    }

    public void setCorrelationKey(String correlationKey) {
        this.correlationKey = correlationKey;
    }

    public String getColleagues() {
        return colleagues;
    }

    public void setColleagues(String colleagues) {
        this.colleagues = colleagues;
    }
}
相关推荐
编程重生之路2 分钟前
Springboot启动异常 错误: 找不到或无法加载主类 xxx.Application异常
java·spring boot·后端
politeboy11 分钟前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
世间万物皆对象7 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
KevinAha7 小时前
Kafka 3.5 源码导读
kafka
求积分不加C7 小时前
-bash: ./kafka-topics.sh: No such file or directory--解决方案
分布式·kafka
nathan05297 小时前
javaer快速上手kafka
分布式·kafka
qq_17448285758 小时前
springboot基于微信小程序的旧衣回收系统的设计与实现
spring boot·后端·微信小程序
代码小鑫10 小时前
A043-基于Spring Boot的秒杀系统设计与实现
java·开发语言·数据库·spring boot·后端·spring·毕业设计
真心喜欢你吖10 小时前
SpringBoot与MongoDB深度整合及应用案例
java·spring boot·后端·mongodb·spring
激流丶10 小时前
【Kafka 实战】Kafka 如何保证消息的顺序性?
java·后端·kafka