Repository: camel Updated Branches: refs/heads/master 86ba6933d -> 87eff0106
CAMEL-10876: service-call eip : add a 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/87eff010 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/87eff010 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/87eff010 Branch: refs/heads/master Commit: 87eff010696acee4e490af0fa60f2f4ba1d0f799 Parents: 86ba693 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Tue Mar 21 17:31:45 2017 +0100 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Wed Mar 22 16:52:09 2017 +0100 ---------------------------------------------------------------------- .../apache/camel/util/function/Suppliers.java | 22 +++ .../consul/cloud/ConsulServiceDiscovery.java | 21 +-- .../README.adoc | 65 +++++++++ .../consumer/pom.xml | 141 +++++++++++++++++++ .../example/ServiceCallConsumerApplication.java | 37 +++++ .../ServiceCallConsumerAutoConfiguration.java | 46 ++++++ .../src/main/resources/application.properties | 16 +++ .../consumer/src/main/resources/logback.xml | 34 +++++ .../pom.xml | 40 ++++++ .../services/pom.xml | 112 +++++++++++++++ .../services/src/main/bash/consul-run.sh | 42 ++++++ .../camel/example/ServiceApplication.java | 72 ++++++++++ .../src/main/resources/application.properties | 8 ++ .../services/src/main/resources/logback.xml | 34 +++++ examples/pom.xml | 1 + 15 files changed, 681 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java b/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java index 88cbe74..b7d874c 100644 --- a/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java +++ b/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java @@ -19,6 +19,7 @@ package org.apache.camel.util.function; import java.util.Objects; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.Consumer; import java.util.function.Supplier; public final class Suppliers { @@ -42,6 +43,27 @@ public final class Suppliers { }; } + public static <T> Supplier<T> memorize(ThrowingSupplier<T, ? extends Exception> supplier, Consumer<Exception> consumer) { + final AtomicReference<T> valueHolder = new AtomicReference<>(); + return () -> { + T supplied = valueHolder.get(); + if (supplied == null) { + synchronized (valueHolder) { + supplied = valueHolder.get(); + if (supplied == null) { + try { + supplied = Objects.requireNonNull(supplier.get(), "Supplier should not return null"); + valueHolder.lazySet(supplied); + } catch (Exception e) { + consumer.accept(e); + } + } + } + } + return supplied; + }; + } + public static <T> Optional<T> firstNotNull(ThrowingSupplier<T, Exception>... suppliers) throws Exception { T answer = null; http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscovery.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscovery.java b/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscovery.java index c8f6b74..c09d211 100644 --- a/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscovery.java +++ b/components/camel-consul/src/main/java/org/apache/camel/component/consul/cloud/ConsulServiceDiscovery.java @@ -19,6 +19,7 @@ package org.apache.camel.component.consul.cloud; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.function.Supplier; import java.util.stream.Collectors; import com.orbitz.consul.Consul; @@ -26,37 +27,37 @@ import com.orbitz.consul.model.catalog.CatalogService; import com.orbitz.consul.model.health.ServiceHealth; import com.orbitz.consul.option.CatalogOptions; import com.orbitz.consul.option.ImmutableCatalogOptions; +import org.apache.camel.RuntimeCamelException; import org.apache.camel.cloud.ServiceDefinition; import org.apache.camel.component.consul.ConsulConfiguration; import org.apache.camel.impl.cloud.DefaultServiceDefinition; import org.apache.camel.impl.cloud.DefaultServiceDiscovery; import org.apache.camel.impl.cloud.DefaultServiceHealth; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.function.Suppliers; + +; public final class ConsulServiceDiscovery extends DefaultServiceDiscovery { - private final Consul client; + private final Supplier<Consul> client; private final CatalogOptions catalogOptions; public ConsulServiceDiscovery(ConsulConfiguration configuration) throws Exception { - this.client = configuration.createConsulClient(); + this.client = Suppliers.memorize(configuration::createConsulClient, e -> { throw new RuntimeCamelException(e); }); ImmutableCatalogOptions.Builder builder = ImmutableCatalogOptions.builder(); - if (ObjectHelper.isNotEmpty(configuration.getDc())) { - builder.datacenter(configuration.getDc()); - } - if (ObjectHelper.isNotEmpty(configuration.getTags())) { - configuration.getTags().forEach(builder::tag); - } + ObjectHelper.ifNotEmpty(configuration.getDc(), builder::datacenter); + ObjectHelper.ifNotEmpty(configuration.getTags(), tags -> tags.forEach(builder::tag)); catalogOptions = builder.build(); } @Override public List<ServiceDefinition> getServices(String name) { - List<CatalogService> services = client.catalogClient() + List<CatalogService> services = client.get().catalogClient() .getService(name, catalogOptions) .getResponse(); - List<ServiceHealth> healths = client.healthClient() + List<ServiceHealth> healths = client.get().healthClient() .getAllServiceInstances(name, catalogOptions) .getResponse(); http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/README.adoc ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/README.adoc b/examples/camel-example-spring-boot-servicecall/README.adoc new file mode 100644 index 0000000..2a0d097 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/README.adoc @@ -0,0 +1,65 @@ +# Spring Boot and ServiceCall EIP Example + +This example show how to use Camel with ServiceCall EIP with spring-boot and consul. + +This example includes two maven modules: + + - services that exposes a number of services + - consumer that consumes services + +## Configuration + +The consumer is configured in the src/main/resources/application.properties in which we blacklist some services for being discovered and we add some additional services not managed by consul + + # Configure service filter + camel.cloud.service-filter.blacklist[service-1] = localhost:9012 + + # Configure additional services + camel.cloud.service-discovery.services[service-2] = localhost:9021,localhost:9022,localhost:9023 + + +## Build + +You can build this example using + + mvn compile + +## Run the example + +Using multiple shells: + + - start consul: + + $ cd services + $ src/main/bash/consul-run.sh + + - start the service-1 service group: + + $ cd services + $ mvn spring-boot:run -Dspring.profiles.active=service-1 + + - start the service-2 service group: + + $ cd services + $ mvn spring-boot:run -Dspring.profiles.active=service-2 + + - start the consumer + + $ cd consumer + $ mvn spring-boot:run + +## Test the example: + +In a new shell: + + $ curl localhost:8080/camel/serviceCall/service1 + Hi!, I'm service-1 on camel-1/route1 + $ curl localhost:8080/camel/serviceCall/service2 + Hi!, I'm service-1 on camel-1/route2 + +If you keep calling the http endpoint you'll notice they are consumed using a round robin policy and that one of the services registered in consul is not taken into account according to the blacklist. + +## 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/87eff010/examples/camel-example-spring-boot-servicecall/consumer/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/consumer/pom.xml b/examples/camel-example-spring-boot-servicecall/consumer/pom.xml new file mode 100644 index 0000000..db8ab42 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/consumer/pom.xml @@ -0,0 +1,141 @@ +<?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</groupId> + <artifactId>camel-example-spring-boot-servicecall</artifactId> + <version>2.19.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-servicecall-consumer</artifactId> + <name>Camel :: Example :: Spring Boot :: ServiceCall :: Consumer</name> + <description>An example showing how to work with Camel ServiceCall EIP and Spring Boot (Consumer)</description> + + <properties> + <category>Beginner</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> + + <!-- Spring Boot --> + <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-actuator</artifactId> + </dependency> + + <!-- Camel --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-consul-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-servlet-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-undertow-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-http-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jackson-starter</artifactId> + </dependency> + + <!-- test --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring</artifactId> + <scope>test</scope> + </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> http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerApplication.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerApplication.java b/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerApplication.java new file mode 100644 index 0000000..61f047c --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerApplication.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 org.apache.camel.example; + +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 ServiceCallConsumerApplication { + + /** + * A main method to start this application. + */ + public static void main(String[] args) { + SpringApplication.run(ServiceCallConsumerApplication.class, args); + } + +} +//CHECKSTYLE:ON http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerAutoConfiguration.java b/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerAutoConfiguration.java new file mode 100644 index 0000000..1d23d67 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/consumer/src/main/java/org/apache/camel/example/ServiceCallConsumerAutoConfiguration.java @@ -0,0 +1,46 @@ +/** + * 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.example; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ServiceCallConsumerAutoConfiguration { + + @Bean + public RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + rest("/serviceCall") + .get("/{serviceId}") + .to("direct:service-call"); + + from("direct:service-call") + .setBody().constant(null) + .to("log:service-call?level=INFO&showAll=true&multiline=true") + .choice() + .when(header("serviceId").isEqualTo("service1")) + .serviceCall("service-1", "undertow:http://service-1") + .when(header("serviceId").isEqualTo("service2")) + .serviceCall("service-2", "undertow:http://service-2"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/application.properties b/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/application.properties new file mode 100644 index 0000000..bfe3682 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/application.properties @@ -0,0 +1,16 @@ +# Spring Boot +endpoints.enabled=false +endpoints.health.enabled=true + +# Camel +camel.springboot.main-run-controller=true +camel.springboot.jmx-enabled=false + +camel.rest.component=servlet +camel.rest.binding-mode=auto + +# Configure service filter +camel.cloud.service-filter.blacklist[service-1] = localhost:9012 + +# Configure additional services +camel.cloud.service-discovery.services[service-2] = localhost:9021,localhost:9022,localhost:9023 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/logback.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/logback.xml b/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/logback.xml new file mode 100644 index 0000000..e2bb6f1 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/consumer/src/main/resources/logback.xml @@ -0,0 +1,34 @@ +<?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. +--> +<configuration> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + </appender> + <appender name="FILE" class="ch.qos.logback.core.FileAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + <file>target/camel-example-spring-boot-servicecall-consumer.log</file> + </appender> + <root level="INFO"> + <!--<appender-ref ref="FILE"/>--> + <appender-ref ref="STDOUT"/> + </root> +</configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/pom.xml b/examples/camel-example-spring-boot-servicecall/pom.xml new file mode 100644 index 0000000..9546468 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/pom.xml @@ -0,0 +1,40 @@ +<?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</groupId> + <artifactId>examples</artifactId> + <version>2.19.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-servicecall</artifactId> + <name>Camel :: Example :: Spring Boot :: ServiceCall</name> + <description>An example showing how to work with Camel ServiceCall EIP and Spring Boot</description> + <packaging>pom</packaging> + + <modules> + <module>consumer</module> + <module>services</module> + </modules> + +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/services/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/services/pom.xml b/examples/camel-example-spring-boot-servicecall/services/pom.xml new file mode 100644 index 0000000..4299bd3 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/services/pom.xml @@ -0,0 +1,112 @@ +<?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</groupId> + <artifactId>camel-example-spring-boot-servicecall</artifactId> + <version>2.19.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-servicecall-services</artifactId> + <name>Camel :: Example :: Spring Boot :: ServiceCall :: Services</name> + <description>An example showing how to work with Camel ServiceCall EIP and Spring Boot (Services)</description> + + <properties> + <category>Beginner</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> + + <!-- Spring Boot --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + + <!-- Camel --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-undertow-starter</artifactId> + </dependency> + + <!-- test --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-test-spring</artifactId> + <scope>test</scope> + </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> http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/services/src/main/bash/consul-run.sh ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/services/src/main/bash/consul-run.sh b/examples/camel-example-spring-boot-servicecall/services/src/main/bash/consul-run.sh new file mode 100755 index 0000000..bb74d96 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/services/src/main/bash/consul-run.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +CONSUL_VER="0.7.5" +CONSUL_ZIP="consul_${CONSUL_VER}_linux_amd64.zip" + +# cleanup +rm -rf "target/consul-data" +rm -rf "target/consul-config" +rm -rf "target/consul" + +mkdir -p target/ +mkdir -p target/consul-data +mkdir -p target/consul-config + +if [ ! -f target/${CONSUL_ZIP} ]; then + wget "https://releases.hashicorp.com/consul/${CONSUL_VER}/${CONSUL_ZIP}" -O target/${CONSUL_ZIP} +fi + +cat > target/consul-config/services.json <<EOF +{ + "services": [{ + "id": "s1i1", "name": "service-1", "tags": ["camel", "service-call"], "address": "localhost", "port": 9011 + }, { + "id": "s1i2", "name": "service-1", "tags": ["camel", "service-call"], "address": "localhost", "port": 9012 + }, { + "id": "s1i3", "name": "service-1", "tags": ["camel", "service-call"], "address": "localhost", "port": 9013 + }] +} +EOF + +unzip -d target target/${CONSUL_ZIP} + +target/consul \ + agent \ + -server \ + -bootstrap \ + -datacenter camel \ + -advertise 127.0.0.1 \ + -bind 0.0.0.0 \ + -log-level trace \ + -data-dir target/consul-data \ + -config-dir target/consul-config \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/services/src/main/java/org/apache/camel/example/ServiceApplication.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/services/src/main/java/org/apache/camel/example/ServiceApplication.java b/examples/camel-example-spring-boot-servicecall/services/src/main/java/org/apache/camel/example/ServiceApplication.java new file mode 100644 index 0000000..9b62fe8 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/services/src/main/java/org/apache/camel/example/ServiceApplication.java @@ -0,0 +1,72 @@ +/** + * 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.example; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +//CHECKSTYLE:OFF +/** + * A sample Spring Boot application that starts the Camel routes. + */ +@SpringBootApplication +public class ServiceApplication { + + @Profile("service-1") + @Component + public class Service1Route extends RouteBuilder { + public void configure() throws Exception { + from("undertow:http://localhost:9011") + .to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true") + .transform().simple("Hi!, I'm {{spring.profiles.active}} on ${camelId}/${routeId}"); + from("undertow:http://localhost:9012") + .to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true") + .transform().simple("Hi!, I'm {{spring.profiles.active}} on ${camelId}/${routeId}"); + from("undertow:http://localhost:9013") + .to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true") + .transform().simple("Hi!, I'm {{spring.profiles.active}} on ${camelId}/${routeId}"); + } + } + + @Profile("service-2") + @Component + public class Service2Route extends RouteBuilder { + public void configure() throws Exception { + from("undertow:http://localhost:9021") + .to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true") + .transform().simple("Hi!, I'm {{spring.profiles.active}} on ${camelId}/${routeId}"); + from("undertow:http://localhost:9022") + .to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true") + .transform().simple("Hi!, I'm {{spring.profiles.active}} on ${camelId}/${routeId}"); + from("undertow:http://localhost:9023") + .to("log:org.apache.camel.example?level=INFO&showAll=true&multiline=true") + .transform().simple("Hi!, I'm {{spring.profiles.active}} on ${camelId}/${routeId}"); + } + } + + /** + * A main method to start this application. + */ + public static void main(String[] args) { + SpringApplication.run(ServiceApplication.class, args); + } + +} +//CHECKSTYLE:ON http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/services/src/main/resources/application.properties ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/services/src/main/resources/application.properties b/examples/camel-example-spring-boot-servicecall/services/src/main/resources/application.properties new file mode 100644 index 0000000..3585779 --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/services/src/main/resources/application.properties @@ -0,0 +1,8 @@ +# Spring Boot +endpoints.enabled=false +endpoints.health.enabled=true + +# Camel +camel.springboot.main-run-controller=true +camel.springboot.jmx-enabled=false + http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/camel-example-spring-boot-servicecall/services/src/main/resources/logback.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-servicecall/services/src/main/resources/logback.xml b/examples/camel-example-spring-boot-servicecall/services/src/main/resources/logback.xml new file mode 100644 index 0000000..39249ae --- /dev/null +++ b/examples/camel-example-spring-boot-servicecall/services/src/main/resources/logback.xml @@ -0,0 +1,34 @@ +<?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. +--> +<configuration> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + </appender> + <appender name="FILE" class="ch.qos.logback.core.FileAppender"> + <encoder> + <pattern>%d{HH:mm:ss.SSS} [%-15.15thread] %-5level %-30.30logger - %msg%n</pattern> + </encoder> + <file>target/camel-example-spring-boot-servicecall-service-1.log</file> + </appender> + <root level="INFO"> + <!--<appender-ref ref="FILE"/>--> + <appender-ref ref="STDOUT"/> + </root> +</configuration> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/87eff010/examples/pom.xml ---------------------------------------------------------------------- diff --git a/examples/pom.xml b/examples/pom.xml index 69c1719..352d04e 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -93,6 +93,7 @@ <module>camel-example-spring-boot-metrics</module> <module>camel-example-spring-boot-rest-jpa</module> <module>camel-example-spring-boot-rest-swagger</module> + <module>camel-example-spring-boot-servicecall</module> <module>camel-example-spring-dm</module> <module>camel-example-spring-javaconfig</module> <module>camel-example-spring-jms</module>