You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by zh...@apache.org on 2019/07/01 14:15:24 UTC

[servicecomb-pack] 01/02: SCB-1321 Support for defining Akka properties in application.yaml

This is an automated email from the ASF dual-hosted git repository.

zhanglei pushed a commit to branch SCB-1321
in repository https://gitbox.apache.org/repos/asf/servicecomb-pack.git

commit 6ba78992ebd9843220f0b889516ce80982610795
Author: Lei Zhang <zh...@apache.org>
AuthorDate: Sat Jun 29 23:36:27 2019 +0800

    SCB-1321 Support for defining Akka properties in application.yaml
---
 .../pack/alpha/fsm/FsmAutoConfiguration.java       | 13 +++--
 .../akka/AkkaConfigPropertyAdapter.java            | 68 ++++++++++++++++++++++
 .../pack/alpha/fsm/SagaIntegrationTest.java        |  5 +-
 .../{application.conf => application.yaml}         |  7 ++-
 4 files changed, 85 insertions(+), 8 deletions(-)

diff --git a/alpha/alpha-fsm/src/main/java/org/apache/servicecomb/pack/alpha/fsm/FsmAutoConfiguration.java b/alpha/alpha-fsm/src/main/java/org/apache/servicecomb/pack/alpha/fsm/FsmAutoConfiguration.java
index 252d6ae..c5082ab 100644
--- a/alpha/alpha-fsm/src/main/java/org/apache/servicecomb/pack/alpha/fsm/FsmAutoConfiguration.java
+++ b/alpha/alpha-fsm/src/main/java/org/apache/servicecomb/pack/alpha/fsm/FsmAutoConfiguration.java
@@ -21,25 +21,30 @@ import akka.actor.ActorSystem;
 import com.google.common.eventbus.EventBus;
 import com.typesafe.config.Config;
 import com.typesafe.config.ConfigFactory;
+import java.util.Map;
 import org.apache.servicecomb.pack.alpha.fsm.event.consumer.SagaEventConsumer;
