-
Notifications
You must be signed in to change notification settings - Fork 7
Routing API requests
This page describes how API requests are routed to a microservice and an API response is sent back to the client, leveraging Apache Camel to provide most of the glue code.
Referring to the code version after PR #71 was merged.
- In the
app
Docker container, when aController
class receives an API request, it converts the requests into a model, feeds that model as a message into a Camel route, and converts the result of the last route endpoint into an API response object for sending back to the client -- seeDemoController
for an example. - The
DemoController
usesCamelEntrance
to inject messages into a Camel endpoint for Camel routing. - Camel routes are defined by
RouteBuilder
subclasses, likePrimaryRoutes
andClaimProcessorRoute
. For example,-
DemoController.assess_health_data
callsCamelEntrance.assess_health_data_demo
- to inject an
AssessHealthData
message into thedirect:assess_health_data_demo
endpoint, - which is defined in
PrimaryRoutes
as a route that goes torabbitmq:assess_health_data?routingKey=health_data_assessor
(thehealth_data_assessor
queue in RabbitMQ'sassess_health_data
direct exchange -- RabbitMQ/AMQP concepts).
-
- In a separate
service-ruby
Docker container, a Ruby microservice is subscribed to RabbitMQ'shealth_data_assessor
queue (using RabbitSubscriberservice-ruby/src/lib/rabbit_subscriber.rb
). As configured inmicroservices.rb
, when a message is popped out of that queue,HealthDataAssessor
will process the message and return a result, which is sent as a reply RabbitMQ message.- It is intended that communication with microservices use RabbitMQ, typically via a direct exchange and routing key to the queue. The message should be a JSON string (automatically encoded as
byte[]
) -- this will facilitate replacing RabbitMQ with other messaging systems (like AWS SQS) if needed.
- It is intended that communication with microservices use RabbitMQ, typically via a direct exchange and routing key to the queue. The message should be a JSON string (automatically encoded as
- The reply message is received back on the Camel route in the
app
Docker container. When the last endpoint on the route is completed, the last message is converted to the return object ofCamelEntrance.assess_health_data_demo
, which will be mapped to the API response object inDemoController
.
When an API request takes more than 30 seconds to complete, consider sending an immediate API response that includes a URL for the client to check on the status and/or to poll for the result. Check out POST request in the CamelApp where a claim is processed in the background and its status
is updated.
We leverage the recipientList EIP to send the POSTed claim to multiple endpoints. Since the endpoints are configured to be asynchronous, we do not wait for the results and move on to the next endpoint of the Camel route (using the same message that was sent to the recipientList
).