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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git

commit d27969a56155104270a572084d898a7ea6131fbf
Author: liubao <bi...@qq.com>
AuthorDate: Sun Sep 24 18:00:48 2023 +0800

    [SCB-2008]change DynamicPropertyFactory to Environment: in edge core
---
 .../edge/core/CommonHttpEdgeDispatcher.java        | 28 ++++++----
 .../servicecomb/edge/core/EdgeAddHeaderFilter.java | 41 +++++++++-----
 .../edge/core/EdgeCoreConfiguration.java           |  5 +-
 .../edge/core/URLMappedConfigurationLoader.java    | 30 +++++------
 .../edge/core/URLMappedEdgeDispatcher.java         | 28 ++++++----
 .../edge/core/TestURLMappedEdgeDispatcher.java     | 62 +++++++++++++++++-----
 6 files changed, 131 insertions(+), 63 deletions(-)

diff --git 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/CommonHttpEdgeDispatcher.java
 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/CommonHttpEdgeDispatcher.java
index f131caf28..fbeb0585d 100644
--- 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/CommonHttpEdgeDispatcher.java
+++ 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/CommonHttpEdgeDispatcher.java
@@ -19,7 +19,9 @@ package org.apache.servicecomb.edge.core;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
+import org.apache.servicecomb.config.ConfigurationChangedEvent;
 import org.apache.servicecomb.config.MicroserviceProperties;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.foundation.common.cache.VersionedCache;
@@ -41,8 +43,7 @@ import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 
-import com.netflix.config.ConcurrentCompositeConfiguration;
-import com.netflix.config.DynamicPropertyFactory;
+import com.google.common.eventbus.Subscribe;
 
 import io.vertx.core.Future;
 import io.vertx.core.Handler;
