kafka 常用非基础的核心设置项

在测试的过程中,心血来潮,想要测试下新topic中还没被消费的消息。专门查了下ai,奈何一本正经的胡说八道,浪费了点时间。现在记录下:

  1. 解决topic缺失时项目无法启动 , 报错: Topic(s) [......] is/are not present and missingTopicsFatal is true
  2. 指定消息消费的区间
java 复制代码
@Configuration
@Primary
public class CommonKafkaConfig extends KafkaProperties {

    @Value("${spring.kafka.concurrency}")
    public int concurrency;

    @Value("${spring.kafka.poll-timeout-ms}")
    public long pollTimeout;

    @Value("${spring.kafka.consumer.auto-offset-reset}")
    private String autoOffsetReset;

    @Value("${spring.kafka.consumer.auto-commit-interval-ms}")
    private String autoCommitInterval;

    @Value("${spring.kafka.consumer.bootstrap-servers}")
    private String mpSyncBootstrapServers;

    @Value("${spring.kafka.consumer.cloud-bootstrap-servers}")
    private String cloudSyncBootstrapServers;

    @Value("${spring.kafka.consumer.group-id}")
    private String mpSyncGroupId;

    @Value("${spring.kafka.consumer.max-pool-records}")
    private String maxPoolRecords;

    @Value("${spring.profiles.active}")
    String env;

    @Bean(name = "kafkaListenerContainerFactory")
    KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<String, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<String, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(ConsumerFactory());
        factory.setConcurrency(concurrency);
        factory.getContainerProperties().setPollTimeout(pollTimeout);
       //忽略不存在的topic, 针对报警: 
       // Topic(s) [......] is/are not present and missingTopicsFatal is true
        factory.getContainerProperties().setMissingTopicsFatal(false);
        return factory;
    }

    private ConsumerFactory<String, String> ConsumerFactory() {
        return new DefaultKafkaConsumerFactory<>(ConsumerConfigs());
    }

    private Map<String, Object> ConsumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, getBootStrap());
        // 配置的值为:latest: 消费最新的消息。 
        // earliest:( 表示当没有初始偏移量或者偏移量无效时,)消费者会从最早的可用消息开始消费
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
        props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, autoCommitInterval);
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.GROUP_ID_CONFIG, mpSyncGroupId);
        props.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPoolRecords);
        return props;
    }

    public String getBootStrap() {
        if (env.contains("cloud")) return cloudSyncBootstrapServers;
        return mpSyncBootstrapServers;
    }


    private Map<String, Object> producerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, getBootStrap());
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        return props;
    }

    private ProducerFactory<String, String> producerFactory() {
        return new DefaultKafkaProducerFactory<>(producerConfigs());
    }

    @Bean(name = "commonKafkaTemplate")
    public KafkaTemplate<String, String> kafkaTemplate() {
        return new KafkaTemplate<>(producerFactory());
    }
}

问题1,日志里会有大量的[warn],如下:

2024-04-02 16:58:32.906 WARN 15652 --- [ errorHandler-4-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-69, groupId=errorHandler] Error while fetching metadata with correlation id 973 : {mp.publish.grab.high.priority=UNKNOWN_TOPIC_OR_PARTITION, mp.publish.other=UNKNOWN_TOPIC_OR_PARTITION, mp.publish.grab.low.priority=UNKNOWN_TOPIC_OR_PARTITION}

可以在logback.xml中,加一行配置,忽略日志:

XML 复制代码
<logger name="org.apache.kafka.clients.NetworkClient" level="ERROR"/>
相关推荐
百事老饼干15 分钟前
Java[面试题]-真实面试
java·开发语言·面试
customer0822 分钟前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
2402_8575893632 分钟前
SpringBoot框架:作业管理技术新解
java·spring boot·后端
HBryce2436 分钟前
缓存-基础概念
java·缓存
一只爱打拳的程序猿1 小时前
【Spring】更加简单的将对象存入Spring中并使用
java·后端·spring
杨荧1 小时前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
minDuck1 小时前
ruoyi-vue集成tianai-captcha验证码
java·前端·vue.js
为将者,自当识天晓地。1 小时前
c++多线程
java·开发语言
daqinzl1 小时前
java获取机器ip、mac
java·mac·ip