1、pom.xml添加依赖

 <dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.2.1</version>
</dependency>

2、创建配置文件

package com.youfan.config;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory; import java.util.HashMap;
import java.util.Map; @Configuration
@EnableKafka
public class KafkaProducerConfig { @Value("${kafka.producer.servers}")
private String servers;
@Value("${kafka.producer.retries}")
private int retries;
@Value("${kafka.producer.batch.size}")
private int batchSize;
@Value("${kafka.producer.linger}")
private int linger;
@Value("${kafka.producer.buffer.memory}")
private int bufferMemory; public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
props.put(ProducerConfig.RETRIES_CONFIG, retries);
props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
props.put(ProducerConfig.LINGER_MS_CONFIG, linger);
props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return props;
} public ProducerFactory<String, String> producerFactory() {
return new DefaultKafkaProducerFactory<String, String>(producerConfigs());
} @Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<String, String>(producerFactory());
}
}

3、properties文件添加配置信息

                kafka.consumer.zookeeper.connect=192.168.227.129:2181
kafka.consumer.servers=192.168.227.129:9092
kafka.consumer.enable.auto.commit=true
kafka.consumer.session.timeout=6000
kafka.consumer.auto.commit.interval=100
kafka.consumer.auto.offset.reset=latest
kafka.consumer.topic=test
kafka.consumer.group.id=test
kafka.consumer.concurrency=10 kafka.producer.servers=192.168.227.129:9092
kafka.producer.retries=0
kafka.producer.batch.size=4096
kafka.producer.linger=1
kafka.producer.buffer.memory=40960

4、编写读取topic的配置文件

public class ReadProperties {
public final static Config config = ConfigFactory.load("kafka.properties");
public static String getKey(String key){
return config.getString(key).trim();
}
public static String getKey(String key,String filename){
Config config = ConfigFactory.load(filename);
return config.getString(key).trim();
}
}
//配置文件内容
//attentionProductLog=attentionProductLog
//buyCartProductLog=buyCartProductLog
//collectProductLog=collectProductLog
//scanProductLog=scanProductLog
 

5、使用kafka

package com.youfan.Control;

import com.alibaba.fastjson.JSONObject;
import com.youfan.entity.ResultMessage;
import com.youfan.log.AttentionProductLog;
import com.youfan.log.BuyCartProductLog;
import com.youfan.log.CollectProductLog;
import com.youfan.log.ScanProductLog;
import com.youfan.utils.ReadProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest;
import java.util.Date; /**
*
*/
@RestController
@RequestMapping("infolog")
public class InfoInControl {
private final String attentionProductLogTopic = ReadProperties.getKey("attentionProductLog");
private final String buyCartProductLogTopic = ReadProperties.getKey("buyCartProductLog");
private final String collectProductLogTopic = ReadProperties.getKey("collectProductLog");
private final String scanProductLogTopic = ReadProperties.getKey("scanProductLog"); @Autowired
private KafkaTemplate<String, String> kafkaTemplate; @RequestMapping(value = "helloworld",method = RequestMethod.GET)
public String hellowolrd(HttpServletRequest req){
String ip =req.getRemoteAddr();
ResultMessage resultMessage = new ResultMessage();
resultMessage.setMessage("hello:"+ip);
resultMessage.setStatus("success");
String result = JSONObject.toJSONString(resultMessage);
return result;
} /**
* AttentionProductLog:{productid:productid....}
BuyCartProductLog:{productid:productid....}
CollectProductLog:{productid:productid....}
ScanProductLog:{productid:productid....}
* @param recevicelog
* @param req
* @return
*/
@RequestMapping(value = "receivelog",method = RequestMethod.POST)
public String hellowolrd(String recevicelog,HttpServletRequest req){
if(StringUtils.isBlank(recevicelog)){
return null;
}
String[] rearrays = recevicelog.split(":",2);
String classname = rearrays[0];
String data = rearrays[1];
String resulmesage= ""; if("AttentionProductLog".equals(classname)){
AttentionProductLog attentionProductLog = JSONObject.parseObject(data,AttentionProductLog.class);
resulmesage = JSONObject.toJSONString(attentionProductLog);
kafkaTemplate.send(attentionProductLogTopic,resulmesage+"##1##"+new Date().getTime());
}else if("BuyCartProductLog".equals(classname)){
BuyCartProductLog buyCartProductLog = JSONObject.parseObject(data,BuyCartProductLog.class);
resulmesage = JSONObject.toJSONString(buyCartProductLog);
kafkaTemplate.send(buyCartProductLogTopic,resulmesage+"##1##"+new Date().getTime());
}else if("CollectProductLog".equals(classname)){
CollectProductLog collectProductLog = JSONObject.parseObject(data,CollectProductLog.class);
resulmesage = JSONObject.toJSONString(collectProductLog);
kafkaTemplate.send(collectProductLogTopic,resulmesage+"##1##"+new Date().getTime());
}else if("ScanProductLog".equals(classname)){
ScanProductLog scanProductLog = JSONObject.parseObject(data,ScanProductLog.class);
resulmesage = JSONObject.toJSONString(scanProductLog);
kafkaTemplate.send(scanProductLogTopic,resulmesage+"##1##"+new Date().getTime());
}
ResultMessage resultMessage = new ResultMessage();
resultMessage.setMessage(resulmesage);
resultMessage.setStatus("success");
String result = JSONObject.toJSONString(resultMessage);
return result;
} }

