SpringBoot整合Kafka实现发布订阅

SpringBoot整合Kafka实现发布订阅

新建SpringBoot项目
基于JDK版本1.8,SpringBoota版本1.5.9.RELEASE

1、pom.xml中添加依赖



    4.0.0

    com.sunlong
    spring-boot-kafka-demo
    1.0.0
    jar

    spring-boot-kafka-demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.kafka
            spring-kafka
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    

2、配置application.yml文件

spring:
    kafka:     # 指定kafka 代理地址,可以多个
      bootstrap-servers: http://kafkahost:9092
      consumer:   # 指定默认消费者group id
        group-id: myGroup
      template:    # 指定默认topic id
        default-topic: tsc_dsc_newMsg
      listener:   # 指定listener 容器中的线程数,用于提高并发量
        concurrency: 5
      producer:  # 每次批量发送消息的数量
        batch-size: 1000

server:
    port: 8888

3、模拟生产者Producer

package com.sunlong.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 * kafkaDemo
 *
 * @Author 孙龙
 * @Date 2018/1/19
 */
@RestController
public class SampleController {
    @Autowired
    private KafkaTemplate template;

    @GetMapping("/send")
    String send(String topic, String key, String data) {
        template.send(topic, key, data);
        return "success";
    }
}

4、消息监听Consumer

可以建议多个消费者

package com.sunlong.listenner;

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

/*
 * kafkaDemo
 *
 * @Author 孙龙
 * @Date 2018/1/19
 */
@Component
public class Listenner {

    @KafkaListener(topics = "topic1")
    public void listenT1(ConsumerRecord cr) throws Exception {
        System.out.println("listenT1收到消息!!   topic:>>>  " + cr.topic() + "    key:>>  " + cr.key() + "    value:>>  " + cr.value());
    }

    @KafkaListener(topics = "topic2")
    public void listenT2(ConsumerRecord cr) throws Exception {
        System.out.println("listenT2收到消息!!   topic:>>>  " + cr.topic() + "    key:>>  " + cr.key() + "    value:>>  " + cr.value());
    }

}

5、测试

5.1 启动项目

5.2 打开浏览器输入

http://localhost:8888/send?topic=topic1&key=msg&data=testmessage

5.3 可以看到控制台打印

listenT1收到消息!! topic:>>> topic1key:>> msgvalue:>> testmessage

Github代码示例Url

你可能感兴趣的:(SpringBoot整合Kafka实现发布订阅)