SpringBoot整合Kafka

懵懂的女人 提交于 2021-01-09 18:01:57

Kafka设置

启动zk

bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

启动kafka

bin/kafka-server-start.sh  config/server.properties

依赖

compile 'org.springframework.kafka:spring-kafka'

application.properties

#============== kafka ===================
# 指定kafka 代理地址,可以多个
# suppress inspection "UnusedProperty"
spring.kafka.bootstrap-servers=YourServerHostName/IP:9092

#=============== provider  =======================

spring.kafka.producer.retries=0
# 每次批量发送消息的数量
spring.kafka.producer.batch-size=16384
spring.kafka.producer.buffer-memory=33554432

# 指定消息key和消息体的编解码方式
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

#=============== consumer  =======================
# 指定默认消费者group id
spring.kafka.consumer.group-id=test-consumer-group

spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.enable-auto-commit=true
spring.kafka.consumer.auto-commit-interval=100

# 指定消息key和消息体的编解码方式
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer

Kafka producer 

@Component
@Slf4j
public class MessageSender {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    private Gson gson = new GsonBuilder().create();

    public void send() {
        Message m = new Message();
        m.setId(System.currentTimeMillis());
        m.setName(UUID.randomUUID().toString());
        m.setDate(new Date());
        log.info("+++++++++++++++++++++  message = {}", gson.toJson(m));
        kafkaTemplate.send("myTopic",gson.toJson(m));
    }
}

Kafka consumer

@Component
@Slf4j
public class MessageReceiver {

    @KafkaListener(topics = {"myTopic"})
    public void listen(ConsumerRecord<?,?> record) {
        Optional<?> message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object m = message.get();
            log.info("------------------record"+ record);
            log.info("------------------message"+ m);
        }

    }
}

main

@SpringBootApplication
public class KafkaApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(KafkaApplication.class, args);

        MessageSender sender = context.getBean(MessageSender.class);

        for (int i = 0; i < 3; i++) {
            sender.send();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

Show topic

bin/kafka-topics.sh --list --zookeeper localhost:2181

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!