Java实现Kafka生产者和消费者的示例(1),mysqlsql语句面试题

RecordMetadata recordMetadata = null;

try {

//将消息发送到Kafka服务器的名称为“one-more-topic”的Topic中

recordMetadata = kafkaProducer.send(new ProducerRecord<>(“one-more-topic”, uuid)).get();

log.info(“recordMetadata: {}”, recordMetadata);

log.info(“uuid: {}”, uuid);

} catch (Exception e) {

log.error(“send fail, uuid: {}”, uuid, e);

}

return uuid;

}

}

消费者

创建一个KafkaConsumer的消费者实例:

@Configuration

public class Config {

public final static String groupId = “kafka-clients-group”;

public final static String bootstrapServers = “127.0.0.1:9092”;

@Bean(destroyMethod = “close”)

public KafkaConsumer kafkaConsumer() {

Properties props = new Properties();

//设置Kafka服务器地址

props.put(“bootstrap.servers”, bootst

你可能感兴趣的:(程序员,面试,java,后端)