springboot整合kafka

208 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。​​​​​​​​​​​​​​​ ​

 1.启动kafka

创建topic:

[root@localhost kafka_2.12-2.1.0]# bin/kafka-topics.sh --create --zookeeper 192.168.184.128:2181 --replication-factor 1 --partitions 1 --topic springboot-topic

2.创建springboot项目:

3.pom文件:

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		
		<!-- springboot-kafka -->
		<dependency>
			<groupId>org.springframework.kafka</groupId>
			<artifactId>spring-kafka</artifactId>
		</dependency>
</dependencies>

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

4.application.properties:

spring.kafka.bootstrap-servers=192.168.184.128:9092
spring.kafka.consumer.group-id=0
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.batch-size=16384
spring.kafka.producer.buffer-memory=33554432
spring.kafka.producer.acks=all
spring.kafka.producer.retries=0

5.生产者:

package com.wyh;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
//发送消息
@Component
public class KafkaSender {

	@Autowired
	private KafkaTemplate<String, String> kafkaTemplate;
	
	public void sendChannelMess(String topic, String message) {
		kafkaTemplate.send(topic, message);
	}

}

6.消费者:

package com.wyh;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
//消费者
@Component
public class KafkaConsumer {

	//消费者不需要调用,会自己监听topic,然后监听的消息就是参数message
	@KafkaListener(topics = {"springboot-topic"})
	public void receiveMessage(String message) {
		System.out.println(message);
	}
}

7.测试类:

package com.wyh;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

//测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootKafkaApplication.class)
public class KafkaTest {

	@Autowired
	private KafkaSender sender;
	
	@Test
	public void myTest() {
		sender.sendChannelMess("springboot-topic", "test-springboot-kafka-message");
	}
}

8.启动消费者:

run on server:

9.启动测试类,发送消息

10.切换至consumer类,查看控制台,已打印出刚才发送的消息:

测试成功。

代码地址:github.com/wyhuiii/spr…