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/12 08:38:22 UTC

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

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

 ##########
 File path: sharding-orchestration/sharding-orchestration-center/sharding-orchestration-center-apollo/src/main/java/org/apache/shardingsphere/orchestration/center/instance/wrapper/ApolloOpenApiWrapper.java
 ##########
 @@ -0,0 +1,108 @@
+/*
+ * 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.shardingsphere.orchestration.center.instance.wrapper;
+
+import com.ctrip.framework.apollo.core.ConfigConsts;
+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.primitives.Ints;
+import org.apache.shardingsphere.orchestration.center.configuration.InstanceConfiguration;
+
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Apollo open api client wrapper.
+ *
+ * @author dongzonglei
+ */
+public final class ApolloOpenApiWrapper {
+    
+    private ApolloOpenApiClient client;
+    
+    private String namespace;
+    
+    private String appId;
+    
+    private String env;
+    
+    private String clusterName;
+    
+    private String administrator;
+    
+    public ApolloOpenApiWrapper(final InstanceConfiguration config, final Properties properties) {
+        namespace = config.getNamespace();
+        appId = properties.getProperty("appId", "APOLLO_SHARDING_SPHERE");
+        env = properties.getProperty("env", "DEV");
+        clusterName = properties.getProperty("clusterName", ConfigConsts.CLUSTER_NAME_DEFAULT);
+        administrator = properties.getProperty("administrator");
+        String apolloToken = properties.getProperty("token");
+        String portalUrl = properties.getProperty("portalUrl");
+        Integer connectTimeout = Ints.tryParse(Objects.toString(properties.get("connectTimeout")));
+        Integer readTimeout = Ints.tryParse(Objects.toString(properties.get("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();
+    }
+    
+    /**
+     * Get config value by key.
+     * 
+     * @param key key
+     * @return value
+     */
+    public String getValue(final String key) {
+        OpenItemDTO itemDTO = client.getItem(appId, env, clusterName, namespace, key);
+        if (itemDTO == null) {
+            return null;
+        }
+        return itemDTO.getValue();
+    }
+    
+    /**
+     * Persist config.
+     * 
+     * @param key key
+     * @param value value
+     */
+    public void persist(final String key, final String value) {
 
 Review comment:
   Whether we should add TCC code for transaction.

----------------------------------------------------------------
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