1.kafukaconfigs
@ConditionalOnProperty(name = "kafka.qllgj.feedback.producer.enable", havingValue = "true", matchIfMissing = true)
@Configuration
@EnableKafka
@Slf4j
public class QLLGJFeedbackProducerConfiguration {
@Value("${kafka.qllgj.feedback.producer.servers:10.111.11.11:1010,10.111.11.11:1026,10.111.11.11:1017}")
private String qllGjFeedbackProducerServers;
@Value("${kafka.qllgj.feedback.producer.retries:1}")
private int retriesFeedback;
@Value("${kafka.qllgj.feedback.roducer.batch.size:4096}")
private int batchSizeFeedback;
@Value("${kafka.qllgj.feedback.producer.linger:1}")
private int lingerFeedback;
@Value("${kafka.qllgj.feedback.producer.buffer.memory:40960}")
private int bufferMemoryFeedback;
@Value("${kafka.qllgj.feedback.sasl-jaas-config:org.apache.kafka.common.security.scram.ScramLoginModule required username=\"admin\" password=\"NTA4YjRhZDBmYjQ3\";}")
private String qllGjFeedbackKafkaSASLJaasConfig;
@Value("${kafka.qllgj.feedback.inner.sasl-mechanism:SCRAM-SHA-512}")
private String qllGjFeedbackKafkaSASLMechanism;
@Value("${kafka.qllgj.feedback.inner.security-protocol:SASL_PLAINTEXT}")
private String qllGjFeedbackKafkaSecurityProtocol;
// 构建producer配置项对象
private Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, qllGjFeedbackProducerServers);
props.put(ProducerConfig.RETRIES_CONFIG, retriesFeedback);
props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSizeFeedback);
props.put(ProducerConfig.LINGER_MS_CONFIG, lingerFeedback);
props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemoryFeedback);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
if (StringUtils.isNotEmpty(qllGjFeedbackKafkaSecurityProtocol)) {
props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, qllGjFeedbackKafkaSecurityProtocol);
props.put(SaslConfigs.SASL_MECHANISM, qllGjFeedbackKafkaSASLMechanism);
props.put("sasl.jaas.config", qllGjFeedbackKafkaSASLJaasConfig);
props.put("compression.type","lz4");
}
return props;
}
private ProducerFactory<String, String> qllgjFeedbackKafkaProducerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
// 使用producerFactory来构建kafkaFeedbackTemplate的bean
@Bean(name = "qllgjFeedbackKafkaTemplate")
public KafkaTemplate<String, String> qllgjFeedbackKafkaTemplate() {
log.info("create qllgj feedback kafka producer");
return new KafkaTemplate<>(qllgjFeedbackKafkaProducerFactory());
}
}
2.发送消息
@Slf4j
@Service
@ConditionalOnProperty(name = "kafka.qllgj.feedback.producer.enable", havingValue = "true", matchIfMissing = true)
public class SendResultServiceImpl implements SendResultService {
@Resource(name = "qllgjFeedbackKafkaTemplate")
private KafkaTemplate<String, String> qllgjFeedbackKafkaTemplate;
@Value("${qll.gj.feedback.tickets.create.topic.name:QLLGJ-PUSH-WORK-ORDER-NOTIFY}")
private String qllgjFeedbackCreateTopicName;
@Override
public void sendResultFeedback(FeedbackReq feedbackReq) {
CompletableFuture.runAsync(() -> {
boolean sendSuccess = false;
String body = JacksonUtil.object2Json(feedbackReq);
for (int retryTimes = 0; retryTimes < 3; ++retryTimes) {
try {
ListenableFuture<SendResult<String, String>> listenableFuture
= qllgjFeedbackKafkaTemplate.send(qllgjFeedbackCreateTopicName, body);
SendResult<String, String> sendResult = listenableFuture.get();
log.info("qll guanjia reply result has been send: {}, sendResult:{}", body, sendResult);
sendSuccess = true;
break;
} catch (Exception e) {
log.error("kafka send message failed", e);
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
}
if (!sendSuccess) {
log.warn("Qll guanjia feedback failed:{}", body);
}
});
}
}
3.消费