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;
	}
}
相关推荐
正在努力Coding4 分钟前
kafka(windows)
分布式·kafka
TTDreamTT18 分钟前
SpringBoot十二、SpringBoot系列web篇之过滤器Filte详解
spring boot
阿里云大数据AI技术2 小时前
ES Serverless 8.17王牌发布:向量检索「火力全开」,智能扩缩「秒级响应」!
大数据·运维·serverless
Mikhail_G2 小时前
Python应用变量与数据类型
大数据·运维·开发语言·python·数据分析
G皮T3 小时前
【Elasticsearch】映射:null_value 详解
大数据·elasticsearch·搜索引擎·映射·mappings·null_value
大霸王龙4 小时前
软件工程的软件生命周期通常分为以下主要阶段
大数据·人工智能·旅游
一只爱撸猫的程序猿4 小时前
构建一个简单的智能文档问答系统实例
数据库·spring boot·aigc
crud4 小时前
Spring Boot 3 整合 Swagger:打造现代化 API 文档系统(附完整代码 + 高级配置 + 最佳实践)
java·spring boot·swagger
点赋科技4 小时前
沙市区举办资本市场赋能培训会 点赋科技分享智能消费新实践
大数据·人工智能
YSGZJJ5 小时前
股指期货技术分析与短线操作方法介绍
大数据·人工智能