@@ -122,15 +123,22 @@ public class CommonHttpEdgeDispatcher extends 
AbstractEdgeDispatcher {
   }
 
   private void loadConfigurations() {
-    ConcurrentCompositeConfiguration config = 
(ConcurrentCompositeConfiguration) DynamicPropertyFactory
-        .getBackingConfigurationSource();
-    configurations = URLMappedConfigurationLoader.loadConfigurations(config, 
KEY_MAPPING_PREFIX);
-    config.addConfigurationListener(event -> {
-      if (event.getPropertyName().startsWith(KEY_MAPPING_PREFIX)) {
-        LOG.info("Map rule have been changed. Reload configurations. Event=" + 
event.getType());
-        configurations = 
URLMappedConfigurationLoader.loadConfigurations(config, KEY_MAPPING_PREFIX);
+    configurations = 
URLMappedConfigurationLoader.loadConfigurations(environment, 
KEY_MAPPING_PREFIX);
+  }
+
+  @Subscribe
+  public void onConfigurationChangedEvent(ConfigurationChangedEvent event) {
+    Map<String, Object> changed = new HashMap<>();
+    changed.putAll(event.getDeleted());
+    changed.putAll(event.getAdded());
+    changed.putAll(event.getUpdated());
+
+    for (Entry<String, Object> entry : changed.entrySet()) {
+      if (entry.getKey().startsWith(KEY_MAPPING_PREFIX)) {
+        loadConfigurations();
+        break;
       }
-    });
+    }
   }
 
   protected void onRequest(RoutingContext context) {
diff --git 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java
 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java
index 0a55d51dd..65a20bc7e 100644
--- 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java
+++ 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeAddHeaderFilter.java
@@ -18,26 +18,31 @@ package org.apache.servicecomb.edge.core;
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.concurrent.CompletableFuture;
 import java.util.function.BiConsumer;
 
 import javax.annotation.Nonnull;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.config.ConfigurationChangedEvent;
 import org.apache.servicecomb.core.Invocation;
 import org.apache.servicecomb.core.filter.ConsumerFilter;
 import org.apache.servicecomb.core.filter.Filter;
 import org.apache.servicecomb.core.filter.FilterNode;
+import org.apache.servicecomb.foundation.common.event.EventManager;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
 import org.apache.servicecomb.swagger.invocation.InvocationType;
 import org.apache.servicecomb.swagger.invocation.Response;
 import org.apache.servicecomb.transport.rest.client.RestClientTransportContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.env.Environment;
 
-import com.netflix.config.ConfigurationManager;
-import com.netflix.config.DynamicPropertyFactory;
+import com.google.common.eventbus.Subscribe;
 
 public class EdgeAddHeaderFilter implements ConsumerFilter {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(EdgeAddHeaderFilter.class);
@@ -50,24 +55,36 @@ public class EdgeAddHeaderFilter implements ConsumerFilter {
 
   private static final String KEY_HEADERS = PREFIX + ".allowedHeaders";
 
+  private final Environment environment;
+
   private List<String> publicHeaders = new ArrayList<>();
 
   private boolean enabled = false;
 
-  public EdgeAddHeaderFilter() {
+  public EdgeAddHeaderFilter(Environment environment) {
+    this.environment = environment;
     init();
-    ConfigurationManager.getConfigInstance()
-        .addConfigurationListener(event -> {
-          if (StringUtils.startsWith(event.getPropertyName(), PREFIX)) {
-            LOGGER.info("Public headers config have been changed. Event=" + 
event.getType());
-            init();
-          }
-        });
+    EventManager.register(this);
+  }
+
+  @Subscribe
+  public void onConfigurationChangedEvent(ConfigurationChangedEvent event) {
+    Map<String, Object> changed = new HashMap<>();
+    changed.putAll(event.getDeleted());
+    changed.putAll(event.getAdded());
+    changed.putAll(event.getUpdated());
+
+    for (Entry<String, Object> entry : changed.entrySet()) {
+      if (entry.getKey().startsWith(PREFIX)) {
+        init();
+        break;
+      }
+    }
   }
 
   private void init() {
-    enabled = 
DynamicPropertyFactory.getInstance().getBooleanProperty(KEY_ENABLED, 
false).get();
-    String publicHeaderStr = 
DynamicPropertyFactory.getInstance().getStringProperty(KEY_HEADERS, "").get();
+    enabled = environment.getProperty(KEY_ENABLED, boolean.class, false);
+    String publicHeaderStr = environment.getProperty(KEY_HEADERS, "");
     String[] split = publicHeaderStr.split(",");
     if (split.length > 0) {
       publicHeaders = Arrays.asList(split);
diff --git 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java
 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java
index e6cb427ee..45333b7d0 100644
--- 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java
+++ 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/EdgeCoreConfiguration.java
@@ -18,6 +18,7 @@ package org.apache.servicecomb.edge.core;
 
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.core.env.Environment;
 
 @Configuration
 public class EdgeCoreConfiguration {
@@ -32,7 +33,7 @@ public class EdgeCoreConfiguration {
   }
 
   @Bean
-  public EdgeAddHeaderFilter edgeAddHeaderFilter() {
-    return new EdgeAddHeaderFilter();
+  public EdgeAddHeaderFilter edgeAddHeaderFilter(Environment environment) {
+    return new EdgeAddHeaderFilter(environment);
   }
 }
diff --git 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedConfigurationLoader.java
 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedConfigurationLoader.java
index b7fd78f16..ae93c1349 100644
--- 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedConfigurationLoader.java
+++ 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedConfigurationLoader.java
@@ -18,16 +18,16 @@
 package org.apache.servicecomb.edge.core;
 
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
+import java.util.Set;
 import java.util.regex.Pattern;
 
 import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.config.ConfigUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
-import com.netflix.config.ConcurrentCompositeConfiguration;
-import com.netflix.config.DynamicPropertyFactory;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.Environment;
 
 public class URLMappedConfigurationLoader {
   private static final Logger LOG = 
LoggerFactory.getLogger(URLMappedConfigurationLoader.class);
@@ -41,15 +41,13 @@ public class URLMappedConfigurationLoader {
   private static final String KEY_MAPPING_PREFIX_SEGMENT_COUNT = 
"%s.%s.prefixSegmentCount";
 
   public static Map<String, URLMappedConfigurationItem> loadConfigurations(
-      ConcurrentCompositeConfiguration config, String configPrefix) {
+      Environment environment, String configPrefix) {
     Map<String, URLMappedConfigurationItem> configurations = new HashMap<>();
-    Iterator<String> configsItems = config.getKeys(configPrefix);
-    while (configsItems.hasNext()) {
-      String pathKey = configsItems.next();
+    Set<String> configsItems = 
ConfigUtil.propertiesWithPrefix((ConfigurableEnvironment) environment, 
configPrefix);
+    for (String pathKey : configsItems) {
       if (pathKey.endsWith(KEY_MAPPING_PATH)) {
         URLMappedConfigurationItem configurationItem = new 
URLMappedConfigurationItem();
-        String pattern = DynamicPropertyFactory.getInstance()
-            .getStringProperty(pathKey, null).get();
+        String pattern = environment.getProperty(pathKey);
         if (StringUtils.isEmpty(pattern)) {
           continue;
         }
@@ -57,15 +55,15 @@ public class URLMappedConfigurationLoader {
         configurationItem.setStringPattern(pattern);
         String pathKeyItem = pathKey
             .substring(configPrefix.length() + 1, pathKey.length() - 
KEY_MAPPING_PATH.length());
-        
configurationItem.setMicroserviceName(DynamicPropertyFactory.getInstance()
-            .getStringProperty(String.format(KEY_MAPPING_SERVICE_NAME, 
configPrefix, pathKeyItem), null).get());
+        configurationItem.setMicroserviceName(environment.getProperty(
+            String.format(KEY_MAPPING_SERVICE_NAME, configPrefix, 
pathKeyItem)));
         if (StringUtils.isEmpty(configurationItem.getMicroserviceName())) {
           continue;
         }
-        
configurationItem.setPrefixSegmentCount(DynamicPropertyFactory.getInstance()
-            .getIntProperty(String.format(KEY_MAPPING_PREFIX_SEGMENT_COUNT, 
configPrefix, pathKeyItem), 0).get());
-        configurationItem.setVersionRule(DynamicPropertyFactory.getInstance()
-            .getStringProperty(String.format(KEY_MAPPING_VERSION_RULE, 
configPrefix, pathKeyItem), "0.0.0+").get());
+        configurationItem.setPrefixSegmentCount(environment.getProperty(
+            String.format(KEY_MAPPING_PREFIX_SEGMENT_COUNT, configPrefix, 
pathKeyItem), int.class, 0));
+        configurationItem.setVersionRule(environment.getProperty(
+            String.format(KEY_MAPPING_VERSION_RULE, configPrefix, 
pathKeyItem), "0.0.0+"));
         configurations.put(pathKeyItem, configurationItem);
       }
     }
diff --git 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedEdgeDispatcher.java
 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedEdgeDispatcher.java
index 868e12e09..310381e8a 100644
--- 
a/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedEdgeDispatcher.java
+++ 
b/edge/edge-core/src/main/java/org/apache/servicecomb/edge/core/URLMappedEdgeDispatcher.java
@@ -19,8 +19,10 @@ package org.apache.servicecomb.edge.core;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.servicecomb.common.rest.RestProducerInvocationFlow;
+import org.apache.servicecomb.config.ConfigurationChangedEvent;
 import org.apache.servicecomb.core.invocation.InvocationCreator;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
 import org.apache.servicecomb.foundation.vertx.http.HttpServletResponseEx;
@@ -33,8 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.env.Environment;
 
 import com.google.common.annotations.VisibleForTesting;
-import com.netflix.config.ConcurrentCompositeConfiguration;
-import com.netflix.config.DynamicPropertyFactory;
+import com.google.common.eventbus.Subscribe;
 
 import io.vertx.ext.web.Router;
 import io.vertx.ext.web.RoutingContext;
@@ -100,15 +101,22 @@ public class URLMappedEdgeDispatcher extends 
AbstractEdgeDispatcher {
   }
 
   private void loadConfigurations() {
-    ConcurrentCompositeConfiguration config = 
(ConcurrentCompositeConfiguration) DynamicPropertyFactory
-        .getBackingConfigurationSource();
-    configurations = URLMappedConfigurationLoader.loadConfigurations(config, 
KEY_MAPPING_PREFIX);
-    config.addConfigurationListener(event -> {
-      if (event.getPropertyName().startsWith(KEY_MAPPING_PREFIX)) {
-        LOG.info("Map rule have been changed. Reload configurations. Event=" + 
event.getType());
-        configurations = 
URLMappedConfigurationLoader.loadConfigurations(config, KEY_MAPPING_PREFIX);
+    configurations = 
URLMappedConfigurationLoader.loadConfigurations(environment, 
KEY_MAPPING_PREFIX);
+  }
+
+  @Subscribe
+  public void onConfigurationChangedEvent(ConfigurationChangedEvent event) {
+    Map<String, Object> changed = new HashMap<>();
+    changed.putAll(event.getDeleted());
+    changed.putAll(event.getAdded());
+    changed.putAll(event.getUpdated());
+
+    for (Entry<String, Object> entry : changed.entrySet()) {
+      if (entry.getKey().startsWith(KEY_MAPPING_PREFIX)) {
+        loadConfigurations();
+        break;
       }
-    });
+    }
   }
 
   protected void preCheck(RoutingContext context) {
diff --git 
a/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java
 
b/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java
index 805374bd9..2e23ab673 100644
--- 
a/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java
+++ 
b/edge/edge-core/src/test/java/org/apache/servicecomb/edge/core/TestURLMappedEdgeDispatcher.java
@@ -17,36 +17,41 @@
 
 package org.apache.servicecomb.edge.core;
 
+import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
+import org.apache.servicecomb.config.ConfigurationChangedEvent;
 import org.apache.servicecomb.transport.rest.vertx.RestBodyHandler;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
-import org.springframework.core.env.Environment;
-
-import com.netflix.config.DynamicPropertyFactory;
+import org.springframework.core.env.ConfigurableEnvironment;
+import org.springframework.core.env.EnumerablePropertySource;
+import org.springframework.core.env.MutablePropertySources;
 
 import io.vertx.ext.web.RoutingContext;
 
 public class TestURLMappedEdgeDispatcher {
-  Environment environment = Mockito.mock(Environment.class);
+  ConfigurableEnvironment environment = 
Mockito.mock(ConfigurableEnvironment.class);
 
   @BeforeEach
   public void setUp() throws Exception {
-    DynamicPropertyFactory.getInstance();
   }
 
   @AfterEach
   public void tearDown() {
-    ArchaiusUtils.resetConfig();
   }
 
   @Test
   public void testConfigurations() {
+    EnumerablePropertySource<?> propertySource = 
Mockito.mock(EnumerablePropertySource.class);
+    MutablePropertySources mutablePropertySources = new 
MutablePropertySources();
+    mutablePropertySources.addLast(propertySource);
+    
Mockito.when(environment.getPropertySources()).thenReturn(mutablePropertySources);
+    Mockito.when(propertySource.getPropertyNames()).thenReturn(new String[] {
+    });
     
Mockito.when(environment.getProperty("servicecomb.http.dispatcher.edge.url.enabled",
 boolean.class, false))
         .thenReturn(true);
     URLMappedEdgeDispatcher dispatcher = new URLMappedEdgeDispatcher();
@@ -58,10 +63,26 @@ public class TestURLMappedEdgeDispatcher {
     
Mockito.when(context.get(RestBodyHandler.BYPASS_BODY_HANDLER)).thenReturn(Boolean.TRUE);
     dispatcher.onRequest(context);
 
-    
ArchaiusUtils.setProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.path",
 "/a/b/c/.*");
-    
ArchaiusUtils.setProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.microserviceName",
 "serviceName");
-    
ArchaiusUtils.setProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.prefixSegmentCount",
 2);
-    
ArchaiusUtils.setProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.versionRule",
 "2.0.0+");
+    Mockito.when(propertySource.getPropertyNames()).thenReturn(new String[] {
+        "servicecomb.http.dispatcher.edge.url.mappings.service1.path",
+        
"servicecomb.http.dispatcher.edge.url.mappings.service1.microserviceName",
+        
"servicecomb.http.dispatcher.edge.url.mappings.service1.prefixSegmentCount",
+        "servicecomb.http.dispatcher.edge.url.mappings.service1.versionRule"
+    });
+    
Mockito.when(environment.getProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.path"))
+        .thenReturn("/a/b/c/.*");
+    
Mockito.when(environment.getProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.microserviceName"))
+        .thenReturn("serviceName");
+    
Mockito.when(environment.getProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.prefixSegmentCount",
+            int.class, 0))
+        .thenReturn(2);
+    
Mockito.when(environment.getProperty("servicecomb.http.dispatcher.edge.url.mappings.service1.versionRule",
+            "0.0.0+"))
+        .thenReturn("2.0.0+");
+    Map<String, Object> latest = new HashMap<>();
+    latest.put("servicecomb.http.dispatcher.edge.url.mappings.service1.path", 
"/a/b/c/.*");
+    
dispatcher.onConfigurationChangedEvent(ConfigurationChangedEvent.createIncremental(latest,
 new HashMap<>()));
+
     items = dispatcher.getConfigurations();
     Assertions.assertEquals(items.size(), 1);
     URLMappedConfigurationItem item = items.get("service1");
@@ -70,8 +91,23 @@ public class TestURLMappedEdgeDispatcher {
     Assertions.assertEquals(item.getStringPattern(), "/a/b/c/.*");
     Assertions.assertEquals(item.getVersionRule(), "2.0.0+");
 
-    
ArchaiusUtils.setProperty("servicecomb.http.dispatcher.edge.url.mappings.service2.versionRule",
 "2.0.0+");
-    
ArchaiusUtils.setProperty("servicecomb.http.dispatcher.edge.url.mappings.service3.path",
 "/b/c/d/.*");
+    Mockito.when(propertySource.getPropertyNames()).thenReturn(new String[] {
+        "servicecomb.http.dispatcher.edge.url.mappings.service1.path",
+        
"servicecomb.http.dispatcher.edge.url.mappings.service1.microserviceName",
+        
"servicecomb.http.dispatcher.edge.url.mappings.service1.prefixSegmentCount",
+        "servicecomb.http.dispatcher.edge.url.mappings.service1.versionRule",
+        "servicecomb.http.dispatcher.edge.url.mappings.service2.versionRule",
+        "servicecomb.http.dispatcher.edge.url.mappings.service3.path"
+    });
+    
Mockito.when(environment.getProperty("servicecomb.http.dispatcher.edge.url.mappings.service2.versionRule"))
+        .thenReturn("2.0.0+");
+    
Mockito.when(environment.getProperty("servicecomb.http.dispatcher.edge.url.mappings.service3.path"))
+        .thenReturn("/b/c/d/.*");
+
+    latest = new HashMap<>();
+    latest.put("servicecomb.http.dispatcher.edge.url.mappings.service3.path", 
"/a/b/c/.*");
+    
dispatcher.onConfigurationChangedEvent(ConfigurationChangedEvent.createIncremental(latest,
 new HashMap<>()));
+
     items = dispatcher.getConfigurations();
     Assertions.assertEquals(items.size(), 1);
     item = items.get("service1");

Reply via email to