2021-09-09 21:42:40.039 WARN 14636 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-mygroup-1, groupId=mygroup] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2021-09-09 21:42:40.039 WARN 14636 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-mygroup-1, groupId=mygroup] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected 2021-09-09 21:42:41.778 WARN 14636 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2021-09-09 21:42:41.779 WARN 14636 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected 2021-09-09 21:42:43.001 WARN 14636 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-mygroup-1, groupId=mygroup] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2021-09-09 21:42:43.002 WARN 14636 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-mygroup-1, groupId=mygroup] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected 2021-09-09 21:42:44.634 WARN 14636 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2021-09-09 21:42:44.634 WARN 14636 --- [ad | producer-1] org.apache.kafka.clients.NetworkClient : [Producer clientId=producer-1] Bootstrap broker localhost:9092 (id: -1 rack: null) disconnected 2021-09-09 21:42:45.908 WARN 14636 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-mygroup-1, groupId=mygroup] Connection to node -1 (localhost/127.0.0.1:9092) could not be established. Broker may not be available. 2021-09-09 21:42:45.908 WARN 14636 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient : [Consumer clientId=consumer-mygroup-1, groupId=mygroup]
代码
@Service public class KafKaConsumerService { private final Logger logger = LoggerFactory.getLogger(KafKaConsumerService.class); @KafkaListener(topics="mytopic", groupId="mygroup") public void consumeFromTopic(String message) { System.out.println("Consummed message "+message); public class KafKaProducerService { private static final Logger logger = LoggerFactory.getLogger(KafKaProducerService.class); public static final String topic = "mytopic"; @Autowired private KafkaTemplate<String, String> kafkaTemp; public void publishToTopic(String message) { System.out.println("Publishing to topic "+topic); this.kafkaTemp.send(topic, message); }
属性
spring.kafka.consumer.bootstrap-servers=localhost:9092 spring.kafka.consumer.group-id=mygroup spring.kafka.consumer.auto-offset-reset=earliest