You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/10/15 17:47:02 UTC

[camel-spring-boot-examples] branch master updated: CAMEL-15323: Add example camel-spring-boot - http endpoint for route information (#16)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot-examples.git


The following commit(s) were added to refs/heads/master by this push:
     new 36e0685  CAMEL-15323: Add example camel-spring-boot - http endpoint for route information (#16)
36e0685 is described below

commit 36e0685d38df77ce52632e0ef7551c76ec139f0a
Author: José Ráez Rodríguez <jo...@gmail.com>
AuthorDate: Thu Oct 15 19:45:56 2020 +0200

    CAMEL-15323: Add example camel-spring-boot - http endpoint for route information (#16)
    
    * Built routes for mappings and metrics
    
    * Added example for shutdown route
    
    * Added readme.adoc
    
    * Updated root's README.adoc with the new example
    
    * Removed excess lines
    
    * Refactorized doOnce function to explicit timer definition for consistency
    
    * Added newline for consistency on route definition (after unmarshal())
    
    * Extended explanation on the mappings section to explain that that timer consumer will only fire once
    
    * Added POM's properties and module to parent for Readme for the new example to be correctlñy generated
    
    * Changed dependencies to user camel-spring-boot's starter versions
    
    * Removed unused imports
    
    * Removed jetty from the restConfiguration. We are using the http component, not jetty, after all
    
    * Changed the category to Management and Monitoring
    
    * Added prettypring and multiline=true option to LOG info for nicer printing of the received data
---
 README.adoc                                        |  18 ++--
 .../pom.xml                                        | 103 +++++++++++++++++++++
 .../readme.adoc                                    |  44 +++++++++
 .../src/main/java/sample/camel/Application.java    |  37 ++++++++
 .../src/main/java/sample/camel/MyRouteBuilder.java |  57 ++++++++++++
 .../src/main/resources/application.properties      |  39 ++++++++
 pom.xml                                            |   1 +
 7 files changed, 291 insertions(+), 8 deletions(-)

diff --git a/README.adoc b/README.adoc
index 9c18658..f1c6c06 100644
--- a/README.adoc
+++ b/README.adoc
@@ -27,13 +27,13 @@ readme's instructions.
 == Examples
 
 // examples: START
-Number of Examples: 43 (0 deprecated)
+Number of Examples: 44 (0 deprecated)
 
 [width="100%",cols="4,2,4",options="header"]
 |===
 | Example | Category | Description
 
-| link:camel-example-spring-boot-health-checks/readme.adoc[Spring Boot Health Checks] (camel-example-spring-boot-health-checks) |  |
+| link:camel-example-spring-boot-health-checks/readme.adoc[Spring Boot Health Checks] (camel-example-spring-boot-health-checks) |  | 
 
 | link:camel-example-spring-boot-undertow-spring-security/readme.adoc[Spring Boot Undertow Spring Security] (camel-example-spring-boot-undertow-spring-security) | Advanced | Example on how to use the Camel Undertow component with spring security and Keycloak
 
@@ -41,6 +41,8 @@ Number of Examples: 43 (0 deprecated)
 
 | link:camel-example-spring-boot/readme.adoc[Spring Boot] (camel-example-spring-boot) | Beginner | An example showing how to work with Camel and Spring Boot
 
+| link:camel-example-spring-boot-actuator-http-metrics/readme.adoc[Spring Boot Actuator Http Metrics] (camel-example-spring-boot-actuator-http-metrics) | Beginner | Example on how to use Spring Boot's Actuator endpoints to gather info like mappings or metrics
+
 | link:camel-example-spring-boot-jira/README.adoc[Spring Boot Jira] (camel-example-spring-boot-jira) | Beginner | An example that uses Jira Camel API
 
 | link:camel-example-spring-boot-pojo/README.adoc[Spring Boot Pojo] (camel-example-spring-boot-pojo) | Beginner | An example showing how to work with Camel POJO routing with Spring Boot
@@ -72,8 +74,8 @@ Number of Examples: 43 (0 deprecated)
 | link:camel-example-spring-boot-arangodb/README.adoc[Spring Boot Arangodb] (camel-example-spring-boot-arangodb) | Database | An example showing the Camel ArangoDb component with Spring Boot
 
 | link:camel-example-spring-boot-rest-jpa/README.adoc[Spring Boot REST JPA] (camel-example-spring-boot-rest-jpa) | Database | An example demonstrating how to use Camel REST DSL with JPA to expose a RESTful API that performs CRUD
-operations on a database
-
+        operations on a database
+    
 
 | link:camel-example-spring-boot-hystrix/README.adoc[Spring Boot Hystrix] (camel-example-spring-boot-hystrix) | EIP | An example showing how to use Hystrix EIP as circuit breaker in Camel routes
 
@@ -82,17 +84,17 @@ operations on a database
 | link:camel-example-spring-boot-fhir/readme.adoc[Spring Boot Fhir] (camel-example-spring-boot-fhir) | Health Care | An example showing how to work with Camel, FHIR and Spring Boot
 
 | link:camel-example-spring-boot-fhir-auth-tx/readme.adoc[Spring Boot Fhir Auth Tx] (camel-example-spring-boot-fhir-auth-tx) | Health Care | An example showing how to work with Camel, FHIR Authorization, FHIR Transaction and Spring Boot
-
+    
 
 | link:camel-example-spring-boot-validator/readme.adoc[Validator Spring Boot] (camel-example-spring-boot-validator) | Input/Output Type Contract | An example showing how to work with declarative validation and Spring Boot
 
 | link:camel-example-spring-boot-apm-opentracing/README.adoc[OpenTracing APM] (camel-example-spring-boot-apm-opentracing) | Management and Monitoring | An example showing how to trace incoming and outgoing messages from Camel with OpenTracing with ElastiCo APM
-
+    
 
 | link:camel-example-spring-boot-metrics/README.adoc[Spring Boot Metrics] (camel-example-spring-boot-metrics) | Management and Monitoring | An example showing how to work with Camel and Spring Boot and report metrics to Graphite
 
 | link:camel-example-spring-boot-opentracing/README.adoc[OpenTracing] (camel-example-spring-boot-opentracing) | Management and Monitoring | An example showing how to trace incoming and outgoing messages from Camel with OpenTracing
-
+    
 
 | link:camel-example-spring-boot-supervising-route-controller/readme.adoc[Spring Boot Supervising Route Controller] (camel-example-spring-boot-supervising-route-controller) | Management and Monitoring | An example showing how to work with Camel's Supervising Route Controller and Spring Boot
 
@@ -113,7 +115,7 @@ operations on a database
 | link:camel-example-spring-boot-widget-gadget/readme.md[Spring Boot Widget Gadget] (camel-example-spring-boot-widget-gadget) | Messaging | The widget and gadget example from EIP book, running on Spring Boot
 
 | link:camel-example-spring-boot-reactive-streams/readme.adoc[Spring Boot Reactive Streams] (camel-example-spring-boot-reactive-streams) | Reactive | An example that shows how Camel can exchange data using reactive streams with Spring Boot reactor
-
+    
 
 | link:camel-example-spring-boot-geocoder/README.adoc[Spring Boot Geocoder] (camel-example-spring-boot-geocoder) | Rest | An example showing the Camel Geocoder component via REST DSL with Spring Boot
 
diff --git a/camel-example-spring-boot-actuator-http-metrics/pom.xml b/camel-example-spring-boot-actuator-http-metrics/pom.xml
new file mode 100644
index 0000000..1c14d09
--- /dev/null
+++ b/camel-example-spring-boot-actuator-http-metrics/pom.xml
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.apache.camel.springboot.example</groupId>
+        <artifactId>examples</artifactId>
+        <version>3.6.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-spring-boot-actuator-http-metrics</artifactId>
+    <name>Camel SB Examples :: Actuator HTTP Metrics</name>
+    <description>Example on how to use Spring Boot's Actuator endpoints to gather info like mappings or metrics</description>
+
+    <properties>
+        <category>Management and Monitoring</category>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+        <spring.boot-version>${spring-boot-version}</spring.boot-version>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- Spring Boot BOM -->
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring.boot-version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+            <!-- Camel BOM -->
+            <dependency>
+                <groupId>org.apache.camel.springboot</groupId>
+                <artifactId>camel-spring-boot-dependencies</artifactId>
+                <version>${project.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+        <!-- Spring Boot -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+        </dependency>
+        <!-- Camel -->
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-spring-boot-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-http-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel.springboot</groupId>
+            <artifactId>camel-jackson-starter</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>${spring-boot-version}</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>repackage</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/camel-example-spring-boot-actuator-http-metrics/readme.adoc b/camel-example-spring-boot-actuator-http-metrics/readme.adoc
new file mode 100644
index 0000000..b89c857
--- /dev/null
+++ b/camel-example-spring-boot-actuator-http-metrics/readme.adoc
@@ -0,0 +1,44 @@
+== Camel with Actuator - Mappings, Metrics and Shutdown example
+
+This example shows how you can query some of Spring Boot Actuator options from your
+Camel routes.
+
+=== How to run
+
+[source,console]
+----
+mvn compile spring-boot:run
+----
+
+=== Actuator routes
+
+You need to enable the HTTP routes on Actuator to be able to use them. For example,
+in this example we have to expose the metrics, mappings and shutdown routes.
+
+[source, properties]
+----
+management.endpoints.web.exposure.include=mappings,metrics,shutdown
+----
+
+You can see other functionalities and more details in
+https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-endpoints-enabling-endpoints[Actuator's documentation].
+
+=== Shutdown
+
+Shutdown should be handled with care for security reasons. You will need to explicitly enable
+that endpoint for it to be usable.
+
+[source, properties]
+----
+management.endpoint.shutdown.enabled=true
+----
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/support.html[let us know].
+
+We also love contributors, so
+https://camel.apache.org/contributing.html[get involved] :-)
+
+The Camel riders!
diff --git a/camel-example-spring-boot-actuator-http-metrics/src/main/java/sample/camel/Application.java b/camel-example-spring-boot-actuator-http-metrics/src/main/java/sample/camel/Application.java
new file mode 100644
index 0000000..32322d9
--- /dev/null
+++ b/camel-example-spring-boot-actuator-http-metrics/src/main/java/sample/camel/Application.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sample.camel;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+//CHECKSTYLE:OFF
+
+/**
+ * A sample Spring Boot application that starts the Camel routes.
+ */
+@SpringBootApplication
+public class Application {
+
+    /**
+     * A main method to start this application.
+     */
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class, args);
+    }
+}
+//CHECKSTYLE:ON
diff --git a/camel-example-spring-boot-actuator-http-metrics/src/main/java/sample/camel/MyRouteBuilder.java b/camel-example-spring-boot-actuator-http-metrics/src/main/java/sample/camel/MyRouteBuilder.java
new file mode 100644
index 0000000..adb47e8
--- /dev/null
+++ b/camel-example-spring-boot-actuator-http-metrics/src/main/java/sample/camel/MyRouteBuilder.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package sample.camel;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.model.rest.RestBindingMode;
+import org.springframework.stereotype.Component;
+
+@Component
+public class MyRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+
+        // First, we have to configure our jetty component, which will be the rest
+        // in charge of querying the REST endpoints from actuator
+        restConfiguration()
+                .host("0.0.0.0")
+                .port(8080)
+                .bindingMode(RestBindingMode.json);
+
+        // First, let's show the routes we have exposed. Let's create a timer
+        // consumer that will only fire once and show us the exposed mappings
+        from("timer:queryTimer?repeatCount=1")
+                .to("rest:get:/actuator/mappings")
+                .unmarshal()
+                .json(true)
+                .to("log:INFO?multiline=true");
+
+        // Then, we will be querying the cpu consumption periodically. For more options, you can check
+        // https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html#production-ready-metrics-endpoint
+        from("timer:metricsTimer?period={{metricsPeriod}}")
+                .to("rest:get:/actuator/metrics/system.cpu.usage")
+                .unmarshal()
+                .json(true)
+                .to("log:INFO?multiline=true");
+
+        // Finally, let's see how to shutdown our application using the actuator endpoint
+        from("timer:shutdownTimer?delay={{shutdownTime}}&repeatCount=1")
+                .log("Shutting down")
+                .to("rest:post:/actuator/shutdown");
+    }
+}
diff --git a/camel-example-spring-boot-actuator-http-metrics/src/main/resources/application.properties b/camel-example-spring-boot-actuator-http-metrics/src/main/resources/application.properties
new file mode 100644
index 0000000..b917daa
--- /dev/null
+++ b/camel-example-spring-boot-actuator-http-metrics/src/main/resources/application.properties
@@ -0,0 +1,39 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+##      http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+
+debug = false
+
+logging.level.org.springframework = INFO
+logging.level.org.apache.camel.spring.boot = INFO
+logging.level.org.apache.camel.health = DEBUG
+logging.level.org.apache.camel.impl.health = DEBUG
+logging.level.sample.camel = DEBUG
+
+# enable the shutdown endpoint
+management.endpoint.shutdown.enabled=true
+
+# expose actuator endpoint via HTTP
+management.endpoints.web.exposure.include=mappings,metrics,shutdown
+
+camel.springboot.name = MyCamel
+
+# properties used in the route
+metricsPeriod = 2s
+shutdownTime = 10s
+
+# enable supervised route controller which will startup routes in safe manner
+camel.springboot.route-controller-supervise-enabled = true
diff --git a/pom.xml b/pom.xml
index 45cdff1..2829ae7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -36,6 +36,7 @@
     <modules>
         <module>camel-example-spring-boot</module>
         <module>camel-example-spring-boot-activemq</module>
+        <module>camel-example-spring-boot-actuator-http-metrics</module>
         <module>camel-example-spring-boot-amqp</module>
         <module>camel-example-spring-boot-apm-opentracing</module>
         <module>camel-example-spring-boot-arangodb</module>