Kafka+SpringBoot 整合使用

  • 相关版本:(非常重要,版本不对后面可能出现各种坑)
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.3.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>1.2.1.RELEASE</version>
         </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>

如果觉得SpringBoot版本不合胃口,可以自行更换版本,但是Spring-Kafka的版本一定要在你SpringBoot的支持范围内,不然是会跑出Exception的,一般情况下这种找不到Broker建议可以配一下你的host文件追加 127.0.0.1 localhost

  • 添加配置文件
# kafka 核心配置
spring.kafka.bootstrap-servers=10.69.216.50:9092,10.69.216.51:9092,10.69.216.52:9092,10.69.216.46:9092
spring.kafka.consumer.group-id=top-api
spring.kafka.consumer.auto-offset-reset=earliest

  • 发送消息
package com.cmrh.touchlog.kafka;

import java.util.Date;
import java.util.UUID;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
/**
 * @author GuoGuiRong
  * @Description: 生产者,只要用户将数据转发到kafka中去
 * @date 2018/9/11 10:57
 */
@Component
public class Producer {

    @Autowired
    private KafkaTemplate kafkaTemplate;

    @Autowired
    Gson gson;

    /**
     * 发送测试消息
     */
    public void sendMessage(){
        Message m = new Message();
        m.setId(System.currentTimeMillis());
        m.setMsg(UUID.randomUUID().toString());
        m.setSendTime(new Date());
        kafkaTemplate.send("touch", gson.toJson(m));
    }

    /**
     * 发送指定的消息
     * @param msgContent 
     */
    public void sendMessage(String msgContent){
        Message m = new Message();
        m.setId(System.currentTimeMillis());
        m.setMsg(msgContent);
        m.setSendTime(new Date());
        kafkaTemplate.send("top-api", gson.toJson(m));
    }
}
  • 接收消息
package com.cmrh.touchlog.kafka;

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

import com.google.gson.Gson;

import java.util.Optional;

/**
 * @author GuoGuiRong
  * @Description:
 * @date 2018/9/11 10:59
 */
@Component
@Slf4j
public class Receiver {

    @Autowired
    Gson gson;

    @KafkaListener(topics = "top-api")
    public void handler(ConsumerRecord<?, ?> record) {

        Optional<?> kafkaMessage = Optional.ofNullable(record.value());
        if (kafkaMessage.isPresent()) {
            Object message = kafkaMessage.get();
            Message message1 = gson.fromJson(message.toString(), Message.class);
            log.info("收到消息message={}",message1);
        }

    }
}

运行结果:

2018-09-11 15:30:27,585  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 org.springframework.kafka.listener.KafkaMessageListenerContainer  [//]  partitions assigned:[touch-0]
2018-09-11 15:30:27,684  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724028, msg=47e05a03-2d23-4c51-9c7b-1fdde25d4d4e, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,684  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724079, msg=c2076c3e-a0cb-42ca-a743-cf3cef6cf5de, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,685  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724080, msg=6bbdb3e2-d60d-4875-9216-a1b7b9cbd434, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,685  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724080, msg=e6dea21b-cae0-40da-a6c3-47fc2c801fbb, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,685  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724080, msg=82972e6d-692f-4721-963e-69802ef55719, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,686  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724080, msg=772af06e-b1ce-47b0-930d-d51c752a181a, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,686  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724080, msg=d2922487-ef5b-4a06-bbb0-7e1a9be76e93, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,687  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724081, msg=e5de1fa2-0c5e-41cc-806e-27868ceb34d8, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,688  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724081, msg=72087eb6-62b4-40ef-9a83-f7d06e8da556, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,688  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724081, msg=26c8a1a1-d153-4eda-a474-b12a5d63aeae, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,688  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724081, msg=93bafc22-632c-4cf0-b501-bd8aa39e5db2, sendTime=Tue Sep 11 15:25:24 CST 2018)
2018-09-11 15:30:27,689  INFO  org.springframework.kafka.KafkaListenerEndpointContainer#0-0-C-1 com.cmrh.touchlog.kafka.Receiver  [//]  收到消息message=Message(id=1536650724081, msg=9efec4bb-ec32-4507-a518-358728e2e6fe, sendTime=Tue Sep 11 15:25:24 CST 2018)

再次说明,版本不对,努力白费。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,809评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,189评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,290评论 0 359
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,399评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,425评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,116评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,710评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,629评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,155评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,261评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,399评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,068评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,758评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,252评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,381评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,747评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,402评论 2 358