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;
	}
}
相关推荐
m0_7482552637 分钟前
Spring Boot 3.x 引入springdoc-openapi (内置Swagger UI、webmvc-api)
spring boot·后端·ui
DavidSoCool1 小时前
Elasticsearch Java API Client [8.17] 使用
java·大数据·elasticsearch
用户199701080181 小时前
淘宝买家/卖家订单列表、订单详情、订单物流 API 接口全攻略
大数据
Arbori_262151 小时前
大数据 spark hive 总结
大数据·hive·spark
阿里云大数据AI技术1 小时前
中免日上使用阿里云向量检索服务 Milvus 版搭建在线推荐系统
大数据
m0_748238272 小时前
SpringBoot 如何调用 WebService 接口
java·spring boot·后端
小王不会写code2 小时前
spring boot+vue项目(免费)
java·spring boot·后端
m0_748251723 小时前
Python大数据可视化:基于python大数据的电脑硬件推荐系统_flask+Hadoop+spider
大数据·python·flask
猿来入此小猿3 小时前
基于SpringBoot实现旅游酒店平台功能六
java·spring boot·毕业设计·旅游·毕业源码·免费学习·旅游景点
m0_748229993 小时前
Spring Boot(十六):使用 Jenkins 部署 Spring Boot
spring boot·后端·jenkins