移除冗余代码并优化Kafka配置

- 删除了KafkaConstants、KafkaConsumerConfig和KafkaProviderConfig类,这些类已被移动到其他项目中
-简化了KafkaConfig类,移除了其中的kafkaProducer方法,该方法已被移动到其他类中
- 在MqttTest类中更新了Kafka主题的硬编码值
- 移除了oneMse类,该类用于测试,目前不再需要
- 在ProtocolApplication的main方法中添加了一个空的System.out.println语句,用于调试或日志记录
dev.carData
Aaaaaaaa 2024-10-07 14:22:46 +08:00
parent 00d1eb18d1
commit e6e7e58f95
7 changed files with 8 additions and 184 deletions

View File

@ -1,54 +0,0 @@
package com.muyu.common.kafka.config;
import com.muyu.common.kafka.constants.KafkaConstants;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* kafka
*/
@Configuration
public class KafkaConsumerConfig {
@Bean
public KafkaConsumer kafkaConsumer() {
Map<String, Object> configs = new HashMap<>();
//kafka服务端的IP和端口,格式:(ip:port)
configs.put("bootstrap.servers", "60.204.221.52:9092");
//开启consumer的偏移量(offset)自动提交到Kafka
configs.put("enable.auto.commit", true);
//consumer的偏移量(offset) 自动提交的时间间隔,单位毫秒
configs.put("auto.commit.interval", 5000);
//在Kafka中没有初始化偏移量或者当前偏移量不存在情况
//earliest, 在偏移量无效的情况下, 自动重置为最早的偏移量
//latest, 在偏移量无效的情况下, 自动重置为最新的偏移量
//none, 在偏移量无效的情况下, 抛出异常.
configs.put("auto.offset.reset", "latest");
//请求阻塞的最大时间(毫秒)
configs.put("fetch.max.wait", 500);
//请求应答的最小字节数
configs.put("fetch.min.size", 1);
//心跳间隔时间(毫秒)
configs.put("heartbeat-interval", 3000);
//一次调用poll返回的最大记录条数
configs.put("max.poll.records", 500);
//指定消费组
configs.put("group.id", KafkaConstants.KafkaGrop);
//指定key使用的反序列化类
Deserializer keyDeserializer = new StringDeserializer();
//指定value使用的反序列化类
Deserializer valueDeserializer = new StringDeserializer();
//创建Kafka消费者
KafkaConsumer kafkaConsumer = new KafkaConsumer(configs, keyDeserializer, valueDeserializer);
return kafkaConsumer;
}
}

View File

@ -1,45 +0,0 @@
package com.muyu.common.kafka.config;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.serialization.Serializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* kafka
*/
@Configuration
public class KafkaProviderConfig {
@Bean
public KafkaProducer kafkaProducer() {
Map<String, Object> configs = new HashMap<>();
//#kafka服务端的IP和端口,格式:(ip:port)
configs.put("bootstrap.servers", "47.116.173.119:9092");
//客户端发送服务端失败的重试次数
configs.put("retries", 2);
//多个记录被发送到同一个分区时,生产者将尝试将记录一起批处理成更少的请求.
//此设置有助于提高客户端和服务器的性能,配置控制默认批量大小(以字节为单位)
configs.put("batch.size", 16384);
//生产者可用于缓冲等待发送到服务器的记录的总内存字节数(以字节为单位)
configs.put("buffer-memory", 33554432);
//生产者producer要求leader节点在考虑完成请求之前收到的确认数,用于控制发送记录在服务端的持久化
//acks=0,设置为0,则生产者producer将不会等待来自服务器的任何确认.该记录将立即添加到套接字(socket)缓冲区并视为已发送.在这种情况下,无法保证服务器已收到记录,并且重试配置(retries)将不会生效(因为客户端通常不会知道任何故障),每条记录返回的偏移量始终设置为-1.
//acks=1,设置为1,leader节点会把记录写入本地日志,不需要等待所有follower节点完全确认就会立即应答producer.在这种情况下,在follower节点复制前,leader节点确认记录后立即失败的话,记录将会丢失.
//acks=all,acks=-1,leader节点将等待所有同步复制副本完成再确认记录,这保证了只要至少有一个同步复制副本存活,记录就不会丢失.
configs.put("acks", "-1");
//指定key使用的序列化类
Serializer keySerializer = new StringSerializer();
//指定value使用的序列化类
Serializer valueSerializer = new StringSerializer();
//创建Kafka生产者
KafkaProducer kafkaProducer = new KafkaProducer(configs, keySerializer, valueSerializer);
return kafkaProducer;
}
}

View File

@ -1,9 +0,0 @@
package com.muyu.common.kafka.constants;
public class KafkaConstants {
public final static String KafkaTopic = "carJsons";
// public final static String KafkaGrop = "kafka_grop";
}

View File

@ -17,7 +17,8 @@ import java.util.HashMap;
* @Date2024/9/28 12:19
*/
@Configuration
public class KafkaConfig {
public class
KafkaConfig {
@Bean
public KafkaProducer kafkaProducer(){

View File

@ -1,5 +1,6 @@
package com.muyu;
import cn.hutool.json.JSONUtil;
import com.muyu.common.security.annotation.EnableMyFeignClients;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
@ -9,7 +10,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableMyFeignClients
@SpringBootApplication
public class ProtocolApplication {
public static void main(String[] args) {
System.out.println("");
SpringApplication.run(ProtocolApplication.class, args);
}
}

View File

@ -1,73 +0,0 @@
package com.muyu.cache.bean;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class oneMse {
/**
*
*/
private Long numther;
/**
*
*/
private Integer numthonre;
/**
*
*/
private Integer numtweh;
/**
*
*/
private String numtrwh;
/**
*
*/
private Integer numrrth;
/**
*
*/
private Integer numereth;
/**
*
*/
private String numth;
/**
*
*/
private Integer numttruh;
/**
*
*/
private Integer numtrert;
/**
*
*/
private Integer erg;
/**
*
*/
private Integer numtgreh;
/**
*
*/
private Integer rtetg;
/**
*
*/
private Integer geewr;
/**
*
*/
private Integer heertherh;
}

View File

@ -1,7 +1,7 @@
package com.muyu.mqtt;
import com.alibaba.fastjson2.JSONObject;
import com.muyu.common.kafka.constants.KafkaConstants;
import com.muyu.domain.CarMessage;
import com.muyu.domain.KafKaData;
@ -88,7 +88,7 @@ public class MqttTest {
.build());
}
String jsonString = JSONObject.toJSONString(kafKaDataList);
ProducerRecord<String, String> producerRecord = new ProducerRecord<>(KafkaConstants.KafkaTopic, jsonString);
ProducerRecord<String, String> producerRecord = new ProducerRecord<>("carJsons",jsonString);
kafkaProducer.send(producerRecord);
log.info("kafka投产{}", jsonString);