我正在使用Spring Boot和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.
答案 0 :(得分:0)
Kafka代理,有时也称为Kafka服务器,应该在您的PC或远程计算机上运行。
请检查此链接 https://kafka.apache.org/quickstart
首先,您需要运行Zookeeper和Kafka经纪人 然后,您可以使用您的应用程序发送消息。