kafka整合springboot的更多相关文章

  1. SpringBoot Kafka 整合集成 示例教程

    1.使用IDEA新建工程,创建工程 springboot-kafka-producer 工程pom.xml文件添加如下依赖: <!-- 添加 kafka 依赖 --> <depend ...

  2. 03篇ELK日志系统——升级版集群之ELK日志系统整合springboot项目

    [ 前言:整个ELK日志系统已经搭建好了,接下来的流程就是: springboot项目中的logback日志配置通过tcp传输,把springboot项目中所有日志数据传到————>logsta ...

  3. Windows平台整合SpringBoot+KAFKA_第1部分_环境配置部分

    项目需要,需要整合 SpringBoot+KAFKA 我调查了一下,发现Linux中,要先装zoomkeeper,再装KAFKA,如  https://blog.csdn.net/zhangcongy ...

  4. RabbitMQ从概念到使用、从Docker安装到RabbitMQ整合Springboot【1.5w字保姆级教学】

    @ 目录 一.前言 二.RabbitMQ作用 1. 异步处理 2. 应用解耦 3. 流量控制 三.RabbitMQ概念 1. RabbitMQ简介 2. 核心概念 四.JMS与AMQP比较 五.Rab ...

  5. flume与kafka整合

    flume与kafka整合 前提: flume安装和测试通过,可参考:http://www.cnblogs.com/rwxwsblog/p/5800300.html kafka安装和测试通过,可参考: ...

  6. 5 kafka整合storm

    本博文的主要内容有 .kafka整合storm   .storm-kafka工程  .storm + kafka的具体应用场景有哪些? 要想kafka整合storm,则必须要把这个storm-kafk ...

  7. 【转】Spark Streaming和Kafka整合开发指南

    基于Receivers的方法 这个方法使用了Receivers来接收数据.Receivers的实现使用到Kafka高层次的消费者API.对于所有的Receivers,接收到的数据将会保存在Spark ...

  8. 整合springboot(app后台框架搭建四)

    springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...

  9. SparkStreaming+Kafka整合

    SparkStreaming+Kafka整合 1.需求 使用SparkStreaming,并且结合Kafka,获取实时道路交通拥堵情况信息. 2.目的 对监控点平均车速进行监控,可以实时获取交通拥堵情 ...

随机推荐

  1. 轻量化模型之SqueezeNet

    自 2012 年 AlexNet 以来,卷积神经网络在图像分类.目标检测.语义分割等领域获得广泛应用.随着性能要求越来越高,AlexNet 已经无法满足大家的需求,于是乎各路大牛纷纷提出性能更优越的 ...

  2. linux基础5-vi文本处理器

    三种模式下各自可以完成的操作: 一般模式:可以完成光标移动.删除单个和整行字.复制和黏贴,通过i.o.a.r这几个命令进入编辑模式 编辑模式:可以输入字符,通过esc返回一般模式 指令模式:读取文件, ...

  3. 算法---FaceNet+mtcnn的使用记录

    FaceNet+mtcnn---ubutntu系统下的使用记录 @WP20190307 由于先配置了FaceNet算法,中途遇到了点问题,单独又配置了mtcnn进行学习,没有深入,蜻蜓点水.今天,在尝 ...

  4. MYSQL安全模式SQL语法需要注意的地方

    MYSQL安全模式 Mysql版本: 背景: 为了避免在执行delete.update将全表数据清空或者覆盖修改,在新项目营销云中开启了mysql的安全模式. 安全模式要求不能对非主键的条件查询做up ...

  5. python+Appium自动化:MultiAction多点触控

    MultiAction MultiAction 是多点触控的类,常用于模拟用户多点操作. 主要包含这add()还有perform()两个方法,模拟多点触控,需要导入TouchAction还有Multi ...

  6. visual studio 和visual studio code 的区别是什么?

    区别有三: 区别一:含义不一样. Visual Studio(简称VS)是美国微软公司的开发工具包系列产品,是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码 ...

  7. 《SVG精髓》笔记(一)

    本文是基于<SVG精髓>一书的简单总结,文中的demo均为该书提供,目的是方便大家使用时快速查阅. 1. 坐标系统 视口(viewport):文档使用的画布区域,表示SVG可见区域的大小, ...

  8. 理解SqlMapConfig.xml文件

    SqlMapConfig.xml mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases( ...

  9. 再度吐槽,PHP在centos7的安装方式稍不注意可能就打击你的积极性

    由于装新机器,没仔细看随便找了篇博文就匆匆安装了php73结果,连配置文件,扩展模块都找不着在哪这里介绍一个linux的查找命令 find / -name php73* 这一命令使用了*这一正则匹配的 ...

  10. python--ctypes模块:调用C函数

    Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库的形式,即 Windows 下的 .dll 文件,或者 Linux 下的 .so 文件 Windows 系统下的 C 标 ...