/spring-boot-camel-rest-sql

a quickstart showing how to use Spring Boot, Apache Camel, REST and SQL together with Kubernetes or OpenShift

Primary LanguageJava

Spring-Boot Camel Quickstart using REST / SQL QuickStart

This example demonstrates how to use SQL via JDBC along with Camel’s REST DSL to expose a RESTful API.

This example relies on the Openshift Maven plugin for its build configuration and uses the fabric8 Java base image.

The application utilizes the Spring @ImportResource annotation to load a Camel Context definition via a src/main/resources/spring/camel-context.xml file on the classpath.

Important
This quickstart can run in 2 modes: standalone on your machine and on Kubernetes / OpenShift Cluster. Quickstart requires Java 8 or Java 11 (fuse-java-openshift-jdk11-rhel8 image is used to build in Java 11).

Deployment options

You can run this quickstart in the following modes:

  • Kubernetes / Single-node OpenShift cluster

  • Standalone on your machine

The most effective way to run this quickstart is to deploy and run the project on OpenShift.

For more details about running this quickstart on a single-node OpenShift cluster, CI/CD deployments, as well as the rest of the runtime, see the Spring Boot Runtime Guide.

Running the Quickstart on a single-node Kubernetes/OpenShift cluster

Important
You need to run this example on Container Development Kit 3.3 or OpenShift 3.7. Both of these products have suitable Fuse images pre-installed. If you run it in an environment where those images are not preinstalled follow the steps described in Running the Quickstart on a single-node Kubernetes/OpenShift cluster without preinstalled images.

A single-node Kubernetes/OpenShift cluster provides you with access to a cloud environment that is similar to a production environment.

If you have a single-node Kubernetes/OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, installed and running, you can deploy your quickstart there.

  1. Log in to your OpenShift cluster:

    $ oc login -u developer -p developer
  2. Create a new OpenShift project for the quickstart:

    $ oc new-project MY_PROJECT_NAME
  3. It is assumed that a MySQL service is already running on the platform. You can deploy it using the provided deployment by executing in single-node OpenShift cluster:

    $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mysql-ephemeral-template.json
    $ oc new-app --template=mysql-ephemeral

    More information can be found in using the MySQL database image. You may need to pass MYSQL_RANDOM_ROOT_PASSWORD=true as environment variable to the deployment.

  4. Change the directory to the folder that contains the extracted quickstart application (for example, my_openshift/spring-boot-camel-rest-sql) :

    or

    $ cd my_openshift/spring-boot-camel-rest-sql
  5. Build and deploy the project to the OpenShift cluster:

    $ mvn clean -DskipTests oc:deploy -Dmysql-service-username=<username> -Dmysql-service-password=<password> -Popenshift

    The username and password system properties correspond to the credentials used when deploying the MySQL database service.

  6. In your browser, navigate to the MY_PROJECT_NAME project in the OpenShift console. Wait until you can see that the pod for the spring-boot-camel-rest-sql has started up.

  7. On the project’s Overview page, navigate to the details page deployment of the spring-boot-camel-rest-sql application: https://OPENSHIFT_IP_ADDR:8443/console/project/MY_PROJECT_NAME/browse/pods/spring-boot-camel-rest-sql-NUMBER_OF_DEPLOYMENT?tab=details.

  8. Switch to tab Logs and then see the messages sent by Camel.

Running the Quickstart on a single-node Kubernetes/OpenShift cluster without preinstalled images

A single-node Kubernetes/OpenShift cluster provides you with access to a cloud environment that is similar to a production environment.

If you have a single-node Kubernetes/OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, installed and running, you can deploy your quickstart there.

  1. Log in to your OpenShift cluster:

    $ oc login -u developer -p developer
  2. Create a new OpenShift project for the quickstart:

    $ oc new-project MY_PROJECT_NAME
  3. Configure Red Hat Container Registry authentication (if it is not configured). Follow documentation.

  4. Import base images in your newly created project (MY_PROJECT_NAME):

    $ oc import-image fuse-java-openshift:1.13 --from=registry.redhat.io/fuse7/fuse-java-openshift:1.13 --confirm
  5. It is assumed that a MySQL service is already running on the platform. You can deploy it using the provided deployment by executing in single-node OpenShift cluster:

    $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mysql-ephemeral-template.json
    $ oc new-app --template=mysql-ephemeral -p MYSQL_USER=<username> -p MYSQL_PASSWORD=<password>

    More information can be found in using the MySQL database image. You may need to pass MYSQL_RANDOM_ROOT_PASSWORD=true as environment variable to the deployment.

  6. Change the directory to the folder that contains the extracted quickstart application (for example, my_openshift/spring-boot-camel-rest-sql) :

    or

    $ cd my_openshift/spring-boot-camel-rest-sql
  7. Build and deploy the project to the OpenShift cluster:

    $ mvn clean -DskipTests oc:deploy -Dmysql-service-username=<username> -Dmysql-service-password=<password> -Popenshift  -Djkube.generator.fromMode=istag -Djkube.generator.from=MY_PROJECT_NAME/fuse-java-openshift:1.13

    The username and password system properties correspond to the credentials used when deploying the MySQL database service.

  8. In your browser, navigate to the MY_PROJECT_NAME project in the OpenShift console. Wait until you can see that the pod for the spring-boot-camel-rest-sql has started up.

  9. On the project’s Overview page, navigate to the details page deployment of the spring-boot-camel-rest-sql application: https://OPENSHIFT_IP_ADDR:8443/console/project/MY_PROJECT_NAME/browse/pods/spring-boot-camel-rest-sql-NUMBER_OF_DEPLOYMENT?tab=details.

  10. Switch to tab Logs and then see the messages sent by Camel.

Accessing the REST service

When the example is running, a REST service is available to list the books that can be ordered, and as well the order statuses.

If you run the example on a single-node OpenShift cluster, then the REST service is exposed at 'http://spring-boot-camel-rest-sql-MY_PROJECT_NAME.OPENSHIFT_IP_ADDR.nip.io/camel-rest-sql/`.

Notice: As it depends on your OpenShift setup, the hostname (route) might vary. Verify with oc get routes which hostname is valid for you. Add the -Djkube.deploy.createExternalUrls=true option to your Maven commands if you want it to deploy a Route configuration for the service.

The actual endpoint is using the context-path camel-rest-sql/books and the REST service provides two services:

  • books: to list all the available books that can be ordered,

  • books/order/{id}: to output order status for the given order id.

The example automatically creates new orders with a running order id starting from 1.

You can then access these services from your Web browser, e.g.:

Swagger API

The example provides API documentation of the service using Swagger using the context-path camel-rest-sql/api-doc. You can access the API documentation from your Web browser at http://spring-boot-camel-rest-sql-MY_PROJECT_NAME.OPENSHIFT_IP_ADDR.nip.io/camel-rest-sql/api-doc.

Running the quickstart standalone on your machine

To run this quickstart as a standalone project on your local machine:

  1. Download the project and extract the archive on your local filesystem.

  2. Build the project:

    $ cd PROJECT_DIR
    $ mvn clean package
  3. Run the service:

    $ mvn spring-boot:run

    Alternatively, you can run the application locally using the executable JAR produced:

    $ java -jar -Dspring.profiles.active=dev target/spring-boot-camel-rest-sql-1.0-SNAPSHOT.jar

    This uses an embedded in-memory HSQLDB database. You can use the default Spring Boot profile in case you have a MySQL server available for you to test.

  4. You can then access the REST API directly from your Web browser, e.g.: