You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2020/02/05 01:09:03 UTC

[GitHub] [incubator-shardingsphere] wgy8283335 commented on a change in pull request #4166: refactor orchestration config for apollo.

wgy8283335 commented on a change in pull request #4166: refactor orchestration config for apollo.
URL: https://github.com/apache/incubator-shardingsphere/pull/4166#discussion_r375010993
 
 

 ##########
 File path: sharding-orchestration/sharding-orchestration-center/sharding-orchestration-center-apollo/src/main/java/org/apache/shardingsphere/orchestration/center/instance/ApolloInstance.java
 ##########
 @@ -17,177 +17,93 @@
 
 package org.apache.shardingsphere.orchestration.center.instance;
 
-import com.ctrip.framework.apollo.Config;
 import com.ctrip.framework.apollo.ConfigChangeListener;
-import com.ctrip.framework.apollo.ConfigService;
-import com.ctrip.framework.apollo.core.ConfigConsts;
 import com.ctrip.framework.apollo.enums.PropertyChangeType;
 import com.ctrip.framework.apollo.model.ConfigChange;
 import com.ctrip.framework.apollo.model.ConfigChangeEvent;
-import com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient;
-import com.ctrip.framework.apollo.openapi.client.constant.ApolloOpenApiConstants;
-import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO;
-import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
+import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
-import com.google.common.primitives.Ints;
 import lombok.Getter;
 import lombok.Setter;
-import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.orchestration.center.api.ConfigCenter;
 import org.apache.shardingsphere.orchestration.center.configuration.InstanceConfiguration;
-import org.apache.shardingsphere.orchestration.center.instance.node.ConfigTreeNode;
+import org.apache.shardingsphere.orchestration.center.instance.wrapper.ApolloConfigWrapper;
+import org.apache.shardingsphere.orchestration.center.instance.wrapper.ApolloOpenApiWrapper;
 import org.apache.shardingsphere.orchestration.center.listener.DataChangedEvent;
 import org.apache.shardingsphere.orchestration.center.listener.DataChangedEventListener;
+import org.apache.shardingsphere.orchestration.center.util.ConfigKeyUtils;
 
-import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
-import java.util.Set;
 
 /**
- * Registry center for Apollo.
+ * Config center for Apollo.
  *
  * @author dongzonglei
  */
-@Slf4j
 public final class ApolloInstance implements ConfigCenter {
     
-    private static final String SHARDING_SPHERE_KEY_ROOT = "/";
+    private final Map<String, DataChangedEventListener> caches = new HashMap<>();
     
-    private static final String SHARDING_SPHERE_KEY_SEPARATOR = "/";
+    private ApolloConfigWrapper configWrapper;
     
-    private static final String APOLLO_KEY_SEPARATOR = ".";
-    
-    private static final String APOLLO_KEY_APP_ID = "app.id";
-    
-    private static final String APOLLO_KEY_ENV = "env";
-    
-    private static final String APOLLO_KEY_CLUSTER = ConfigConsts.APOLLO_CLUSTER_KEY;
-    
-    private static final String APOLLO_KEY_META = ConfigConsts.APOLLO_META_KEY;
-    
-    private String namespace;
-    
-    private String appId;
-    
-    private String env;
-    
-    private String clusterName;
-    
-    private String administrator;
-    
-    private Config apolloConfig;
-    
-    private ApolloOpenApiClient client;
-    
-    private ConfigTreeNode tree;
+    private ApolloOpenApiWrapper openApiWrapper;
     
     @Getter
     @Setter
     private Properties properties = new Properties();
     
     @Override
     public void init(final InstanceConfiguration config) {
-        initApolloConfig(config);
-        initApolloOpenApiClient();
-        initKeysRelationship();
-    }
-    
-    private void initApolloConfig(final InstanceConfiguration config) {
-        namespace = config.getNamespace();
-        appId = properties.getProperty("appId", "APOLLO_SHARDING_SPHERE");
-        env = properties.getProperty("env", "DEV");
-        clusterName = properties.getProperty("clusterName", ConfigConsts.CLUSTER_NAME_DEFAULT);
-        System.setProperty(APOLLO_KEY_APP_ID, appId);
-        System.setProperty(APOLLO_KEY_ENV, env);
-        System.setProperty(APOLLO_KEY_CLUSTER, clusterName);
-        System.setProperty(APOLLO_KEY_META, config.getServerLists());
-        apolloConfig = ConfigService.getConfig(namespace);
-    }
-    
-    private void initApolloOpenApiClient() {
-        administrator = properties.getProperty("administrator");
-        String apolloToken = properties.getProperty("token");
-        String portalUrl = properties.getProperty("portalUrl");
-        Integer connectTimeout = Ints.tryParse(properties.getProperty("connectTimeout"));
-        Integer readTimeout = Ints.tryParse(properties.getProperty("readTimeout"));
-        client = ApolloOpenApiClient.newBuilder().withPortalUrl(portalUrl)
-                .withConnectTimeout(connectTimeout == null ? ApolloOpenApiConstants.DEFAULT_CONNECT_TIMEOUT : connectTimeout)
-                .withReadTimeout(readTimeout == null ? ApolloOpenApiConstants.DEFAULT_READ_TIMEOUT : readTimeout)
-                .withToken(apolloToken).build();
-    }
-    
-    private void initKeysRelationship() {
-        List<OpenItemDTO> items = client.getNamespace(appId, env, clusterName, namespace).getItems();
-        Set<String> keys = Sets.newHashSet();
-        for (OpenItemDTO each : items) {
-            keys.add(each.getKey());
-        }
-        tree = ConfigTreeNode.create(keys, ".");
+        configWrapper = new ApolloConfigWrapper(config, properties);
+        openApiWrapper = new ApolloOpenApiWrapper(config, properties);
     }
     
     @Override
     public String get(final String key) {
-        return apolloConfig.getProperty(convertKey(key), "");
-    }
-    
-    private String convertKey(final String shardingSphereKey) {
-        return shardingSphereKey.replace(SHARDING_SPHERE_KEY_SEPARATOR, APOLLO_KEY_SEPARATOR).substring(1);
-    }
-    
-    private String deConvertKey(final String apolloKey) {
-        return new StringBuilder(SHARDING_SPHERE_KEY_ROOT).append(apolloKey.replace(APOLLO_KEY_SEPARATOR, SHARDING_SPHERE_KEY_SEPARATOR)).toString();
+        String value = configWrapper.getProperty(ConfigKeyUtils.path2Key(key));
 
 Review comment:
   String value = configWrapper.getProperty(ConfigKeyUtils.path2Key(key));
   or
   String newKey = ConfigKeyUtils.path2Key(key);
   String value = configWrapper.getProperty(newKey);
   Which one is better, accroding to clean code?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services