CAMEL-10320: Camel Master component for clustered services (spring-boot example)
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ccd30cd2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ccd30cd2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ccd30cd2 Branch: refs/heads/master Commit: ccd30cd252bc8c8e0a4d4aa20f7176ae59b4fc66 Parents: 6b14425 Author: lburgazzoli <[email protected]> Authored: Wed Sep 27 14:39:18 2017 +0200 Committer: lburgazzoli <[email protected]> Committed: Wed Sep 27 16:17:02 2017 +0200 ---------------------------------------------------------------------- .../camel-example-spring-boot-master/pom.xml | 131 +++++++++++++++++++ .../readme.adoc | 21 +++ .../camel/examples/master/MasterNode.java | 36 +++++ .../master/MasterNodeConfiguration.java | 68 ++++++++++ .../src/main/resources/application.properties | 42 ++++++ examples/pom.xml | 1 + 6 files changed, 299 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ccd30cd2/examples/camel-example-spring-boot-master/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-master/pom.xml b/examples/camel-example-spring-boot-master/pom.xml new file mode 100644 index 0000000..ba9dd80 --- /dev/null +++ b/examples/camel-example-spring-boot-master/pom.xml @@ -0,0 +1,131 @@ +<?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.example</groupId> + <artifactId>examples</artifactId> + <version>2.20.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-master</artifactId> + <name>Camel :: Example :: Spring Boot :: Master</name> + <description>An example showing how to work with Camel's Master component and Spring Boot</description> + <packaging>jar</packaging> + + <properties> + <category>Clustering</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</groupId> + <artifactId>camel-spring-boot-dependencies</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-undertow</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-actuator</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-master-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> + + <profiles> + <profile> + <id>jdk9-build</id> + <activation> + <jdk>9</jdk> + </activation> + <build> + <plugins> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <argLine>--add-modules java.xml.bind --add-opens java.base/java.lang=ALL-UNNAMED</argLine> + </configuration> + </plugin> + </plugins> + </build> + </profile> + </profiles> +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/ccd30cd2/examples/camel-example-spring-boot-master/readme.adoc ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-master/readme.adoc b/examples/camel-example-spring-boot-master/readme.adoc new file mode 100644 index 0000000..dddb054 --- /dev/null +++ b/examples/camel-example-spring-boot-master/readme.adoc @@ -0,0 +1,21 @@ +# Camel Clustered Route Controller Example Spring Boot + +This example shows how to work with a simple Apache Camel application using Spring Boot and a Master component. + +## How to run + +1. build the project: ++ + mvn clean package + +2. in a separate shell, run the first camel node ++ + mvn spring-boot:run + +3. in a separate shell, run the second camel node ++ + mvn spring-boot:run + +## More information + +You can find more information about Apache Camel at the website: http://camel.apache.org/ http://git-wip-us.apache.org/repos/asf/camel/blob/ccd30cd2/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNode.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNode.java b/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNode.java new file mode 100644 index 0000000..57c8a35 --- /dev/null +++ b/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNode.java @@ -0,0 +1,36 @@ +/** + * 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 org.apache.camel.examples.master; + +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 MasterNode { + + /** + * A main method to start this application. + */ + public static void main(String[] args) { + SpringApplication.run(MasterNode.class, args); + } +} +//CHECKSTYLE:ON http://git-wip-us.apache.org/repos/asf/camel/blob/ccd30cd2/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNodeConfiguration.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNodeConfiguration.java b/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNodeConfiguration.java new file mode 100644 index 0000000..6d18bc9 --- /dev/null +++ b/examples/camel-example-spring-boot-master/src/main/java/org/apache/camel/examples/master/MasterNodeConfiguration.java @@ -0,0 +1,68 @@ +/** + * 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 org.apache.camel.examples.master; + +import java.io.IOException; +import java.net.ServerSocket; + +import org.apache.camel.builder.RouteBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MasterNodeConfiguration { + private static final Logger LOGGER = LoggerFactory.getLogger(MasterNodeConfiguration.class); + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // This route is configured to be local (see application.properties) + // so it will be started regardless of the leadership status if + // this node. + from("timer:heartbeat?period=10s") + .routeId("heartbeat") + .log("HeartBeat route (timer) {{node.id}} ..."); + + // This route is configured to be clustered so it will be started + // by the controller only when this node is leader + from("master:{{node.namespace}}:timer:clustered?period=5s") + .routeId("clustered") + .log("Clustered route (timer) {{node.id}} ..."); + } + }; + } + + /** + * A small hack to find out the a free port so you can run multiple instances + * of the example without having to manually set the property: server.port + */ + @Bean + public EmbeddedServletContainerCustomizer containerCustomizer() { + return container -> { + try (ServerSocket socket = new ServerSocket(0)) { + LOGGER.debug("server.port: {}", socket.getLocalPort()); + container.setPort(socket.getLocalPort()); + } catch (IOException ignored) { + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/ccd30cd2/examples/camel-example-spring-boot-master/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-master/src/main/resources/application.properties b/examples/camel-example-spring-boot-master/src/main/resources/application.properties new file mode 100644 index 0000000..d0bc3bf --- /dev/null +++ b/examples/camel-example-spring-boot-master/src/main/resources/application.properties @@ -0,0 +1,42 @@ +## --------------------------------------------------------------------------- +## 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.io.atomix = DEBUG +logging.level.org.apache.camel.ha = DEBUG +logging.level.org.apache.camel.impl.ha = DEBUG +logging.level.org.apache.camel.component.atomix = DEBUG +logging.level.org.apache.camel.component.master = DEBUG +logging.level.org.apache.camel.examples.cluster = DEBUG + +endpoints.enabled = false +endpoints.jmx.enabled = false +endpoints.health.enabled = true + +management.port = -1 + +node.id = ${random.uuid} +node.namespace = camel-master + +camel.springboot.name = SampleClusteredRouteController +camel.springboot.jmx-enabled = false + +camel.component.file.cluster.service.enabled = true +camel.component.file.cluster.service.id = ${node.id} +camel.component.file.cluster.service.root = ${java.io.tmpdir} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/ccd30cd2/examples/pom.xml ---------------------------------------------------------------------- diff --git a/examples/pom.xml b/examples/pom.xml index 18cedb0..6a67f6c 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -101,6 +101,7 @@ <module>camel-example-spring-boot-health-checks</module> <module>camel-example-spring-boot-infinispan</module> <module>camel-example-spring-boot-live-reload</module> + <module>camel-example-spring-boot-master</module> <module>camel-example-spring-boot-metrics</module> <module>camel-example-spring-boot-pojo</module> <module>camel-example-spring-boot-rest-jpa</module>
