spring boot 配置kafka,测试代码

1、安装依赖

<dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>

2、配置文件

spring.kafka.bootstrap-servers:localhost:9092

spring.kafka.producer.key-serializer:org.apache.kafka.common.serialization.StringSerializer 

spring.kafka.producer.value-serializer: org.springframework.kafka.support.serializer.JsonSerializer 

spring.kafka.consumer.group-id=testGroup
spring.kafka.consumer.key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer: org.springframework.kafka.support.serializer.JsonDeserializer

3、生产者代码

@Service
public class ProviderService {
    public static final String TOPIC = "xxxxxxx";

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    public void send(String key,String message) {
        // 发送消息
        kafkaTemplate.send(TOPIC, key,message);
        System.out.println("Provider= " + message);
    }
}

4、消费者测试代码

package com.example.hat.service.Impl;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class ConsumerService {

    @KafkaListener(topics = {"xxxxxxxx"})
    public void onMessage1(ConsumerRecord<?, ?> record){
        // 消费的哪个topic、partition的消息,打印出消息内容
        System.out.println("简单消费:"+record.topic()+"-"+record.partition()+"-"+record.key()+"-"+record.value());
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将Spring BootKafka集成在一起,需要进行以下配置: 1. 首先,需要在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.7.2</version> </dependency> ``` 2. 然后,在application.properties文件中添加以下配置: ``` spring.kafka.bootstrap-servers=<kafka服务器地址> spring.kafka.consumer.group-id=<消费者组ID> spring.kafka.consumer.auto-offset-reset=earliest ``` 其中,`bootstrap-servers`指定了Kafka服务器的地址,`consumer.group-id`指定了消费者组的ID,`consumer.auto-offset-reset`指定了消费者从哪个位置开始消费消息。 3. 接下来,需要创建一个KafkaTemplate bean,用于向Kafka发送消息。在配置类中添加以下代码: ``` @Bean public KafkaTemplate<String, String> kafkaTemplate() { return new KafkaTemplate<>(producerFactory()); } @Bean public ProducerFactory<String, String> producerFactory() { Map<String, Object> configProps = new HashMap<>(); configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "<kafka服务器地址>"); configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); return new DefaultKafkaProducerFactory<>(configProps); } ``` 4. 最后,需要创建一个KafkaListener bean,用于监听来自Kafka的消息。在配置类中添加以下代码: ``` @KafkaListener(topics = "<要监听的topic>") public void listen(ConsumerRecord<String, String> record) { // 处理消息 } ``` 其中,`topics`指定了要监听的topic。 以上就是Spring Boot整合Kafka的基本配置。需要注意的是,Kafka配置非常灵活,可以根据具体需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值