Repository: camel Updated Branches: refs/heads/master dd6f9ba60 -> fd0db5611
CAMEL-10919: auto-bind hystrix servlet in spring-web Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fd0db561 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fd0db561 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fd0db561 Branch: refs/heads/master Commit: fd0db561137613b62bf27dd4834d7a4c37466c7c Parents: 239a6b0 Author: Nicola Ferraro <ni.ferr...@gmail.com> Authored: Thu Mar 2 13:54:34 2017 +0100 Committer: Nicola Ferraro <ni.ferr...@gmail.com> Committed: Thu Mar 2 13:55:00 2017 +0100 ---------------------------------------------------------------------- .../camel-hystrix-starter/pom.xml | 6 ++ .../HystrixMappingAutoConfiguration.java | 50 +++++++++++ .../springboot/HystrixMappingConfiguration.java | 68 ++++++++++++++ .../main/resources/META-INF/spring.factories | 19 ++++ .../HystrixMappingAutoConfigurationTest.java | 93 ++++++++++++++++++++ .../test/HystrixMappingDisablingTest.java | 52 +++++++++++ 6 files changed, 288 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fd0db561/platforms/spring-boot/components-starter/camel-hystrix-starter/pom.xml ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/pom.xml b/platforms/spring-boot/components-starter/camel-hystrix-starter/pom.xml index 8d47976..e9fdeba 100644 --- a/platforms/spring-boot/components-starter/camel-hystrix-starter/pom.xml +++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/pom.xml @@ -55,5 +55,11 @@ <artifactId>camel-spring-boot-starter</artifactId> </dependency> <!--END OF GENERATED CODE--> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + <version>${spring-boot-version}</version> + <optional>true</optional> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/camel/blob/fd0db561/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingAutoConfiguration.java new file mode 100644 index 0000000..2f6ac64 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingAutoConfiguration.java @@ -0,0 +1,50 @@ +/** + * 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.component.hystrix.springboot; + +import org.apache.camel.component.hystrix.metrics.servlet.HystrixEventStreamServlet; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Servlet mapping auto-configuration. + */ +@Configuration +@ConditionalOnProperty(name = "camel.component.hystrix.mapping.enabled", matchIfMissing = true) +@ConditionalOnBean(type = "org.apache.camel.spring.boot.CamelAutoConfiguration") +@AutoConfigureAfter(name = "org.apache.camel.spring.boot.CamelAutoConfiguration") +@ConditionalOnWebApplication +@EnableConfigurationProperties(HystrixMappingConfiguration.class) +public class HystrixMappingAutoConfiguration { + + @Bean + ServletRegistrationBean servletRegistrationBean(HystrixMappingConfiguration config) { + ServletRegistrationBean mapping = new ServletRegistrationBean(); + mapping.setServlet(new HystrixEventStreamServlet()); + mapping.addUrlMappings(config.getPath()); + mapping.setName(config.getServletName()); + + return mapping; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/fd0db561/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingConfiguration.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingConfiguration.java new file mode 100644 index 0000000..07f959b --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/java/org/apache/camel/component/hystrix/springboot/HystrixMappingConfiguration.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.component.hystrix.springboot; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Mapping settings for the hystrix component. + */ +@ConfigurationProperties(prefix = "camel.component.hystrix.mapping") +public class HystrixMappingConfiguration { + + /** + * Endpoint for hystrix metrics servlet. + */ + private String path = "/hystrix.stream"; + + /** + * Name of the Hystrix metrics servlet. + */ + private String servletName = "HystrixEventStreamServlet"; + + /** + * Enables the automatic mapping of the hystrics metric servlet into the Spring web context. + */ + private Boolean enabled = true; + + public HystrixMappingConfiguration() { + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getServletName() { + return servletName; + } + + public void setServletName(String servletName) { + this.servletName = servletName; + } + + public Boolean getEnabled() { + return enabled; + } + + public void setEnabled(Boolean enabled) { + this.enabled = enabled; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/fd0db561/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/resources/META-INF/spring.factories ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/resources/META-INF/spring.factories b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..331a5e4 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,19 @@ +# +# 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. +# + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.apache.camel.component.hystrix.springboot.HystrixMappingAutoConfiguration http://git-wip-us.apache.org/repos/asf/camel/blob/fd0db561/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingAutoConfigurationTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingAutoConfigurationTest.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingAutoConfigurationTest.java new file mode 100644 index 0000000..c13a15b --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingAutoConfigurationTest.java @@ -0,0 +1,93 @@ +/** + * 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.component.hystrix.springboot.test; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertTrue; + +/** + * Testing the hystrix mapping + */ +@RunWith(SpringRunner.class) +@SpringBootApplication +@DirtiesContext +@ContextConfiguration(classes = HystrixMappingAutoConfigurationTest.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class HystrixMappingAutoConfigurationTest { + + private static final Logger LOG = LoggerFactory.getLogger(HystrixMappingAutoConfigurationTest.class); + + @LocalServerPort + private int serverPort; + + @Test + public void testHystrixServletMapping() throws Exception { + + CountDownLatch pingFound = new CountDownLatch(1); + Consumer<String> consumer = s -> { + if (s != null && s.toLowerCase().contains("ping")) { + pingFound.countDown(); + } + }; + + URL hystrix = new URL("http://localhost:" + serverPort + "/hystrix.stream"); + try (InputStream stream = consume(hystrix, consumer)) { + // Hystrix stream is infinite, we stop reading it after the first ping (or timeout) + + assertTrue(pingFound.await(5, TimeUnit.SECONDS)); + } + + } + + private InputStream consume(URL url, final Consumer<String> consumer) throws IOException { + final InputStream stream = url.openConnection().getInputStream(); + new Thread() { + @Override + public void run() { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { + String line; + while ((line = reader.readLine()) != null) { + consumer.accept(line); + } + } catch (IOException ex) { + LOG.error("Consumer thread is terminating", ex); + } + } + }.start(); + return stream; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/fd0db561/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingDisablingTest.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingDisablingTest.java b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingDisablingTest.java new file mode 100644 index 0000000..6fbdad8 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-hystrix-starter/src/test/java/org/apache/camel/component/hystrix/springboot/test/HystrixMappingDisablingTest.java @@ -0,0 +1,52 @@ +/** + * 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.component.hystrix.springboot.test; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * Testing that the hystrix mapping can be disabled. + */ +@RunWith(SpringRunner.class) +@SpringBootApplication +@DirtiesContext +@ContextConfiguration(classes = HystrixMappingDisablingTest.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { + "camel.component.hystrix.mapping.enabled=false" +}) +public class HystrixMappingDisablingTest { + + @Autowired + private TestRestTemplate restTemplate; + + + @Test + public void testHystrixServletMapping() { + Assert.assertEquals(404, restTemplate.getForEntity("/hystrix.stream", String.class).getStatusCodeValue()); + } + +} +