Spring Cloud Stream Kafka(3.2.2版本)

问题

正在尝试只用Spring Cloud Stream Kafka。

结果

我用的这个版本Spring Cloud Stream Kafka(3.2.2版本),在NACOS2.0.2版本下面,依然不能在NACOS服务器上面配置Spring Cloud Stream Kafka相关配置。而且,就算是Spring Cloud Stream Kafka本地配置,也出现多个消费者和AdminClient无法禁用的问题。算了,我还是用回Spring Boot 的Kafka库。下面,仅记录Spring Cloud Stream Kafka配置过程,万不可用于生产环境。待以后有缘份,用新版本的Spring Cloud Stream Kafka再来看看。

步骤

配置

spring:
  cloud:
    function:
      definition: project2Building    
    stream:
      kafka:
        binder:
          brokers: xxxx:9002
          configuration:
            # 反序列化配置
            key.serializer: org.apache.kafka.common.serialization.StringDeserializer
            value.deserializer: org.apache.kafka.common.serialization.StringDeserializer
            # JAAS配置
            security.protocol: SASL_PLAINTEXT
            sasl.mechanism: PLAIN
            sasl.jaas.config: "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"xxx\" password=\"xxx\";"
          autoCreateTopics: false
      bindings:
        # 自定义消费bean的方法名称
        project2Building-in-0:
          # 消费组
          group: xxxx
          # 主题
          destination: xxxx
          consumer:
            autoCommitOffset: false
            ackMode: MANUAL
            startOffset: earliest
            pollTimeout: 30

消费方法

package xxxxx.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;

import java.util.function.Consumer;


/**
 * 与xxxx的kafka对接处理
 * @author zyl
 */
@Slf4j
@Configuration
public class MainConfig {

    @Bean
    public Consumer<Message<String>> project2Building(){
        return msg ->{
            log.info(String.format("Kafka消息:%s",msg.getPayload()));
            // TODO 手动提交kafka
            Acknowledgment acknowledgment = msg.getHeaders().get(KafkaHeaders.ACKNOWLEDGMENT, Acknowledgment.class);
            if (acknowledgment != null) {
                acknowledgment.acknowledge();
            }
        };
    }

}

参考:

  • Spring Cloud Stream Kafka Binder Reference Guide
  • Kafka With Spring Cloud Streams Using Function-based Mode
  • Spring Cloud Stream - functional and reactive

你可能感兴趣的:(kafka,spring,cloud)