Skip to content

Latest commit

 

History

History
524 lines (444 loc) · 13 KB

walkthrough.adoc

File metadata and controls

524 lines (444 loc) · 13 KB

Camel Spring Boot - Simple Demo

Explore, build, test and deploy a Camel Spring Boot demo application using the Developer Sandbox and OpenShift Dev Spaces.

This hands-on lab is based on the following blog article in Red Hat Developers:


Assuming you have followed the article’s instructions, you should be all set to get hands-on with Camel Spring Boot in the OpenShift Dev Spaces workspace.

For illustration purposes, the picture below shows what the integration end-to-end flow looks like.

00 demo end2end

A client invokes an OpenApi service. A Camel route attends the call, translates the JSON input into XML and calls a backend service to obtain an XML response, then it’s translated back to JSON before responding to the original service call.


Explore the source code

The Camel source file api-simple.xml defines the entire end-to-end processing logic, which you can find in your project explorer under the path:

  • camelsb/level1simple/src/main/resources/camel/api-simple.xml

    00 camel routes

    Click on the Camel source file to display it in your code editor.


Inside the Camel source you’ll see the main route definition:

00 camel main

The key processing actions are:

  1. Performs the JSON to XML transformation

  2. Invokes the backend service

  3. Transforms the XML response into JSON

The code above is written using the XML DSL (Domain Specific Language), but Camel also provides a Java DSL and a YAML DSL.

Feel free to explore other regions of the code and project if you are curious about the entire implementation.


Run the stub in your terminal

