mirror of
https://gitee.com/san-bing/JChargePointProtocol
synced 2026-05-09 20:39:55 +08:00
云快充1.5.0 初始化
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.queue.provider;
|
||||
|
||||
|
||||
import sanbing.jcpp.infrastructure.queue.ProtoQueueMsg;
|
||||
import sanbing.jcpp.infrastructure.queue.QueueConsumer;
|
||||
import sanbing.jcpp.infrastructure.queue.QueueProducer;
|
||||
import sanbing.jcpp.proto.gen.ProtocolProto.UplinkQueueMessage;
|
||||
|
||||
public interface AppQueueFactory {
|
||||
|
||||
QueueConsumer<ProtoQueueMsg<UplinkQueueMessage>> createProtocolUplinkMsgConsumer();
|
||||
|
||||
QueueProducer<ProtoQueueMsg<UplinkQueueMessage>> createProtocolUplinkMsgProducer(String topic);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.queue.provider;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
import sanbing.jcpp.infrastructure.queue.ProtoQueueMsg;
|
||||
import sanbing.jcpp.infrastructure.queue.QueueConsumer;
|
||||
import sanbing.jcpp.infrastructure.queue.QueueProducer;
|
||||
import sanbing.jcpp.infrastructure.queue.memory.InMemoryQueueConsumer;
|
||||
import sanbing.jcpp.infrastructure.queue.memory.InMemoryQueueProducer;
|
||||
import sanbing.jcpp.infrastructure.queue.memory.InMemoryStorage;
|
||||
import sanbing.jcpp.infrastructure.queue.settings.QueueAppSettings;
|
||||
import sanbing.jcpp.proto.gen.ProtocolProto.UplinkQueueMessage;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnExpression("'${queue.type:null}'=='memory' && '${service.type:null}'=='monolith'")
|
||||
public class InMemoryAppQueueFactory implements AppQueueFactory {
|
||||
|
||||
private final InMemoryStorage storage;
|
||||
private final QueueAppSettings appSettings;
|
||||
|
||||
public InMemoryAppQueueFactory(InMemoryStorage storage, QueueAppSettings appSettings) {
|
||||
this.storage = storage;
|
||||
this.appSettings = appSettings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueueConsumer<ProtoQueueMsg<UplinkQueueMessage>> createProtocolUplinkMsgConsumer() {
|
||||
return new InMemoryQueueConsumer<>(storage, appSettings.getTopic());
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueueProducer<ProtoQueueMsg<UplinkQueueMessage>> createProtocolUplinkMsgProducer(String topic) {
|
||||
return new InMemoryQueueProducer<>(storage, topic);
|
||||
}
|
||||
|
||||
@Scheduled(fixedRateString = "${queue.in_memory.stats.print-interval-ms:60000}")
|
||||
private void printInMemoryStats() {
|
||||
storage.printStats();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* 抖音关注:程序员三丙
|
||||
* 知识星球:https://t.zsxq.com/j9b21
|
||||
*/
|
||||
package sanbing.jcpp.infrastructure.queue.provider;
|
||||
|
||||
import com.google.protobuf.util.JsonFormat;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.stereotype.Component;
|
||||
import sanbing.jcpp.infrastructure.queue.ProtoQueueMsg;
|
||||
import sanbing.jcpp.infrastructure.queue.QueueAdmin;
|
||||
import sanbing.jcpp.infrastructure.queue.QueueConsumer;
|
||||
import sanbing.jcpp.infrastructure.queue.QueueProducer;
|
||||
import sanbing.jcpp.infrastructure.queue.discovery.ServiceInfoProvider;
|
||||
import sanbing.jcpp.infrastructure.queue.kafka.*;
|
||||
import sanbing.jcpp.infrastructure.queue.settings.QueueAppSettings;
|
||||
import sanbing.jcpp.proto.gen.ProtocolProto.UplinkQueueMessage;
|
||||
|
||||
@Component
|
||||
@ConditionalOnExpression("'${queue.type:null}'=='kafka'")
|
||||
public class KafkaAppQueueFactory implements AppQueueFactory {
|
||||
|
||||
private final KafkaSettings kafkaSettings;
|
||||
private final QueueAppSettings appSettings;
|
||||
private final KafkaConsumerStatsService consumerStatsService;
|
||||
private final ServiceInfoProvider serviceInfoProvider;
|
||||
|
||||
private final QueueAdmin appAdmin;
|
||||
|
||||
public KafkaAppQueueFactory(KafkaSettings kafkaSettings,
|
||||
ServiceInfoProvider serviceInfoProvider,
|
||||
QueueAppSettings appSettings,
|
||||
KafkaConsumerStatsService consumerStatsService,
|
||||
KafkaTopicConfigs kafkaTopicConfigs) {
|
||||
this.kafkaSettings = kafkaSettings;
|
||||
this.serviceInfoProvider = serviceInfoProvider;
|
||||
this.appSettings = appSettings;
|
||||
this.consumerStatsService = consumerStatsService;
|
||||
|
||||
this.appAdmin = new KafkaAdmin(kafkaSettings, kafkaTopicConfigs.getAppConfigs());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public QueueConsumer<ProtoQueueMsg<UplinkQueueMessage>> createProtocolUplinkMsgConsumer() {
|
||||
KafkaConsumerTemplate.KafkaConsumerTemplateBuilder<ProtoQueueMsg<UplinkQueueMessage>> consumerBuilder = KafkaConsumerTemplate.builder();
|
||||
consumerBuilder.settings(kafkaSettings);
|
||||
consumerBuilder.topic(appSettings.getTopic());
|
||||
consumerBuilder.clientId("protocol-uplink-consumer-" + serviceInfoProvider.getServiceId());
|
||||
consumerBuilder.groupId("protocol-uplink-consumer");
|
||||
if (appSettings.getDecoder() == QueueAppSettings.DecoderType.protobuf) {
|
||||
consumerBuilder.decoder(msg -> new ProtoQueueMsg<>(msg.getKey(), UplinkQueueMessage.parseFrom(msg.getData()), msg.getHeaders()));
|
||||
} else {
|
||||
consumerBuilder.decoder(msg -> {
|
||||
UplinkQueueMessage.Builder builder = UplinkQueueMessage.newBuilder();
|
||||
JsonFormat.parser().merge(new String(msg.getData()), builder);
|
||||
return new ProtoQueueMsg<>(msg.getKey(), builder.build(), msg.getHeaders());
|
||||
});
|
||||
}
|
||||
consumerBuilder.admin(appAdmin);
|
||||
consumerBuilder.statsService(consumerStatsService);
|
||||
return consumerBuilder.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueueProducer<ProtoQueueMsg<UplinkQueueMessage>> createProtocolUplinkMsgProducer(String topic) {
|
||||
KafkaProducerTemplate.KafkaProducerTemplateBuilder<ProtoQueueMsg<UplinkQueueMessage>> requestBuilder = KafkaProducerTemplate.builder();
|
||||
requestBuilder.settings(kafkaSettings);
|
||||
requestBuilder.clientId("protocol-to-app-" + serviceInfoProvider.getServiceId());
|
||||
requestBuilder.topic(topic);
|
||||
requestBuilder.admin(appAdmin);
|
||||
return requestBuilder.build();
|
||||
}
|
||||
|
||||
|
||||
@PreDestroy
|
||||
private void destroy() {
|
||||
if (appAdmin != null) {
|
||||
appAdmin.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user