5.3 Microservices Setup

Understand the setup of the Order microservice.

In this stage we will have a look at the setup and configuration of the Order microservice.

Task 5.3.1 - Inspect Dependencies

To simplify the use of the Outbox pattern the following dependencies have already been added to the pom.xml.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<dependencies>
    <dependency>
        <groupId>io.debezium</groupId>
        <artifactId>debezium-quarkus-outbox</artifactId>
        <version>${debezium.version}</version>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-smallrye-reactive-messaging-kafka</artifactId>
    </dependency>
    <dependency>
        <groupId>io.opentracing.contrib</groupId>
        <artifactId>opentracing-kafka-client</artifactId>
    </dependency>
    ...
</dependencies>
Dependency Description
quarkus-smallrye-reactive-messaging-kafka Reactive Messaging extensions to receive and write Messages to or from Apache Kafka.
debezium-quarkus-outbox Provides a reusable, configurable component that facilitates the use of the Outbox pattern in conjunction with debezium.
opentracing-kafka-client Injects and reads tracing information from Headers of an Apache Kafka message.

full source of pom.xml

Debezium Quarkus Outbox Extension

This quarkus extension provides us with the ExportedEvent interface. For every different event in our application we will implement this interface to create our custom event.

The application will then be able to emit the event using the javax.enterprise.event.Event class. The extension will also take care of writing the emitted events to our outboxevent database table.

Task 5.3.2 - Inspect application properties

The configurations for the Reactive-Messaging and the Outbox Pattern are located in the quarkus configuration file application.properties.

Have a look at the Quarkus Debezium Outbox pattern configuration.

1
2
3
4
5
6
7
8
quarkus.debezium-outbox.table-name=outboxevent
quarkus.debezium-outbox.id.name=id
quarkus.debezium-outbox.aggregate-id.name=aggregateid
quarkus.debezium-outbox.aggregate-type.name=aggregatetype
quarkus.debezium-outbox.type.name=type
quarkus.debezium-outbox.timestamp.name=timestamp
quarkus.debezium-outbox.payload.name=payload
quarkus.debezium-outbox.remove-after-insert=false

This configuration will define the table and field names of our outbox table. For further details, please refer to the Debezium Outbox Quarkus Extension documentation.

Please take a note of the configuration quarkus.debezium-outbox.remove-after-insert=false (default true). With disabling this our events stay in the outbox table forever. In theory there is no need to keep these persisted. Debezium will get the create event (row insert) from the WAL log. The provided EventRouter is only sensible to create events. For debugging we have set this to false.

Now have a look at the reactive messaging configuration.

1
2
3
4
5
6
7
mp.messaging.incoming.stock.topics=stock-stockcomplete-events,stock-stockincomplete-events
mp.messaging.incoming.stock.group.id=order-service
mp.messaging.incoming.stock.connector=smallrye-kafka
mp.messaging.incoming.stock.bootstrap.servers=kafka:9092
mp.messaging.incoming.stock.key.deserializer=org.apache.kafka.common.serialization.StringDeserializer
mp.messaging.incoming.stock.value.deserializer=org.apache.kafka.common.serialization.StringDeserializer
mp.messaging.incoming.stock.interceptor.classes=io.opentracing.contrib.kafka.TracingConsumerInterceptor

With this configuration our application will use a Kafka connector and will provide access to the messages using an incoming channel named stock. This channel will contain events from both topics stock-stockcomplete-events and stock-stockincomplete-events.

To get more details about the concepts of Connectors and Channels you may head over to the SmallRye Reactive Messaging Documentation

Have a look at the snippet below which is used do consume the messages

1
2
3
4
@Incoming("stock")
public CompletionStage<Void> onMessage(KafkaRecord<String, String> message) {
   // implementation
}

If you want to know more about reactive messaging in quarkus have a look at the Quarkus Reactive Messaging Guide.