java消息没有传递到kafka主题

hyrbngr7  于 2021-06-05  发布在  Kafka
关注(0)|答案(1)|浏览(413)

我正在开发一个简单的应用程序,使用springboot和kafka将一个对象保存到kafka主题中,然后有一个使用者将侦听该记录并将其添加到内存数据库中。在下面的配置之后,kafka和spring boot都在工作,但是对kafka的api调用将挂起一段时间,kafka主题中也不会有任何消息。
我遵循下面的教程
https://www.confluent.io/blog/apache-kafka-spring-boot-application/
但我改变了一点我的服务。

package com.dilshan.shoppingcart.cart;

import org.springframework.stereotype.Service;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;

@Service
public class ShoppingService {
    private static final String TOPIC = "users";

    @Autowired
    private KafkaTemplate<String, ShoppingCart> kafkaTemplate;

    private ShoppingCartRepository cartRepository;

    public void publish(ShoppingCart cart) {
        this.kafkaTemplate.send(TOPIC, cart);
    }

    @KafkaListener(topics = "users", groupId = "group_id")
    public void subscribe(ShoppingCart cart) throws IOException {
        System.out.println("Hello-"+cart.getId());
        cartRepository.save(cart);

    }

}

这是我的application.properties文件

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true
spring.h2.console.path=/h2
server.port=8080
spring.kafka.consumer.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=group_id
spring.kafka.consumer.auto-offset-reset=earliest
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.listener.missing-topics-fatal=false

这是我用来访问Kafka方法的控制器。

package com.dilshan.shoppingcart.cart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingService shoppingService;

    @RequestMapping(method = RequestMethod.POST, value = "/shoppingcart")
    private ResponseEntity<ShoppingCart> selectProducts( @RequestBody ShoppingCart cart) {
        shoppingService.publish(cart);
        return ResponseEntity.ok().build();
    }

}

下面的日志在应用程序日志中不断显示。

2020-02-18 19:22:12.951[0;39m [33m WARN[0;39m [35m6986[0;39m [2m---[0;39m [2m[ntainer#0-0-C-1][0;39m [36morg.apache.kafka.clients.NetworkClient  [0;39m [2m:[0;39m [Consumer clientId=consumer-1, groupId=group_id] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[2m2020-02-18 19:22:13.958[0;39m [33m WARN[0;39m [35m6986[0;39m [2m---[0;39m [2m[ntainer#0-0-C-1][0;39m [36morg.apache.kafka.clients.NetworkClient  [0;39m [2m:[0;39m [Consumer clientId=consumer-1, groupId=group_id] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[2m2020-02-18 19:22:15.115[0;39m [33m WARN[0;39m [35m6986[0;39m [2m---[0;39m [2m[ntainer#0-0-C-1][0;39m [36morg.apache.kafka.clients.NetworkClient  [0;39m [2m:[0;39m [Consumer clientId=consumer-1, groupId=group_id] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
[2m2020-02-18 19:22:16.272[0;39m [33m WARN[0;39m [35m6986[0;39m [2m---[0;39m [2m[ntainer#0-0-C-1][0;39m [36morg.apache.kafka.clients.NetworkClient  [0;39m [2m:[0;39m [Consumer clientId=consumer-1, groupId=group_id] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available.
wtzytmuj

wtzytmuj1#

kafka代理,有时称为kafka服务器,应该在您的pc或远程机器上运行。
请检查此链接https://kafka.apache.org/quickstart
首先,您需要运行zookeeper和kafka代理,然后您可以使用应用程序发送消息。

相关问题