springboot整合kafka

Springboot整合Kafka

1.下载kafka

启动

java 复制代码
	1.zookeeper-server-start.bat ..\..\config\zookeeper.properties
	2.kafka-server-start.bat ..\..\config\server.properties
 	3.kafka-server-stop.bat
 	4.zookeeper-server-stop.bat
 

2.引入依赖

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

3.消息实体

java 复制代码
package com.example.kafka;

import java.util.Date;

public class KafkaMessage
{
	private long id;
	private String username;
	private String password;
	private Date date;

	public long getId()
	{
		return id;
	}

	public void setId(long id)
	{
		this.id = id;
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public Date getDate()
	{
		return date;
	}

	public void setDate(Date date)
	{
		this.date = date;
	}
}

4.生产者

java 复制代码
package com.example.kafka;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;


@Component
public class KafkaProducer
{
	@Autowired
	private KafkaTemplate<String,String> kafkaTemplate;

	public KafkaTemplate<String, String> getKafkaTemplate()
	{
		return kafkaTemplate;
	}

	public void setKafkaTemplate(KafkaTemplate<String, String> kafkaTemplate)
	{
		this.kafkaTemplate = kafkaTemplate;
	}

	public void sendKafkaMessage(KafkaMessage message)
	{
		kafkaTemplate.send("myTopic", JSONObject.toJSONString(message));
	}

}

5.消费者

java 复制代码
package com.example.kafka;

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

@Component
public class KafkaConsumer
{

	@KafkaListener(topics = "myTopic", groupId = "myGroup")
	public void obtainMessage(ConsumerRecord<String, String> consumerRecord)
	{
		System.out.println("obtainMessage invoked");
		String topic = consumerRecord.topic();
		String key = consumerRecord.key();
		String value = consumerRecord.value();
		long timestamp = consumerRecord.timestamp();
		int partition = consumerRecord.partition();
		System.out.println("topic:" + topic);
		System.out.println("key:" + key);
		System.out.println("value:" + value);
		System.out.println("timestamp:" + timestamp);
		System.out.println("partition:" + partition);
		System.out.println("=======================");
	}
}

5.controller测试

java 复制代码
package com.example.controller;

import com.example.kafka.KafkaMessage;
import com.example.kafka.KafkaProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping(value = "/kafka")
public class KafkaController
{
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	private KafkaProducer kafkaProducer;

	@RequestMapping(value = "message", method = RequestMethod.GET)
	public KafkaMessage sendKafkaMessage(@RequestParam(name = "id") long id, @RequestParam(name = "username") String username,
			@RequestParam(name = "password") String password)
	{
		KafkaMessage kafkaMessage = new KafkaMessage();
		kafkaMessage.setId(id);
		kafkaMessage.setUsername(username);
		kafkaMessage.setPassword(password);
		kafkaMessage.setDate(new Date());
		kafkaProducer.sendKafkaMessage(kafkaMessage);
		return kafkaMessage;
	}
}
相关推荐
御前一品带刀侍卫20 分钟前
elasticsearch基础
大数据·elasticsearch·搜索引擎
武子康21 分钟前
大数据-193 Apache Tez - DAG 作业计算框架 核心解释 工作原理 配置集成
大数据·hive·hadoop·hdfs·apache·hbase·mapreduce
武子康43 分钟前
大数据-191 Elasticsearch - ES 集群模式 配置启动 规划调优
java·大数据·elk·elasticsearch·搜索引擎·全文检索
落落落sss1 小时前
es实现自动补全
大数据·服务器·elasticsearch·搜索引擎·全文检索
小汤猿人类1 小时前
什么是Elasticsearch?
大数据·elasticsearch·搜索引擎
武子康1 小时前
大数据-187 Elasticsearch - ELK 家族 Logstash Filter 插件 使用详解
大数据·数据结构·elk·elasticsearch·搜索引擎·全文检索·1024程序员节
Hadoop_Liang1 小时前
Docker Compose一键部署Spring Boot + Vue项目
vue.js·spring boot·docker
阿乾之铭2 小时前
Spring Boot框架中的IO
java·spring boot·log4j·1024程序员节
wclass-zhengge2 小时前
SpringBoot篇(运维实用篇 - 临时属性)
运维·spring boot·后端
2401_857600952 小时前
商场应急管理:SpringBoot技术解决方案
java·spring boot·后端