springboot集成kafka发送消息
- 1、kafkaTemplate.send()方法
- 1.1、springboot集成kafka发送消息Message对象消息
- 1.2、springboot集成kafka发送ProducerRecord对象消息
- 1.3、springboot集成kafka发送指定分区消息
- 2、kafkaTemplate.sendDefault()方法
- 3、kafkaTemplate.send(...)和kafkaTemplate.sendDefault(...)的区别
1、kafkaTemplate.send()方法
1.1、springboot集成kafka发送消息Message对象消息
生产者代码
package com.power.producer;
import org.springframework.kafka.annotation.KafkaHandler;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class EventProducer {
@Resource
private KafkaTemplate<String,String> kafkaTemplate;
public void sendMessage(){
//通过构建器模式创建Message对象
Message message = MessageBuilder.withPayload("hello-message")
.setHeader(KafkaHeaders.TOPIC,"test-topic-02")//在header中放置topic的名字
.build();
kafkaTemplate.send(message);
}
}
2、测试类
package com.power;
import com.power.producer.EventProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
public class SpringBoot01KafkaBaseApplication {
@Resource
private EventProducer eventProducer;
@Test
void testMessage(){
eventProducer.sendMessage();
}
@Test
void testMessage(){
eventProducer.sendMessage();
}
}
1.2、springboot集成kafka发送ProducerRecord对象消息
生产者
package com.power.producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.header.internals.RecordHeaders;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Component
public class EventProducer {
@Resource
private KafkaTemplate<String,String> kafkaTemplate;
public void sendMessage(){
//通过构建器模式创建Message对象
Message message = MessageBuilder.withPayload("hello-message")
.setHeader(KafkaHeaders.TOPIC,"test-topic-02")//在header中放置topic的名字
.build();
kafkaTemplate.send(message);
}
public void sendProducerRecord(){
//Headers里面放一些信息(信息是key-value键值对),到时候消费组接收到消息后,可以拿到这个Headers里面放的信息
Headers headers = new RecordHeaders();
headers.add("phone","17676767676".getBytes(StandardCharsets.UTF_8));
headers.add("orderId","OD1456467576467".getBytes(StandardCharsets.UTF_8));
//String topic, Integer partition, Long timestamp, K key, V value, Iterable<Header> headers
ProducerRecord<String,String> record =
new ProducerRecord("test-topic-02", 0, System.currentTimeMillis(), "key1", "value", headers);
kafkaTemplate.send(record);
}
}
测试类:
package com.power;
import com.power.producer.EventProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
public class SpringBoot01KafkaBaseApplication {
@Resource
private EventProducer eventProducer;
@Test
void testProducerRecord(){
eventProducer.sendProducerRecord();
}
}
1.3、springboot集成kafka发送指定分区消息
生产者:(方法sendEvent4)
package com.power.producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.header.internals.RecordHeaders;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Component
public class EventProducer {
@Resource
private KafkaTemplate<String,String> kafkaTemplate;
public void sendEvent4(){
//String topic, Integer partition, Long timestamp, K key, V data
kafkaTemplate.send("test-topic-02",0,System.currentTimeMillis(),"k2","hello-kafka");
}
}
测试类:
package com.power;
import com.power.producer.EventProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
public class SpringBoot01KafkaBaseApplication {
@Resource
private EventProducer eventProducer;
@Test
void sendEvent4(){
eventProducer.sendEvent4();
}
}
2、kafkaTemplate.sendDefault()方法
生产者:
package com.power.producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.header.Headers;
import org.apache.kafka.common.header.internals.RecordHeaders;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Component
public class EventProducer {
@Resource
private KafkaTemplate<String,String> kafkaTemplate;
public void sendDefault(){
//Integer partition, Long timestamp, K key, V data
kafkaTemplate.sendDefault(0,System.currentTimeMillis(),"k3","hello-kafka");
}
}
测试类:
package com.power;
import com.power.producer.EventProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
public class SpringBoot01KafkaBaseApplication {
@Resource
private EventProducer eventProducer;
@Test
void sendDefault(){
eventProducer.sendDefault();
}
}
配置文件:设置默认topic
不设置topic运行生产者会报找不到topic的错。