Springboot 与kafka简单整合demo

  • maven依赖
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
  • properties配置
server.port=8881

spring.kafka.bootstrap-servers=localhost:9092

指定默认消费者group id --> 由于在kafka中,同一组中的consumer不会读取到同一个消息,依靠groud.id设置组名
spring.kafka.consumer.group-id=testGroup3

  • produce
import com.jifenn.test.kafka.KafkaMsgSend;
import com.jifenn.test.kafka.enums.KafkaMsgTopicEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MsgSendController {

    @Autowired
    private KafkaMsgSend msgSend;

    @GetMapping(path = "/msg/send")
    public boolean sendMsg(@RequestParam String message) {
        msgSend.sendMessage(KafkaMsgTopicEnum.TEST_TOPIC.getValue(), message);
        return true;
    }

}
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SuccessCallback;


@Slf4j
@Component
public class KafkaMsgSend {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    public <K, T> void sendMessage(final String topic, final String message) {
        log.info("send kafka message,topic:{},message:{}", topic, message);
        ListenableFuture<SendResult<K, T>> listenableFuture = null;
        listenableFuture = kafkaTemplate.send(topic, message);
        SuccessCallback<SendResult<K, T>> successCallback = new SuccessCallback<SendResult<K, T>>() {
            @Override
            public void onSuccess(SendResult<K, T> result) {
                log.info("发送成功,topic:{},message:{}", topic, message);
            }
        };

        FailureCallback failureCallback = new FailureCallback() {
            @Override
            public void onFailure(Throwable e) {
                log.error("发送失败,topic:{},message:{}", topic, message);
                //todo 记录失败日志
                throw new RuntimeException(e);
            }
        };
        listenableFuture.addCallback(successCallback, failureCallback);
    }

}
  • consumer
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@Component
public class KafkaMsgConsumer {

    @KafkaListener(topics = {"kafkaTopicDev"})
    public void receive(List<String> message) {
        log.info("接收到消息:{}", message);
    }
}
  • 测试

 最基础的配置就OK了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值