java中springboot集成kafka生产者Producer启动报错kafkaTemplate模板报错

前言:之前博客里面提到本公司为物联网项目。项目中使用mqtt+kafka进行与设备端的通讯,之前的协议格式为json格式,现在改成字节数组byte[]格式进行通信。

  • 报错信息如下
Can't convert value of class [B to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer

Field kafkaTemplateByte in com.hiss.producer.KafkaNewProducer required a bean of type 'org.springframework.kafka.core.KafkaTemplate' that could not be found.
- Bean method 'kafkaTemplate' in 'KafkaAutoConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.kafka.core.KafkaTemplate; SearchStrategy: all) found bean 'kafkaTemplate'


Action:
Consider revisiting the conditions above or defining a bean of type 'org.springframework.kafka.core.KafkaTemplate' in your configuration.

  • 分析原因
    之前声明KafkaTemplate时候value值为String,后续改为byte[]时候,未将KafkaTemplate的value类型改为byte[]类型。

  • 解决办法

 public KafkaTemplate<String, byte[]> kafkaTemplateByte() {
    	return new KafkaTemplate<String,byte[]>(producerFactoryByte());
    }
要在springboot集成kafka生产者,需要遵循以下步骤: 1. 添加Maven依赖 在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.7.2</version> </dependency> ``` 2. 配置Kafka生产者 在application.properties文件添加Kafka的配置: ```properties spring.kafka.producer.bootstrap-servers=<broker地址> spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer ``` 3. 创建Kafka生产者 在代码创建Kafka生产者: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Service; @Service public class KafkaProducerService { private final KafkaTemplate<String, String> kafkaTemplate; @Autowired public KafkaProducerService(KafkaTemplate<String, String> kafkaTemplate) { this.kafkaTemplate = kafkaTemplate; } public void sendMessage(String topic, String message) { this.kafkaTemplate.send(topic, message); } } ``` 4. 发送消息 在需要发送消息的地方,注入KafkaProducerService,并调用sendMessage方法: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class MessageController { private final KafkaProducerService kafkaProducerService; @Autowired public MessageController(KafkaProducerService kafkaProducerService) { this.kafkaProducerService = kafkaProducerService; } @PostMapping("/message") public void sendMessage(@RequestBody String message) { this.kafkaProducerService.sendMessage("test-topic", message); } } ``` 以上就是在springboot集成Kafka生产者的步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值