はじめに
この記事では、
LocalStack上でエミュレートしたAWS Kinesisストリームに、Apache Camel (Spring Boot) アプリからメッセージを送信する方法を紹介します。
開発・検証をローカル完結で進めたい方に向けた、シンプルな構成です。
環境
- OS:Ubuntu(WSL2)
- Java:Amazon Corretto 21
- Spring Boot:3.4.4
- Apache Camel:4.8.3.redhat-00009
- LocalStack:Docker版最新
1. LocalStack準備
1-1. LocalStack起動
sudo docker run --rm -it -p 4566:4566 -p 4571:4571 localstack/localstack
1-2. AWS CLI設定(ダミー値)
aws configure
設定値は次の通り:
AWS Access Key ID : test
AWS Secret Access Key : test
Default region name : ap-northeast-1
Default output format : json
1-3. Kinesisストリーム作成
aws --endpoint-url=http://localhost:4566 kinesis create-stream \
--stream-name aws-network-test-kinesis \
--shard-count 1 \
--region ap-northeast-1
ストリーム作成後、確認:
aws --endpoint-url=http://localhost:4566 kinesis list-streams --region ap-northeast-1
2. Camel Spring Boot アプリ構成
2-1. pom.xml 依存関係(抜粋)
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-aws2-kinesis-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-platform-http-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2-2. application.properties
server.port=8180
aws.accessKey=test
aws.secretKey=test
aws.region=ap-northeast-1
kinesis.streamName=aws-network-test-kinesis
2-3. camel-context.xml
今回は、ループで複数メッセージを送信する構成にしています。
パーティションキーには、Camelが生成する一意なExchange IDを利用しています。
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="sendToKinesisRoute">
<from uri="platform-http:/send"/>
<setHeader name="CamelLoopCount">
<simple>${header.count}</simple>
</setHeader>
<loop>
<simple>${header.CamelLoopCount}</simple>
<setHeader name="CamelAwsKinesisPartitionKey">
<simple>${exchangeId}</simple>
</setHeader>
<setBody>
<simple>This is message ${exchangeProperty.CamelLoopIndex} at ${date:now:yyyy/MM/dd HH:mm:ss,SSS}</simple>
</setBody>
<to uri="aws2-kinesis://{{kinesis.streamName}}?accessKey={{aws.accessKey}}&secretKey={{aws.secretKey}}®ion={{aws.region}}&uriEndpointOverride=http://localhost:4566&overrideEndpoint=true"/>
<choice>
<when>
<simple>${header.CamelAwsKinesisSequenceNumber} != null</simple>
<log message="✅ Success! Sequence Number = ${header.CamelAwsKinesisSequenceNumber}"/>
</when>
<otherwise>
<log message="❌ Failed to send to Kinesis! No Sequence Number"/>
</otherwise>
</choice>
</loop>
</route>
</camelContext>
3. 動作確認
アプリ起動後、以下のリクエストでメッセージ送信テストを行います。
curl "http://localhost:8180/send?count=10"
リクエスト数分、Kinesisにメッセージが送信されます。
まとめ
LocalStackを使うことで、AWS環境を利用せずにKinesis連携を開発・検証できます。
Camel側では、ループやパーティションキー設定も柔軟に設計できるため、実運用に近い動きも再現可能です。