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;
    }
}
相关推荐
苹果醋338 分钟前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
荆州克莱2 小时前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术
武昌库里写JAVA3 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
Q_19284999064 小时前
基于Spring Boot的九州美食城商户一体化系统
java·spring boot·后端
Q_19284999064 小时前
基于Spring Boot的营销项目系统
spring boot
路在脚下@6 小时前
Spring Boot @Conditional注解
java·spring boot·spring
陶庵看雪6 小时前
Spring Boot注解总结大全【案例详解,一眼秒懂】
java·spring boot·后端
qq_5470261796 小时前
Kafka 常见问题
kafka
core5126 小时前
flink sink kafka
flink·kafka·sink
Q_19284999067 小时前
基于Spring Boot的图书管理系统
java·spring boot·后端