Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実践PubSubマイクロサービス / Implementation Pub Sub microservice

nrs
November 10, 2023

実践PubSubマイクロサービス / Implementation Pub Sub microservice

イベント駆動なPUB/SUBのマイクロサービスについてお話しています。
具体的なコードを追いながらマイクロサービスを連携させているかの開設がメインです。
こちらのは JJUG CCC 2023 Fall の講演用資料です。
ライブコードリーディング前提の資料です。そのため、ライブコードリーディングがないと理解が難しいかもしれません。

YouTube: https://www.youtube.com/watch?v=gejnwpvsWJE
Code: https://github.com/nrslib/pubsubdoc

# URL
YouTube: https://www.youtube.com/c/narusemi
HomePage: https://nrslib.com
Twitter: https://twitter.com/nrslib

nrs

November 10, 2023
Tweet

More Decks by nrs

Other Decks in Programming

Transcript

  1. public record DocCreated(
    DocId docId,
    String body
    ) implements DocEvent {
    }
    public record Doc(
    DocId docId,
    String body
    ) implements EventDrivenAggregateRoot {
    ...
    }
    public record DocCreate(DocId docId, String body)
    implements DocCommand {
    }

    View full-size slide

  2. @Configuration
    public class AxonConfiguration {
    @Bean
    public CorrelationDataProvider processIdCorrelationDataProvider() {
    return new MultiCorrelationDataProvider>(
    List.of(
    new SimpleCorrelationDataProvider("processId"),
    new MessageOriginProvider()
    )
    );
    }
    }

    View full-size slide

  3. axon:
    kafka:
    client-id: pubsubdoc-service-back
    producer:
    retries: 0
    consumer:
    event-processor-mode: tracking
    properties:
    security.protocol: PLAINTEXT
    default-topic: pubsubdoc-service-topic
    bootstrap-servers: localhost:29092,localhost:29192,localhost:29292

    View full-size slide

  4. application:
    kafka:
    topics: doc-service-topic, payment-service-topic, pubsubdoc-service-topic, user-service-topic
    @Configuration
    @ConditionalOnExpression("!${application.disable-kafka:false} and '${axon.kafka.consumer.event-processor-mode}' == 'trackin
    public class AxonKafkaConfiguration {
    @Value("${application.kafka.topics}")
    private List topics;
    @Bean
    public StreamableKafkaMessageSource streamableKafkaMessageSource(...) {
    return StreamableKafkaMessageSource.builder()
    .topics(topics)
    .consumerFactory(kafkaConsumerFactory)
    .fetcher(kafkaFetcher)
    .messageConverter(kafkaMessageConverter)
    .bufferFactory (
    () -> new SortedKafkaMessageBuffer<>(
    properties.getFetcher().getBufferSize()
    )

    View full-size slide

  5. @Configuration
    @ConditionalOnExpression("!${application.disable-kafka:false} and '${axon.kafka.consumer.event-pr
    public class AxonKafkaTrackingConfiguration {
    @Autowired
    public void registerProcessor(
    EventProcessingConfigurer configurer,
    StreamableKafkaMessageSource streamableKafkaMessageSource) {
    var processorNames = List.of(
    DocProjection.class.getPackageName(),
    UserProjection.class.getPackageName()
    );
    processorNames.forEach(it ->
    configurer.registerTrackingEventProcessor(
    it,
    c -> streamableKafkaMessageSource
    ));
    }
    }

    View full-size slide

  6. axon:
    eventhandling:
    processors:
    "[com.example.service.processor]":
    initialSegmentCount: 3
    eventProcessingConfiguration.eventProcessors()
    .map { it.value }
    .filter {
    it is TrackingEventProcessor
    }.forEach {
    val casted = it as TrackingEventProcessor
    casted.splitSegment(segmentId)
    }

    View full-size slide