springboot整合kafka

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类,查看控制台,已打印出刚才发送的消息:

测试成功。

代码地址:https://github.com/wyhuiii/springboot-kafka

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QYHuiiQ

听说打赏的人工资翻倍~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值