Spring for kafka系列——1、快速浏览

先决条件:您必须安装并运行Apache Kafka。然后,您必须将Spring for Apache Kafka(Spring Kafka)JAR及其所有依赖项放在类路径上。最简单的方法是在构建工具中声明依赖关系。

如果您没有使用Spring Boot,请在项目中将Spring kafka-jar声明为依赖项。


  org.springframework.kafka
  spring-kafka
  3.3.7

使用Spring Boot时(您还没有使用start.Spring.io创建项目),省略版本,Boot将自动引入与您的Boot版本兼容的正确版本:


  org.springframework.kafka
  spring-kafka

然而,最快的入门方法是使用start.spring.io(或spring Tool Suits和Intellij IDEA中的向导)并创建一个项目,选择“spring for Apache Kafka”作为依赖项。

Compatibility

此快速教程适用于以下版本:

Apache Kafka客户端3.7.x

Spring框架6.1.x

最低Java版本:17

Getting Started

最简单的入门方法是使用start.spring.io(或spring Tool Suits和Intellij IDEA中的向导)并创建一个项目,选择“spring for Apache Kafka”作为依赖项。请参阅Spring Boot文档,了解有关其基础架构bean的自主自动配置的更多信息。

这是一个最小的消费者应用程序。

Spring Boot Consumer App

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("topic1")
                .partitions(10)
                .replicas(1)
                .build();
    }

    @KafkaListener(id = "myId", topics = "topic1")
    public void listen(String in) {
        System.out.println(in);
    }

}
spring.kafka.consumer.auto-offset-reset=earliest

NewTopic bean导致在代理上创建主题;如果主题已经存在,则不需要。

Spring Boot Producer App

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public NewTopic topic() {
        return TopicBuilder.name("topic1")
                .partitions(10)
                .replicas(1)
                .build();
    }

    @Bean
    public ApplicationRunner runner(KafkaTemplate template) {
        return args -> {
            template.send("topic1", "test");
        };
    }

}

With Java Configuration (No Spring Boot)

Spring for Apache Kafka旨在用于Spring应用程序上下文。例如,如果你在Spring上下文之外自己创建监听器容器,除非你满足所有条件,否则并非所有函数都能工作。..感知容器实现的接口。

以下是一个不使用Spring Boot的应用程序示例;它既有消费者,也有生产者。

public class Sender {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        context.getBean(Sender.class).send("test", 42);
    }

    private final KafkaTemplate template;

    public Sender(KafkaTemplate template) {
        this.template = template;
    }

    public void send(String toSend, int key) {
        this.template.send("topic1", key, toSend);
    }

}

public class Listener {

    @KafkaListener(id = "listen1", topics = "topic1")
    public void listen1(String in) {
        System.out.println(in);
    }

}

@Configuration
@EnableKafka
public class Config {

    @Bean
    ConcurrentKafkaListenerContainerFactory
                        kafkaListenerContainerFactory(ConsumerFactory consumerFactory) {
        ConcurrentKafkaListenerContainerFactory factory =
                                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory);
        return factory;
    }

    @Bean
    public ConsumerFactory consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerProps());
    }

    private Map consumerProps() {
        Map props = new HashMap<>();
        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ConsumerConfig.GROUP_ID_CONFIG, "group");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        // ...
        return props;
    }

    @Bean
    public Sender sender(KafkaTemplate template) {
        return new Sender(template);
    }

    @Bean
    public Listener listener() {
        return new Listener();
    }

    @Bean
    public ProducerFactory producerFactory() {
        return new DefaultKafkaProducerFactory<>(senderProps());
    }

    private Map senderProps() {
        Map props = new HashMap<>();
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(ProducerConfig.LINGER_MS_CONFIG, 10);
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        //...
        return props;
    }

    @Bean
    public KafkaTemplate kafkaTemplate(ProducerFactory producerFactory) {
        return new KafkaTemplate<>(producerFactory);
    }

}

正如您所看到的,在不使用Spring Boot时,您必须定义几个基础设施bean。

你可能感兴趣的:(Spring,for,Apache,Kafka,Spring,kafka)