springboot整合kafka实现发布订阅,kafka学习五

1.引入依赖

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.kafka</groupId>
         <artifactId>spring-kafka</artifactId>
     </dependency>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
     </dependency>

2.yml配置

spring:
  kafka:              # 指定kafka 代理地址,可以多个
    bootstrap-servers: 192.168.211.137:9092,192.168.211.139:9092,192.168.211.140:9092
    template:    # 指定默认topic id
      default-topic: producer
    listener:   # 指定listener 容器中的线程数,用于提高并发量
      concurrency: 5
    consumer:
      group-id: myGroup # 指定默认消费者group id
      client-id: 200
      max-poll-records: 200
      auto-offset-reset: earliest # 最早未被消费的offset
    producer:
      batch-size: 1000 # 每次批量发送消息的数量
      retries: 3
      client-id: 200

3.代码示例

  1. 生产者
package com.wyu.tt06kafkademo.demo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class KafkaProducer {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    public void send(String topic, Object body) {
        Message<String> message = new Message<>();
        message.setId(System.currentTimeMillis());
        message.setMessage(body.toString());
        message.setTime(new Date());
        String content = null;
        try {
            content = objectMapper.writeValueAsString(message);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        kafkaTemplate.send(topic, content);
        log.info("send {} to {} success!", message, topic);
        System.out.println("send "+ message +" to "+ topic +" success!");
    }
}

  1. 消费者
package com.wyu.tt06kafkademo.demo;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Slf4j
@Component
public class KafkaConsumer {

    /**
     * 有消息就读取,读取消息topic,offset,key,value等信息
     */
    @KafkaListener(topics = {"kafka1"})
    public void listen(ConsumerRecord<?, ?> record) {
        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            log.info("详细消息读取-------------------->");
            log.info("message:{} + record:{}", message, record);
        }
    }

    /**
     * 有消息就读取,批量读取消息
     */
    @KafkaListener(topics = "kafka1")
    public void onMessage(List<String> crs) {
        for(String str : crs){
            log.info("批量读取-------------------->");
            log.info("kafka1:" + str);
        }
    }

    /**
     * 有消息就读取message
     */
    @KafkaListener(topics = {"kafka1"})
    public void receiveMessage(String message){
        log.info("读取message-------------------->");
        log.info("kafka1:" + message);
    }
}

  1. controller
package com.wyu.tt06kafkademo.controller;

import com.wyu.tt06kafkademo.demo.KafkaProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class KafkaController {
    @Autowired
    private KafkaProducer kafkaProducer;

    @GetMapping("/kafka/{topic}")
    public String send(@PathVariable("topic") String topic, @RequestParam String message) {
        kafkaProducer.send(topic, message);
        return "success";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

高并发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值