The stub acts as the backend service that provides the XML data we need to fetch.


  1. Open your terminal

    Make sure you make your terminal visible in the IDE. You can toggle it using the keyboard keys Ctrl+` or simply find the option from the menu system as per the picture below:

    01 toggle terminal
  2. Let’s first run the stub

    Copy and paste the following command in your terminal to place yourself in the stub’s Camel Spring Boot project:

    cd camelsb/stubs/end1

    Then, copy/paste the following command to start the stub in the terminal:

    mvn -Dspring-boot.run.profiles=dev -s configuration/settings.xml

    • After Maven downloads all the dependencies, you should see in your terminal logs that the stub has started:

      02 stub terminal logs
      Note
      Two notifications will pop up to inform you of new listening ports. You can ignore these messages; they will automatically close after a few seconds.


    Now, test your stub from a new terminal. From your terminal’s top right corner, choose the Split option, as shown below:

    03 terminal split

    Copy/paste the following cURL command to obtain a response from the stub:

    curl -s \
    -H "content-type: application/xml" \
    -d '' \
    http://localhost:9000/camel/subscriber/details \
    | bat -pP -lxml
    Note
    The command also includes a pipe to colorize the XML output for better reading.

    The invocation should return an XML payload similar to:

    <Individual>
        <Name>Some</Name>
        <Surname>One</Surname>
        <Address>
            <Number>1</Number>
            <Street>Some Street</Street>
            <City>Somewhere</City>
            <PostCode>SOME C0D3</PostCode>
            <Country>UK</Country>
        </Address>
    </Individual>


Did you obtain the same XML message as shown above?

You’ve successfully tested the stub !!

Inspect in the stub logs to investigate the possible causes of failure.

Run the service in your terminal

The main Camel service exposes a JSON REST API and integrates with the XML backend service (the stub).


  1. Run the main service

    Copy and paste the following command in your terminal to place yourself in the main Camel Spring Boot project:

    cd /projects/devsandbox-camel/camelsb/level1simple/

    Then, copy/paste the following command to start the stub in the terminal:

    mvn -Dspring-boot.run.profiles=dev -s configuration/settings.xml

    • Some more dependencies will be downloaded before the engine starts. When done, you should see logs of both systems in both terminals:

      04 terminal system logs
      Note
      More notifications pop up about the new listening ports. You can ignore these messages; they will automatically close after a few seconds.

    Open a third terminal from which you can issue commands. From your terminal’s top right corner, choose the Split option as shown below:

    05 terminal split 2

    Copy/paste the following cURL command to obtain a response from the stub:

    curl -s \
    -H "content-type: application/json" \
    -d '{"id":"123"}' \
    http://localhost:8080/camel/subscriber/details | jq
    Note
    The command includes a pipe to parse the JSON response with JQuery, which nicely renders the returned JSON payload.

    The cURL command should return a JSON payload similar to:

    {
      "fullName": "Some One",
      "addressLine1": "1 Some Street",
      "addressLine2": "Somewhere SOME C0D3",
      "addressLine3": "UK"
    }


Did you obtain the same JSON response as the one shown above?

You’ve successfully tested the main service !!

Inspect in the stub logs to investigate possible causes of failure.

Deploy and test the stub

The stub acts as the backend service that provides the XML data we need to fetch.


  1. Stop both systems

    Make sure you stop both the stub and the main service by selecting each terminal and pressing the keys Ctrl+c. Your view of your terminals should look like:

    06 terminal systems stopped


  2. Deploy the stub

    Make sure your CLI oc client (OpenShift client) points to your Developer Sandbox project (aka namespace):

    oc projects -q | grep dev | xargs oc project
    Note
    The Developer Sandbox only allows 1 project (namespace) per user.

    The command above should output something similar to:

    Now using project "<your-username>-dev" on server "https://172.30.0.1:443".

    Warning
    Not specifying your target project (namespace) in OpenShift may result in a deployment failure.


    You can now copy and paste the following command in your terminal to trigger the deployment:

    mvn oc:deploy -Popenshift -s configuration/settings.xml

    • You’ll see Maven fetching more dependencies and then interact with OpenShift to finalise the deployment of the stub.

      When done, if successful, going back to your browser’s tab with your OpenShift’s developer topology view, you should see the new service up and ready when fully started, looking similar to:

      07 topology stub


  3. Test the stub

    Copy/paste the following cURL command to obtain a response from the stub:

    curl -s \
    -H "content-type: application/xml" \
    -d '' \
    http://end1:8080/camel/subscriber/details \
    | bat -pP -lxml
    Note
    The cURL command above now points to the newly deployed pod, with its service end1 listening on port 8080.
    Note
    The command also includes a pipe to colorise the XML output for better reading.

    The invocation should return an XML payload similar to:

    <Individual>
        <Name>Some</Name>
        <Surname>One</Surname>
        <Address>
            <Number>1</Number>
            <Street>Some Street</Street>
            <City>Somewhere</City>
            <PostCode>SOME C0D3</PostCode>
            <Country>UK</Country>
        </Address>
    </Individual>


Did you obtain the same XML message as shown above?

You’ve successfully tested the stub deployed in the sandbox !!

Inspect in the stub logs to investigate possible causes of failure.

Deploy and test the main service

With the stub already deployed, we just need to deploy the service which will integrate with the stub running under the same namespace.


  1. Deploy the service

    Ensure you run the commands below from the terminal located in the path of your main service project.

    You can now copy and paste the following command in your terminal to trigger the deployment:

    mvn oc:deploy -Popenshift -s configuration/settings.xml

    • Maven will interact with OpenShift to deploy the service.

      When done, if successful, going back to your browser’s tab with your OpenShift’s developer topology view, you should see both services available, the main service and the stub, up and ready when fully started, looking similar to:

      08 topology service stub


  2. Test the service

    Copy/paste the following cURL command to obtain a response from the simple service:

    curl -s \
    -H "content-type: application/json" \
    -d '{"id":"123"}' \
    http://simple:8080/camel/subscriber/details | jq
    Note
    The cURL command above now points to the newly deployed pod’s Kubernetes service simple, listening on port 8080.
    Note
    The command also includes a pipe to parse and colorise the JSON output for better reading.

    The invocation should return a JSON payload similar to:

    {
      "fullName": "Some One",
      "addressLine1": "1 Some Street",
      "addressLine2": "Somewhere SOME C0D3",
      "addressLine3": "UK"
    }


  3. Invoke the service as an external client

    Notice the previous cURL command uses an internal service URL, which is not directly accessible by external consumers. However, the deployment automatically creates a route in OpenShift that exposes the service to external clients.

    You can obtain the route details with the following command and use its URL from your favourite local HTTP client/tester, like Postman, Swagger or others.

    oc get route simple

    Embedding the oc get route command in a cURL allows you to invoke the service as an external consumer.

    • Copy/paste the following cURL to obtain the service OpenAPI definition:

      curl -s http://`oc get route simple -o jsonpath={.spec.host}`/camel/openapi.json | jq

      The invocation should return the OpenApi specification, similar to:

      {
        "openapi": "3.0.2",
        "info": {
          "title": "Subscriber",
          "version": "1.0.0"
        },
      
        ...

    • Copy/paste the following cURL command to simulate an external call and obtain a response from the Camel service:

      curl -s \
      -H "content-type: application/json" \
      -d '{"id":"123"}' \
      http://`oc get route simple -o jsonpath={.spec.host}`/camel/subscriber/details | jq

      The invocation should return a JSON payload similar to:

      {
        "fullName": "Some One",
        "addressLine1": "1 Some Street",
        "addressLine2": "Somewhere SOME C0D3",
        "addressLine3": "UK"
      }


Did you obtain the same JSON response as shown above?

You’ve successfully invoked the simple service as an external client !!

Inspect in the stub logs to investigate possible causes of failure.


Clean up your namespace

When you’re done playing in the Developer Sandbox, you can clean up your Sandbox namespace by un-deploying your Camel simple service and stub end1 using the following Maven oc:undeploy command for both:

mvn oc:undeploy -Popenshift -s configuration/settings.xml

Executing the command above for both services should leave your topology view clean from routes, services, and other Kubernetes artifacts in your namespace.


Is your namespace clean from artifacts?

You’ve successfully cleaned up your namespace !!

Inspect in the logs to investigate possible causes of failure.