繁体   English   中英

使用springboot将数据从文件发送到kafka主题

[英]Sending data from file to kafka topic using springboot

我想使用 kafka 和 springboot 从文件实时发送数据,我已经测试了这段代码使用 postman 以 JSON 格式将数据发送到 kafka 主题。

    @RestController
public class OrderController {

    @Autowired
    private KafkaProducerService kafkaProducerService;

    @PostMapping("/post")
    public void order(@RequestBody Order order) {
        kafkaProducerService.send(order);
    }

}

在可能的情况下,我想使用 springboot 从文件发送数据。 关于如何完成的任何想法?

这帮助了我:

.propretiespom.xml中进行适当配置后,我可以在我的服务层中使用它。

private final static String TOPIC = "TOPIC";

@Autowired
private KafkaTemplate<String, Sting> kafkaTemplate;

public void someMethod(){
    
    //... code
    
    kafkaTemplate.send(TOPIC, obj.toString());
}

我需要发送字符串并且它工作正常,希望它有所帮助。 我还制定了以编程方式在 Kafka 队列上发送的预定方法。

您还应该添加到您的 pom

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
</dependency>

这就是我需要的 my.properties

# Required connection configs for Kafka producer, consumer, and admin
spring.kafka.properties.sasl.mechanism=PLAIN
spring.kafka.properties.bootstrap.servers=server
spring.kafka.properties.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule   required username=''   password='';
spring.kafka.properties.security.protocol=SASL_SSL

#Serialization
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

# Best practice for higher availability in Apache Kafka clients prior to 3.0
spring.kafka.properties.session.timeout.ms=45000

让我知道这是否有帮助或需要其他东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM