Springboot 集成kafka 生产者和消费者配置

package com.springboot.kafka.business;

import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 生产者
 */
@Component
public class KafkaProducerBusiness {
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    public void send(String topic,String msg){
        kafkaTemplate.send(topic,msg);
    }

    public void sendCallback(String topic,String msg){
        kafkaTemplate.send(topic,msg).addCallback(
                success ->{
                    String topics = success.getRecordMetadata().topic();
                    int partition = success.getRecordMetadata().partition();
                    long offset = success.getRecordMetadata().offset();
                    System.out.println("topic:" + topics + " partition:" + partition + " offset:" + offset);
                },
                failure ->{
                    String message = failure.getMessage();
                    System.out.println(message);
                }
        );
    }

}
import com.springboot.kafka.constant.KafkaConstant;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.lang.reflect.Array;

/**
 * 消费者
 */
@Component
public class KafkaConsumerBusiness {

    /**
     * topicGroup1
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.topic,groupId = KafkaConstant.topicGroup1)
    public void getMessage1(ConsumerRecord record){
        Object values = record.value();
        System.out.println("1------------------------------------"+values);
    }

    /**
     * topicGroup2
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.topic,groupId = KafkaConstant.topicGroup2)
    public void getMessage2(ConsumerRecord record){
        Object values = record.value();
        System.out.println("2------------------------------------"+values);
    }





    /**
     *  监听多个topic   topic在application.yml配置
     * @param record
     */
    @KafkaListener(topics = {"#{'${spring.kafka.topics}'.split(',')}"},groupId = KafkaConstant.topicGroup)
    public void getMessage3(ConsumerRecord record){
        Object values = record.value();
        System.out.println("3------------------------------------"+values);
    }
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在springboot集成kafka生产者,需要遵循以下步骤: 1. 添加Maven依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.7.2</version> </dependency> ``` 2. 配置Kafka生产者 在application.properties文件中添加Kafka配置: ```properties spring.kafka.producer.bootstrap-servers=<broker地址> spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer ``` 3. 创建Kafka生产者 在代码中创建Kafka生产者: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; @Service public class KafkaProducerService { private final KafkaTemplate<String, String> kafkaTemplate; @Autowired public KafkaProducerService(KafkaTemplate<String, String> kafkaTemplate) { this.kafkaTemplate = kafkaTemplate; } public void sendMessage(String topic, String message) { this.kafkaTemplate.send(topic, message); } } ``` 4. 发送消息 在需要发送消息的地方,注入KafkaProducerService,并调用sendMessage方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class MessageController { private final KafkaProducerService kafkaProducerService; @Autowired public MessageController(KafkaProducerService kafkaProducerService) { this.kafkaProducerService = kafkaProducerService; } @PostMapping("/message") public void sendMessage(@RequestBody String message) { this.kafkaProducerService.sendMessage("test-topic", message); } } ``` 以上就是在springboot集成Kafka生产者的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浮生若梦01

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

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

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

打赏作者

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

抵扣说明:

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

余额充值