This example illustrates how to use Spring Boot with Camel and Apache CXF implementing REST services using bottom-up approach.
If you are using REST services directly from Camel and you want to use OpenTelemetry please refer to the dedicated example
There are 3 services which communicate each other, starting from the random
service:
-
random: the main service, exposes the entry point REST service and store the results
-
even: the service that verifies the even numbers
-
odd: the service that verifies the odd numbers
moreover there is a common module containing common classes
You can build this example using the following command; it will also download the OpenTelemetry agent used to instrument the applications:
$ mvn package -Potel-agent
Run docker-compose to start all the needed services:
$ docker-compose -f containers/docker-compose.yml up -d
the command runs:
-
minio as application storage
-
opentelemetry-collector to receive the generated Trace/Span
-
Jaeger to visualize the traces
Run each services on separated terminals:
$ source containers/env.sh \ && java -javaagent:target/opentelemetry-javaagent.jar \ -Dotel.service.name=random \ -jar rest-cxf-otel-random/target/*.jar
$ source containers/env.sh \ && java -javaagent:target/opentelemetry-javaagent.jar \ -Dotel.service.name=even \ -Dserver.port=8081 \ -jar rest-cxf-otel-even/target/*.jar
$ source containers/env.sh \ && java -javaagent:target/opentelemetry-javaagent.jar \ -Dotel.service.name=odd \ -Dserver.port=8082 \ -jar rest-cxf-otel-odd/target/*.jar
After the Spring Boot applications have been started, you can open the following URL in your web browser http://localhost:8080/services/ to access the list of services, including WADL definition
You can also access the REST endpoint from the command line:
i.e. to generate 5 random numbers run:
$ curl -X POST http://localhost:8080/services/api/play/5 -s | jq .
The command will produce an output like:
{
"result": {
"ODD": [
{
"number": 229,
"type": "ODD"
},
{
"number": 585,
"type": "ODD"
}
],
"EVEN": [
{
"number": 648,
"type": "EVEN"
},
{
"number": 670,
"type": "EVEN"
},
{
"number": 846,
"type": "EVEN"
}
]
},
"evenCount": 3,
"oddCount": 2
}
Now in the Jaeger UI http://localhost:16686 the traces will be available, both from Camel and from CXF instrumentation
The services can be stopped pressing [CTRL] + [C]
in the shell, and the containers can be stopped with
$ docker-compose -f containers/docker-compose.yml down
The dependency org.apache.cxf:cxf-integration-tracing-opentelemetry
provides org.apache.cxf.tracing.opentelemetry.jaxrs.OpenTelemetryFeature
that instruments the OpenTelemetry API implemented in the java agent.
The feature is injected in the Spring context
link:rest-cxf-otel-common/src/main/java/org/apache/camel/example/springboot/cxf/otel/CxfConfig.java[role=include]
and then it is configured in the Camel route as provider in the cxfrs
endpoint
link:rest-cxf-otel-random/src/main/java/org/apache/camel/example/springboot/cxf/otel/CamelRouter.java[role=include]
When the feature is enabled it is possible to add custom tags or logs using TracerContext
from CXF
link:rest-cxf-otel-random/src/main/java/org/apache/camel/example/springboot/cxf/otel/RandomService.java[role=include]
link:rest-cxf-otel-random/src/main/java/org/apache/camel/example/springboot/cxf/otel/RandomServiceImpl.java[role=include]
If you hit any problem using Camel or have some feedback, then please let us know.
We also love contributors, so get involved :-)
The Camel riders!