本篇文章为你整理了kafka(kafka和rabbitmq的区别)的详细内容,包含有kafka是做什么的 kafka和rabbitmq的区别 kafka面试题 kafka如何保证不重复消费又不丢失数据 kafka,希望能帮助你了解 kafka。
groupId org.springframework.boot /groupId
artifactId spring-boot-starter-web /artifactId
/dependency
!-- kafkfa --
dependency
groupId org.springframework.kafka /groupId
artifactId spring-kafka /artifactId
exclusions
exclusion
groupId org.apache.kafka /groupId
artifactId kafka-clients /artifactId
/exclusion
/exclusions
/dependency
dependency
groupId org.apache.kafka /groupId
artifactId kafka-clients /artifactId
/dependency
dependency
groupId com.alibaba /groupId
artifactId fastjson /artifactId
/dependency
/dependencies
2.配置文件
spring:
kafka:
bootstrap-servers: 192.168.200.130:9092 #主机IP:端口号
producer:
retries: 10
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
spring:
kafka:
bootstrap-servers: 192.168.200.130:9092 #主机IP:端口号
consumer:
group-id: ${spring.application.name}
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
3.生产者发送消息
@RestController
public class HelloController {
@Autowired
private KafkaTemplate String,String kafkaTemplate;
//发送String
@GetMapping("/hello")
public String hello(){
String message = "Hello,SpringBoot Kafka!";
kafkaTemplate.send("topic",message);
return "success";
//发送pojo
@GetMapping("/sendPojo")
public String sendPojo(){
User user = User.builder()
.id(1001)
.name("张三")
.birthday(new Date())
.build();
kafkaTemplate.send("pojo-topic", JSON.toJSONString(user));
return "success";
3.消费者接收消息
@Component
public class HelloListener {
//接收Stirng
@KafkaListener(topics = "topic")
public void onMessage(String message){
if(!StringUtils.isEmpty(message)){
System.out.println(message);
//接收pojo
@KafkaListener(topics = "pojo-topic")
public void onMessage2(String message) {
if (!StringUtils.isEmpty(message)){
User user = JSON.parseObject(message, User.class);
System.out.println("user = " + user);
生产者发送消息,多个消费者订阅同一个主题,只能有一个消费者收到消息(一对一)
生产者发送消息,多个消费者订阅同一个主题,所有消费者都能收到消息(一对多)(设置不同得组)
#kafka安装(docker)
Kafka对于zookeeper是强依赖,保存kafka相关的节点数据,所以安装Kafka之前必须先安装zookeeper
--env KAFKA_ZOOKEEPER_CONNECT=主机IP:2181 \
--env KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://主机IP:9092 \
--env KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
--env KAFKA_HEAP_OPTS="-Xmx256M -Xms256M" \
--restart always=true
--net=host wurstmeister/kafka:2.12-2.3.1
以上就是kafka(kafka和rabbitmq的区别)的详细内容,想要了解更多 kafka的内容,请持续关注盛行IT软件开发工作室。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。