kafka整合springboot
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的更多相关文章
- SpringBoot Kafka 整合集成 示例教程
1.使用IDEA新建工程,创建工程 springboot-kafka-producer 工程pom.xml文件添加如下依赖: <!-- 添加 kafka 依赖 --> <depend ...
- 03篇ELK日志系统——升级版集群之ELK日志系统整合springboot项目
[ 前言:整个ELK日志系统已经搭建好了,接下来的流程就是: springboot项目中的logback日志配置通过tcp传输,把springboot项目中所有日志数据传到————>logsta ...
- Windows平台整合SpringBoot+KAFKA_第1部分_环境配置部分
项目需要,需要整合 SpringBoot+KAFKA 我调查了一下,发现Linux中,要先装zoomkeeper,再装KAFKA,如 https://blog.csdn.net/zhangcongy ...
- RabbitMQ从概念到使用、从Docker安装到RabbitMQ整合Springboot【1.5w字保姆级教学】
@ 目录 一.前言 二.RabbitMQ作用 1. 异步处理 2. 应用解耦 3. 流量控制 三.RabbitMQ概念 1. RabbitMQ简介 2. 核心概念 四.JMS与AMQP比较 五.Rab ...
- flume与kafka整合
flume与kafka整合 前提: flume安装和测试通过,可参考:http://www.cnblogs.com/rwxwsblog/p/5800300.html kafka安装和测试通过,可参考: ...
- 5 kafka整合storm
本博文的主要内容有 .kafka整合storm .storm-kafka工程 .storm + kafka的具体应用场景有哪些? 要想kafka整合storm,则必须要把这个storm-kafk ...
- 【转】Spark Streaming和Kafka整合开发指南
基于Receivers的方法 这个方法使用了Receivers来接收数据.Receivers的实现使用到Kafka高层次的消费者API.对于所有的Receivers,接收到的数据将会保存在Spark ...
- 整合springboot(app后台框架搭建四)
springboot可以说是为了适用SOA服务出现,一方面,极大的简便了配置,加速了开发速度:第二方面,也是一个嵌入式的web服务,通过jar包运行就是一个web服务: 还有提供了很多metric,i ...
- SparkStreaming+Kafka整合
SparkStreaming+Kafka整合 1.需求 使用SparkStreaming,并且结合Kafka,获取实时道路交通拥堵情况信息. 2.目的 对监控点平均车速进行监控,可以实时获取交通拥堵情 ...
随机推荐
- STM32L1xx——ADC(中断/DMA)样例代码
此代码欲实现的功能是:使用中断或者DMA的方式采集滑动变阻器采集到的电压值,使用单ADC单通道采样! (由于不是直接需要电压,所以转换函数我就没列出来,可根据自身需要去网上查到转换的函数.) 代码结构 ...
- 07_Hive的基本命令_Insert命令
1.将查询结果插入Hive表语法结构: 1.1.基本模式插入: INSERT OVERWRITE TABLE tablename1 [PARTITION (partcol1=val1, partcol ...
- golang embedded structs
golang 中把struct 转成json格式输出 package main import ( "encoding/json" "fmt" ) type Pe ...
- ubuntu16.04中不能连接无线网络
安装完ubuntu desktop版之后,无线网络连接中没有出现当前可以连接的wifi列表. 直接插上网线之后,是可以上网的.但是还是不是很方便, 可以点击右上角的齿轮-->system set ...
- string::crbegin string::crend
const_reverse_iterator crbegin() const noexcept;功能:crbegin是最后一个字符,crend第一个字符的前一个.迭代器向左移动是“+”,向右移动是“- ...
- 【Linux】解决用vi修改文件,保存文件时,提示“readonly option is set”
当在终端执行sudo命令时,系统提示“hadoop is not in the sudoers file”: 其实就是没有权限进行sudo,解决方法如下(这里假设用户名是cuser): 1.切换到超级 ...
- 莫比乌斯函数介绍&&基础
定义 设正整数$N$按照算术基本定理分解质因数为$N=p_1^{c_1}p_2^{c_2} \cdots P_m^{c_m}$,定义函数: $$\mu(N)= \left\{\begin{matrix ...
- WebUI自动化之Java语言讲解
Java学习网站: default是兜底逻辑,以上条件都不符合时,如何处理. break是终止循环,continue是终止本次循环:
- Python一等函数
一等对象 一等对象的定义: (1)在运行时创建 (2)能赋值给变量或数据结构中的元素 (3)能作为参数传给函数 (4)能作为函数的返回结果 ▲ Python中,整数.字符串和字典.函数都是一等对象. ...
- Laravel 多态关联中利用关联表相关字段进行排序的问题
1 目标 1.1 在 Laravel 项目的开发中,多态的需求很常见,按多态关联进行排序的需求也是必须的. 1.2 请想像,我们有一个需求,荣誉栏目多态关联一个档案模型,要求在荣誉中按档案的推荐时间进 ...