+import org.apache.servicecomb.pack.alpha.fsm.spring.integration.akka.AkkaConfigPropertyAdapter;
 import org.apache.servicecomb.pack.alpha.fsm.spring.integration.eventbus.EventSubscribeBeanPostProcessor;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.ConfigurableEnvironment;
 
 @Configuration
 @ConditionalOnProperty(value = {"alpha.model.actor.enabled"})
 public class FsmAutoConfiguration {
 
   @Bean
-  public ActorSystem actorSystem() {
-    ActorSystem system = ActorSystem.create("alpha-akka", akkaConfiguration());
+  public ActorSystem actorSystem(ConfigurableApplicationContext applicationContext, ConfigurableEnvironment environment) {
+    ActorSystem system = ActorSystem.create("alpha-akka", akkaConfiguration(applicationContext,environment));
     return system;
   }
 
   @Bean
-  public Config akkaConfiguration() {
-    return ConfigFactory.load();
+  public Config akkaConfiguration(ConfigurableApplicationContext applicationContext, ConfigurableEnvironment environment) {
+    final Map<String, Object> converted = AkkaConfigPropertyAdapter.getPropertyMap(environment);
+    return ConfigFactory.parseMap(converted).withFallback(ConfigFactory.defaultReference(applicationContext.getClassLoader()));
   }
 
   @Bean(name = "sagaEventBus")
diff --git a/alpha/alpha-fsm/src/main/java/org/apache/servicecomb/pack/alpha/fsm/spring/integration/akka/AkkaConfigPropertyAdapter.java b/alpha/alpha-fsm/src/main/java/org/apache/servicecomb/pack/alpha/fsm/spring/integration/akka/AkkaConfigPropertyAdapter.java
new file mode 100644
index 0000000..c6ae195
--- /dev/null
+++ b/alpha/alpha-fsm/src/main/java/org/apache/servicecomb/pack/alpha/fsm/spring/integration/akka/AkkaConfigPropertyAdapter.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.servicecomb.pack.alpha.fsm.spring.integration.akka;
+
+import java.lang.invoke.MethodHandles;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.EnumerablePropertySource;
+import org.springframework.core.env.PropertySource;
+
+import java.util.*;
+import org.springframework.core.env.StandardEnvironment;
+
+public class AkkaConfigPropertyAdapter {
+
+  private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public static final String PROPERTY_SOURCE_NAME = "akkaConfig.";
+
+  public static Map<String, Object> getPropertyMap(ConfigurableEnvironment environment) {
+    final Map<String, Object> propertyMap = new HashMap<>();
+
+    for (final PropertySource source : environment.getPropertySources()) {
+      if (isEligiblePropertySource(source)) {
+        final EnumerablePropertySource enumerable = (EnumerablePropertySource) source;
+        LOG.debug("Adding properties from property source " + source.getName());
+        for (final String name : enumerable.getPropertyNames()) {
+          if (name.startsWith(PROPERTY_SOURCE_NAME) && !propertyMap.containsKey(name)) {
+            String key = name.substring(PROPERTY_SOURCE_NAME.length());
+            Object value = environment.getProperty(name);
+            if (LOG.isTraceEnabled()) {
+              LOG.trace("Adding property {}={}" + key, value);
+            }
+            propertyMap.put(key, value);
+          }
+        }
+      }
+    }
+
+    return Collections.unmodifiableMap(propertyMap);
+  }
+
+  public static boolean isEligiblePropertySource(PropertySource source) {
+    // Exclude system environment properties and system property sources
+    // because they are already included in the default configuration
+    final String name = source.getName();
+    return (source instanceof EnumerablePropertySource) &&
+        !(
+            name.equalsIgnoreCase(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME) ||
+                name.equalsIgnoreCase(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)
+        );
+  }
+}
diff --git a/alpha/alpha-fsm/src/test/java/org/apache/servicecomb/pack/alpha/fsm/SagaIntegrationTest.java b/alpha/alpha-fsm/src/test/java/org/apache/servicecomb/pack/alpha/fsm/SagaIntegrationTest.java
index c13c6af..5a213f1 100644
--- a/alpha/alpha-fsm/src/test/java/org/apache/servicecomb/pack/alpha/fsm/SagaIntegrationTest.java
+++ b/alpha/alpha-fsm/src/test/java/org/apache/servicecomb/pack/alpha/fsm/SagaIntegrationTest.java
@@ -35,7 +35,10 @@ import org.springframework.test.context.junit4.SpringRunner;
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes = {SagaApplication.class},
     properties = {
-        "alpha.model.actor.enabled=true"
+        "alpha.model.actor.enabled=true",
+        "akkaConfig.akka.persistence.journal.plugin=akka.persistence.journal.inmem",
+        "akkaConfig.akka.persistence.snapshot-store.plugin=akka.persistence.snapshot-store.local",
+        "akkaConfig.akka.persistence.snapshot-store.local.dir=target/example/snapshots"
     })
 public class SagaIntegrationTest {
 
diff --git a/alpha/alpha-fsm/src/test/resources/application.conf b/alpha/alpha-fsm/src/test/resources/application.yaml
similarity index 80%
rename from alpha/alpha-fsm/src/test/resources/application.conf
rename to alpha/alpha-fsm/src/test/resources/application.yaml
index 8d39346..b3577a6 100644
--- a/alpha/alpha-fsm/src/test/resources/application.conf
+++ b/alpha/alpha-fsm/src/test/resources/application.yaml
@@ -15,6 +15,7 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
-akka.persistence.journal.plugin = "akka.persistence.journal.inmem"
-akka.persistence.snapshot-store.plugin = "akka.persistence.snapshot-store.local"
-akka.persistence.snapshot-store.local.dir = "target/example/snapshots"
\ No newline at end of file
+akkaConfig:
+  akka.persistence.journal.plugin: akka.persistence.journal.inmem
+  akka.persistence.snapshot-store.plugin: akka.persistence.snapshot-store.local
+  akka.persistence.snapshot-store.local.dir: target/example/snapshots
\ No newline at end of file