This program demonstrates Kafka integration using Ballerina, featuring both Kafka consumer and producer functionalities. The program allows you to configure the Kafka server URL, username, and password. It consumes order objects published to the "order" topic from another Kafka producer. If the payment status of an order is successful, the program produces the same order object to the "payment-success-order" topic.
- Ballerina
- Access to a Kafka server
- Basic understanding of Kafka concepts
Create a Config.toml
file and configure the following properties to match your Kafka setup:
[wso2.kakfa.integration.kafkaConfig]
kafkaEndpoint = "KAFKA_SERVER_URL"
kafkaUsername = "KAFKA_USERNAME"
kafkaPass = "KAFKA_PASSWORD"
If you are using Choreo, you can provide values for configuarables via the Choreo Console.
The Kafka consumer in this program subscribes to the "order" topic and listens for incoming order objects. When an order object is received, the program checks the payment status. If the payment status is successful, the order object is sent to the "payment-success-order" topic using the Kafka producer.
The Kafka producer in this program reads order objects from the "order" topic and sends them to the "payment-success-order" topic when the payment status is SUCESSFUL.
{
"id": 1,
"desc": "Order Description",
"paymentStatus": "SUCCESS"
}
- Creating a Event Handler Component
- Fork this repository
- Login to Choreo
- Navigate to create a
Event Handler
component - Provide a name and description for the component
- Authorize and select the GitHub details
- Select the
GitHub Account
and the forked repository forGitHub Repository
- Select the
Branch
asmain
- Select
Ballerina
asBuildpack
- Select
kafka-order-processing-client
as theBallerina Project Directory
- Click on "Create" to create the component
- Build and deploy the component
- Once the component is created, add the following environment variables:
KAFKA_SERVER_URL
- The Kafka server URLKAFKA_USERNAME
- The Kafka usernameKAFKA_PASSWORD
- The Kafka password
- Deploy the component
- Once the component is created, add the following environment variables:
Send a message to the "order" topic using a Kafka producer. The message should be a JSON object with the following structure:
{
"id": 1,
"desc": "Order Description",
"paymentStatus": "SUCCESS"
}
Check the logs of the component to see the order object being consumed and produced to the "payment-success-order" topic. You can also check the "payment-success-order" topic to see the order object being produced.