This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit 36617bb3d208ca563e167827596139186da67653 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Wed May 23 20:23:40 2018 +0200 CAMEL-12518: camel cloud : leverage spring-cloud ServiceRegistry to register routes --- .../core/xml/AbstractCamelContextFactoryBean.java | 5 +++ .../camel/spring/boot/CamelAutoConfiguration.java | 5 +++ .../cloud/CamelCloudConfigurationProperties.java | 38 ++++++++++++++++++++++ .../consul/CamelCloudConsulAutoConfiguration.java | 8 +++-- .../ServiceDefinitionToConsulRegistration.java | 8 ++++- .../CamelCloudZookeeperAutoConfiguration.java | 8 +++-- .../ServiceDefinitionToZookeeperRegistration.java | 8 +++-- ...pringCloudServiceRegistryAutoConfiguration.java | 3 ++ .../apache/camel/example/ServiceApplication.java | 13 +++++--- .../src/main/resources/application.properties | 3 +- 10 files changed, 85 insertions(+), 14 deletions(-) diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java index de873c8..2d87e03 100644 --- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java +++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java @@ -350,6 +350,11 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex if (serviceRegistries != null && !serviceRegistries.isEmpty()) { for (Map.Entry<String, ServiceRegistry> entry : serviceRegistries.entrySet()) { ServiceRegistry service = entry.getValue(); + + if (service.getId() == null) { + service.setId(getContext().getUuidGenerator().generateUuid()); + } + LOG.info("Using ServiceRegistry with id: {} and implementation: {}", service.getId(), service); getContext().addService(service); } diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java index 9e967dd..95cb392 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java @@ -480,6 +480,11 @@ public class CamelAutoConfiguration { if (serviceRegistries != null && !serviceRegistries.isEmpty()) { for (Map.Entry<String, ServiceRegistry> entry : serviceRegistries.entrySet()) { ServiceRegistry service = entry.getValue(); + + if (service.getId() == null) { + service.setId(camelContext.getUuidGenerator().generateUuid()); + } + LOG.info("Using ServiceRegistry with id: {} and implementation: {}", service.getId(), service); camelContext.addService(service); } diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java index bfbd8b2..753bd77 100644 --- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java @@ -33,6 +33,7 @@ public class CamelCloudConfigurationProperties { private ServiceDiscovery serviceDiscovery = new ServiceDiscovery(); private ServiceFilter serviceFilter = new ServiceFilter(); private ServiceChooser serviceChooser = new ServiceChooser(); + private ServiceRegistry serviceRegistry = new ServiceRegistry(); public boolean isEnabled() { return enabled; @@ -62,6 +63,10 @@ public class CamelCloudConfigurationProperties { return serviceChooser; } + public ServiceRegistry getServiceRegistry() { + return serviceRegistry; + } + // ***************************************** // Service Call // ***************************************** @@ -284,4 +289,37 @@ public class CamelCloudConfigurationProperties { this.enabled = enabled; } } + + // ***************************************** + // Service Registry + // ***************************************** + + public static class ServiceRegistry { + /** + * Configure if service registry should be enabled or not, default true. + */ + private boolean enabled = true; + + /** + * Configure the service listening address. + */ + private String serviceHost; + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + + public String getServiceHost() { + return serviceHost; + } + + public void setServiceHost(String serviceHost) { + this.serviceHost = serviceHost; + } + } } diff --git a/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/CamelCloudConsulAutoConfiguration.java b/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/CamelCloudConsulAutoConfiguration.java index c83fbc2..a1ba31f 100644 --- a/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/CamelCloudConsulAutoConfiguration.java +++ b/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/CamelCloudConsulAutoConfiguration.java @@ -17,9 +17,11 @@ package org.apache.camel.spring.cloud.consul; import org.apache.camel.cloud.ServiceDefinition; +import org.apache.camel.spring.boot.cloud.CamelCloudConfigurationProperties; import org.apache.camel.spring.boot.util.GroupCondition; import org.apache.camel.spring.cloud.CamelSpringCloudServiceRegistryAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.consul.ConditionalOnConsulEnabled; import org.springframework.cloud.consul.serviceregistry.ConsulRegistration; import org.springframework.context.annotation.Bean; @@ -31,11 +33,13 @@ import org.springframework.core.convert.converter.Converter; @AutoConfigureBefore(CamelSpringCloudServiceRegistryAutoConfiguration.class) @ConditionalOnConsulEnabled @Conditional(CamelCloudConsulAutoConfiguration.Condition.class) +@EnableConfigurationProperties(CamelCloudConfigurationProperties.class) public class CamelCloudConsulAutoConfiguration { @Bean(name = "service-definition-to-consul-registration") - public Converter<ServiceDefinition, ConsulRegistration> serviceDefinitionToConsulRegistration() { - return new ServiceDefinitionToConsulRegistration(); + public Converter<ServiceDefinition, ConsulRegistration> serviceDefinitionToConsulRegistration( + CamelCloudConfigurationProperties properties) { + return new ServiceDefinitionToConsulRegistration(properties); } // ******************************* diff --git a/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/ServiceDefinitionToConsulRegistration.java b/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/ServiceDefinitionToConsulRegistration.java index 443fc98..648f8cb 100644 --- a/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/ServiceDefinitionToConsulRegistration.java +++ b/components/camel-spring-cloud-consul/src/main/java/org/apache/camel/spring/cloud/consul/ServiceDefinitionToConsulRegistration.java @@ -21,17 +21,23 @@ import java.util.stream.Collectors; import com.ecwid.consul.v1.agent.model.NewService; import org.apache.camel.cloud.ServiceDefinition; +import org.apache.camel.spring.boot.cloud.CamelCloudConfigurationProperties; import org.springframework.cloud.consul.serviceregistry.ConsulRegistration; import org.springframework.core.convert.converter.Converter; public final class ServiceDefinitionToConsulRegistration implements Converter<ServiceDefinition, ConsulRegistration> { + private final CamelCloudConfigurationProperties properties; + + public ServiceDefinitionToConsulRegistration(CamelCloudConfigurationProperties properties) { + this.properties = properties; + } @Override public ConsulRegistration convert(ServiceDefinition source) { NewService service = new NewService(); service.setName(source.getName()); service.setId(source.getId()); - service.setAddress(source.getHost()); + service.setAddress(properties.getServiceRegistry().getServiceHost()); service.setPort(source.getPort()); service.setTags( diff --git a/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/CamelCloudZookeeperAutoConfiguration.java b/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/CamelCloudZookeeperAutoConfiguration.java index 85ba227..b223e02 100644 --- a/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/CamelCloudZookeeperAutoConfiguration.java +++ b/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/CamelCloudZookeeperAutoConfiguration.java @@ -17,9 +17,11 @@ package org.apache.camel.spring.cloud.zookeeper; import org.apache.camel.cloud.ServiceDefinition; +import org.apache.camel.spring.boot.cloud.CamelCloudConfigurationProperties; import org.apache.camel.spring.boot.util.GroupCondition; import org.apache.camel.spring.cloud.CamelSpringCloudServiceRegistryAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.zookeeper.ConditionalOnZookeeperEnabled; import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration; import org.springframework.context.annotation.Bean; @@ -31,11 +33,13 @@ import org.springframework.core.convert.converter.Converter; @AutoConfigureBefore(CamelSpringCloudServiceRegistryAutoConfiguration.class) @ConditionalOnZookeeperEnabled @Conditional(CamelCloudZookeeperAutoConfiguration.Condition.class) +@EnableConfigurationProperties(CamelCloudConfigurationProperties.class) public class CamelCloudZookeeperAutoConfiguration { @Bean(name = "service-definition-to-zookeeper-registration") - public Converter<ServiceDefinition, ZookeeperRegistration> serviceDefinitionToConsulRegistration() { - return new ServiceDefinitionToZookeeperRegistration(); + public Converter<ServiceDefinition, ZookeeperRegistration> serviceDefinitionToConsulRegistration( + CamelCloudConfigurationProperties properties) { + return new ServiceDefinitionToZookeeperRegistration(properties); } // ******************************* diff --git a/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/ServiceDefinitionToZookeeperRegistration.java b/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/ServiceDefinitionToZookeeperRegistration.java index 30cc93b..0ad2ee1 100644 --- a/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/ServiceDefinitionToZookeeperRegistration.java +++ b/components/camel-spring-cloud-zookeeper/src/main/java/org/apache/camel/spring/cloud/zookeeper/ServiceDefinitionToZookeeperRegistration.java @@ -17,6 +17,7 @@ package org.apache.camel.spring.cloud.zookeeper; import org.apache.camel.cloud.ServiceDefinition; +import org.apache.camel.spring.boot.cloud.CamelCloudConfigurationProperties; import org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties; import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance; import org.springframework.cloud.zookeeper.serviceregistry.ServiceInstanceRegistration; @@ -24,7 +25,10 @@ import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration import org.springframework.core.convert.converter.Converter; public final class ServiceDefinitionToZookeeperRegistration implements Converter<ServiceDefinition, ZookeeperRegistration> { - public ServiceDefinitionToZookeeperRegistration() { + private final CamelCloudConfigurationProperties properties; + + public ServiceDefinitionToZookeeperRegistration(CamelCloudConfigurationProperties properties) { + this.properties = properties; } @Override @@ -36,7 +40,7 @@ public final class ServiceDefinitionToZookeeperRegistration implements Converter ); return ServiceInstanceRegistration.builder() - .address(source.getHost()) + .address(properties.getServiceRegistry().getServiceHost()) .port(source.getPort()) .name(source.getName()) .payload(instance) diff --git a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudServiceRegistryAutoConfiguration.java b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudServiceRegistryAutoConfiguration.java index caffeaa..2d8daba 100644 --- a/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudServiceRegistryAutoConfiguration.java +++ b/components/camel-spring-cloud/src/main/java/org/apache/camel/spring/cloud/CamelSpringCloudServiceRegistryAutoConfiguration.java @@ -20,11 +20,13 @@ import java.util.Collection; import org.apache.camel.cloud.ServiceRegistry; import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.cloud.CamelCloudConfigurationProperties; import org.apache.camel.spring.boot.util.GroupCondition; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; @@ -35,6 +37,7 @@ import org.springframework.context.annotation.Configuration; @AutoConfigureBefore(CamelAutoConfiguration.class) @ConditionalOnBean(org.springframework.cloud.client.serviceregistry.ServiceRegistry.class) @Conditional(CamelSpringCloudServiceRegistryAutoConfiguration.ServiceRegistryCondition.class) +@EnableConfigurationProperties(CamelCloudConfigurationProperties.class) public class CamelSpringCloudServiceRegistryAutoConfiguration { @Bean diff --git a/examples/camel-example-spring-cloud-serviceregistry/service/src/main/java/org/apache/camel/example/ServiceApplication.java b/examples/camel-example-spring-cloud-serviceregistry/service/src/main/java/org/apache/camel/example/ServiceApplication.java index 31ff9c8..869f46d 100644 --- a/examples/camel-example-spring-cloud-serviceregistry/service/src/main/java/org/apache/camel/example/ServiceApplication.java +++ b/examples/camel-example-spring-cloud-serviceregistry/service/src/main/java/org/apache/camel/example/ServiceApplication.java @@ -28,14 +28,17 @@ import org.springframework.util.SocketUtils; */ @SpringBootApplication public class ServiceApplication { - + @Component public class Services extends RouteBuilder { public void configure() throws Exception { - fromF("service:my-service:undertow:http://localhost:%d/path/to/service/1", SocketUtils.findAvailableTcpPort()) - .transform().simple("Hi!, I'm service-1 on path: /path/to/service/1"); - fromF("service:my-service:undertow:http://localhost:%d/path/to/service/2", SocketUtils.findAvailableTcpPort()) - .transform().simple("Hi!, I'm service-1 on path: /path/to/service/2"); + // TODO: service.host should be set using properties + fromF("service:my-service:undertow:http://localhost:%d/path/to/service/1?service.host=localhost", SocketUtils.findAvailableTcpPort()) + .transform().simple("Hi!, I'm service-1 on path: /path/to/service/1"); + + // TODO: service.host should be set using properties + fromF("service:my-service:undertow:http://localhost:%d/path/to/service/2?service.host=localhost", SocketUtils.findAvailableTcpPort()) + .transform().simple("Hi!, I'm service-1 on path: /path/to/service/2"); } } diff --git a/examples/camel-example-spring-cloud-serviceregistry/service/src/main/resources/application.properties b/examples/camel-example-spring-cloud-serviceregistry/service/src/main/resources/application.properties index b0e9629..8ea1736 100644 --- a/examples/camel-example-spring-cloud-serviceregistry/service/src/main/resources/application.properties +++ b/examples/camel-example-spring-cloud-serviceregistry/service/src/main/resources/application.properties @@ -29,5 +29,4 @@ spring.cloud.service-registry.auto-registration.enabled = false # Camel camel.springboot.main-run-controller = true -camel.springboot.jmx-enabled = false - +camel.springboot.jmx-enabled = false \ No newline at end of file -- To stop receiving notification emails like this one, please contact lburgazz...@apache.org.