[camel-spring-boot] branch master updated: CAMEL-16448: Added configuration for service definitions with metadata (#313)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[camel-spring-boot] branch master updated: CAMEL-16448: Added configuration for service definitions with metadata (#313)

davsclaus-2
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.git


The following commit(s) were added to refs/heads/master by this push:
     new be5a2d1  CAMEL-16448: Added configuration for service definitions with metadata (#313)
be5a2d1 is described below

commit be5a2d1eda9cdb736915c4263eed550b9a87ef33
Author: Dietrich Schulten <[hidden email]>
AuthorDate: Wed Apr 7 20:22:02 2021 +0200

    CAMEL-16448: Added configuration for service definitions with metadata (#313)
---
 .../src/main/docs/spring-boot.adoc                 |  5 +-
 .../cloud/CamelCloudConfigurationProperties.java   | 64 ++++++++++++++++++++-
 ...amelCloudServiceDiscoveryAutoConfiguration.java | 14 +++++
 .../CamelCloudServiceCallConfigurationTest.java    | 65 +++++++++++++++++++++-
 4 files changed, 142 insertions(+), 6 deletions(-)

diff --git a/core/camel-spring-boot/src/main/docs/spring-boot.adoc b/core/camel-spring-boot/src/main/docs/spring-boot.adoc
index 586a815..c9ead0d 100644
--- a/core/camel-spring-boot/src/main/docs/spring-boot.adoc
+++ b/core/camel-spring-boot/src/main/docs/spring-boot.adoc
@@ -111,9 +111,10 @@ The component supports 170 options, which are listed below.
 | *camel.cloud.service-call.service-filter* | A reference to the org.apache.camel.cloud.ServiceFilter to use. |  | String
 | *camel.cloud.service-call.uri* | The uri of the endpoint to send to. The uri can be dynamic computed using the simple language expression. |  | String
 | *camel.cloud.service-chooser.enabled* | Global option to enable/disable Camel cloud service chooser, default is true. | true | Boolean
-| *camel.cloud.service-discovery.configurations* | Configure the service discovery rules. |  | Map
+| *camel.cloud.service-discovery.configurations* | Configure additional static service discoveries using a map of custom service discovery names, each associated with a map of service names associated with `org.apache.camel.spring.boot.cloud.CamelCloudConfigurationProperties.ServiceDiscovery` objects. |  | Map
 | *camel.cloud.service-discovery.enabled* | Global option to enable/disable Camel cloud service discovery, default is true. | true | Boolean
-| *camel.cloud.service-discovery.services* | Configure service discoveries. |  | Map
+| *camel.cloud.service-discovery.services* | Configure static service discovery using a map of service names associated with simple host:port Strings. |  | Map
+| *camel.cloud.service-discovery.service-definitions* | Configure static service discovery using a map of service names associated with `org.apache.camel.spring.boot.cloud.CamelCloudConfigurationProperties.ServiceDefinitionConfiguration` objects. |  | Map
 | *camel.cloud.service-filter.blacklist* | Configure service filter blacklists. |  | Map
 | *camel.cloud.service-filter.configurations* | Configure the service filtering rules. |  | Map
 | *camel.cloud.service-filter.enabled* | Global option to enable/disable Camel cloud service filter, default is true. | true | Boolean
diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java
index 133facb..9d967f8 100644
--- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudConfigurationProperties.java
@@ -218,14 +218,22 @@ public class CamelCloudConfigurationProperties {
 
     public static class ServiceDiscoveryConfiguration {
         /**
-         * Configure service discoveries.
+         * Configure static service discovery using simple host:port strings.
          */
         private Map<String, List<String>> services = new HashMap<>();
 
+        /**
+         * Configure static service discovery with distinct id, host, port, and metadata properties.
+         */
+        private Map<String, List<ServiceDefinitionConfiguration>> serviceDefinitions = new HashMap<>();
 
         public Map<String, List<String>> getServices() {
             return services;
         }
+
+        public Map<String, List<ServiceDefinitionConfiguration>> getServiceDefinitions() {
+            return serviceDefinitions;
+        }
     }
 
     public static class ServiceDiscovery extends ServiceDiscoveryConfiguration {
@@ -251,6 +259,60 @@ public class CamelCloudConfigurationProperties {
         }
     }
 
+    public static class ServiceDefinitionConfiguration {
+        /**
+         * Service definition id.
+         */
+        private String id;
+
+        /**
+         * Service host.
+         */
+        private String host;
+
+        /**
+         * Service port.
+         */
+        private int port;
+
+        /**
+         * Service metadata to register with the service.
+         */
+        private Map<String, String> metadata;
+
+        public String getId() {
+            return id;
+        }
+
+        public void setId(String id) {
+            this.id = id;
+        }
+
+        public String getHost() {
+            return host;
+        }
+
+        public void setHost(String host) {
+            this.host = host;
+        }
+
+        public int getPort() {
+            return port;
+        }
+
+        public void setPort(int port) {
+            this.port = port;
+        }
+
+        public Map<String, String> getMetadata() {
+            return metadata;
+        }
+
+        public void setMetadata(Map<String, String> metadata) {
+            this.metadata = metadata;
+        }
+    }
+
     // *****************************************
     // Service Filter
     // *****************************************
diff --git a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceDiscoveryAutoConfiguration.java b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceDiscoveryAutoConfiguration.java
index 9ba1600..e931b6e 100644
--- a/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceDiscoveryAutoConfiguration.java
+++ b/core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceDiscoveryAutoConfiguration.java
@@ -24,6 +24,7 @@ import javax.annotation.PostConstruct;
 import org.apache.camel.CamelContext;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.cloud.ServiceDiscovery;
+import org.apache.camel.impl.cloud.DefaultServiceDefinition;
 import org.apache.camel.impl.cloud.StaticServiceDiscovery;
 import org.apache.camel.spring.boot.util.GroupCondition;
 import org.slf4j.Logger;
@@ -116,6 +117,19 @@ public class CamelCloudServiceDiscoveryAutoConfiguration implements BeanFactoryA
             staticServiceDiscovery.addServers(entry.getKey(), entry.getValue());
         }
 
+        configuration.getServiceDefinitions().entrySet()
+            .stream()
+            .flatMap(serviceDefinitionEntry -> serviceDefinitionEntry.getValue()
+                .stream()
+                .map(serviceDefinitionConf -> DefaultServiceDefinition.builder()
+                    .withName(serviceDefinitionEntry.getKey())
+                    .withId(serviceDefinitionConf.getId())
+                    .withHost(serviceDefinitionConf.getHost())
+                    .withPort(serviceDefinitionConf.getPort())
+                    .withMeta(serviceDefinitionConf.getMetadata())
+                    .build())
+            ).forEach(staticServiceDiscovery::addServer);
+
         return staticServiceDiscovery;
     }
 }
diff --git a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java
index 864ad21..c1bba5c 100644
--- a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java
+++ b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/cloud/CamelCloudServiceCallConfigurationTest.java
@@ -16,10 +16,14 @@
  */
 package org.apache.camel.spring.boot.cloud;
 
+import java.util.List;
+import java.util.Map;
 import org.apache.camel.cloud.ServiceChooser;
+import org.apache.camel.cloud.ServiceDefinition;
 import org.apache.camel.cloud.ServiceDiscovery;
 import org.apache.camel.cloud.ServiceFilter;
 import org.apache.camel.cloud.ServiceLoadBalancer;
+import org.apache.camel.impl.cloud.StaticServiceDiscovery;
 import org.apache.camel.spring.boot.CamelAutoConfiguration;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -27,10 +31,10 @@ import org.springframework.boot.autoconfigure.AutoConfigurations;
 import org.springframework.boot.test.context.runner.ApplicationContextRunner;
 import org.springframework.core.env.Environment;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-@Ignore("TODO: Fix me later")
 public class CamelCloudServiceCallConfigurationTest {
     @Test
     public void testConfiguration() {
@@ -57,14 +61,69 @@ public class CamelCloudServiceCallConfigurationTest {
                     assertFalse(env.getProperty("camel.cloud.service-filter.enabled", Boolean.class));
                     assertTrue(env.getProperty("camel.cloud.service-chooser.enabled", Boolean.class));
                     assertFalse(env.getProperty("camel.cloud.load-balancer.enabled", Boolean.class));
-            
+
                     assertTrue(context.getBeansOfType(ServiceDiscovery.class).isEmpty());
                     assertTrue(context.getBeansOfType(ServiceFilter.class).isEmpty());
                     assertTrue(context.getBeansOfType(ServiceChooser.class).isEmpty());
                     assertTrue(context.getBeansOfType(ServiceLoadBalancer.class).isEmpty());
-                                      
+
                 }
             );
     }
 
+    @Test
+    public void testStaticServiceDiscoveryConfigurationWithMetadata() {
+      new ApplicationContextRunner()
+          .withConfiguration(
+              AutoConfigurations.of(
+                  CamelAutoConfiguration.class,
+                  CamelCloudAutoConfiguration.class,
+                  CamelCloudServiceChooserAutoConfiguration.class,
+                  CamelCloudServiceDiscoveryAutoConfiguration.class
+              )
+          )
+          .withPropertyValues(
+              "camel.cloud.enabled=true",
+              "camel.cloud.service-discovery.enabled=true",
+              "camel.cloud.service-discovery.services.first-service[0]=service1-1:80",
+              "camel.cloud.service-discovery.services.first-service[1]=service1-2:80",
+              "camel.cloud.service-discovery.services.second-service[0]=service2-1:80",
+              "camel.cloud.service-discovery.service-definitions.third-service-with-metadata[0].id=service3-0",
+              "camel.cloud.service-discovery.service-definitions.third-service-with-metadata[0].host=service3",
+              "camel.cloud.service-discovery.service-definitions.third-service-with-metadata[0].port=80",
+              "camel.cloud.service-discovery.service-definitions.third-service-with-metadata[0].metadata.foo=bar",
+              "camel.cloud.service-discovery.configurations.my-static-service-discovery.services.fourth-service[0]=service4-1:80",
+              "camel.cloud.service-discovery.configurations.my-static-service-discovery.service-definitions.fifth-service-with-metadata[0].host=service5",
+              "camel.cloud.service-discovery.configurations.my-static-service-discovery.service-definitions.fifth-service-with-metadata[0].port=80",
+              "camel.cloud.service-discovery.configurations.my-static-service-discovery.service-definitions.fifth-service-with-metadata[0].metadata.baz=gnarf",
+              "camel.cloud.service-filter.enabled=false",
+              "camel.cloud.service-chooser.enabled=false",
+              "camel.cloud.load-balancer.enabled=false",
+              "debug=false"
+          )
+          .run(context -> {
+                Map<String, ServiceDiscovery> serviceDiscoveryBeans = context.getBeansOfType(ServiceDiscovery.class);
+
+                ServiceDiscovery staticServiceDiscovery = serviceDiscoveryBeans.get("static-service-discovery");
+                assertEquals(2, staticServiceDiscovery.getServices("first-service").size());
+                assertEquals(1, staticServiceDiscovery.getServices("second-service").size());
+                List<ServiceDefinition> serviceDefinitionsWithMetadata = staticServiceDiscovery.getServices("third-service-with-metadata");
+                assertEquals(1, serviceDefinitionsWithMetadata.size());
+                ServiceDefinition serviceDefinition = serviceDefinitionsWithMetadata.get(0);
+                assertEquals("third-service-with-metadata", serviceDefinition.getName());
+                assertEquals("service3-0", serviceDefinition.getId());
+                assertEquals("service3", serviceDefinition.getHost());
+                assertEquals(80, serviceDefinition.getPort());
+                assertEquals("bar", serviceDefinition.getMetadata().get("foo"));
+
+                ServiceDiscovery myStaticServiceDiscovery = serviceDiscoveryBeans.get("my-static-service-discovery");
+                assertEquals(myStaticServiceDiscovery.getServices("fourth-service").size(), 1);
+                List<ServiceDefinition> services = myStaticServiceDiscovery.getServices("fifth-service-with-metadata");
+                assertEquals(services.size(), 1);
+                assertEquals(services.get(0).getMetadata().get("baz"), "gnarf");
+
+              }
+          );
+    }
+
 }