deason 4 years ago
parent
commit
9d906ee9ba

+ 16 - 11
config-center-client/src/main/java/cn/com/qmth/framework/config/center/client/core/ConfigRefreshEndPoint.java

@@ -28,17 +28,22 @@ public class ConfigRefreshEndPoint {
 
     @ReadOperation
     public String configRefreshEndPoint() {
-        Map<String, Object> remoteProperties = RemotePropertyLoader.call(
-                environment.getProperty(ConfigConstants.SYS_CONFIG_CENTER_ADDRESS),
-                environment.getProperty(ConfigConstants.SYS_CONFIG_CENTER_NAMESPACE),
-                environment.getProperty(ConfigConstants.CONFIG_CENTER_APP_CODE),
-                environment.getProperty(ConfigConstants.SPRING_PROFILES_ACTIVE)
-        );
-
-        CustomMapPropertySource customMapPropertySource = new CustomMapPropertySource(ConfigConstants.CUSTOM_PROPERTY_SOURCE, remoteProperties);
-        environment.getPropertySources().addLast(customMapPropertySource);
-
-        return "finished";
+        try {
+            Map<String, Object> remoteProperties = RemotePropertyLoader.call(
+                    environment.getProperty(ConfigConstants.SYS_CONFIG_CENTER_ADDRESS),
+                    environment.getProperty(ConfigConstants.SYS_CONFIG_CENTER_NAMESPACE),
+                    environment.getProperty(ConfigConstants.CONFIG_CENTER_APP_CODE),
+                    environment.getProperty(ConfigConstants.SPRING_PROFILES_ACTIVE)
+            );
+
+            CustomMapPropertySource customMapPropertySource = new CustomMapPropertySource(ConfigConstants.CUSTOM_PROPERTY_SOURCE, remoteProperties);
+            environment.getPropertySources().addLast(customMapPropertySource);
+
+            return "finished";
+        } catch (Exception e) {
+            log.warn(e.getMessage(), e);
+            return e.getMessage();
+        }
     }
 
 }

+ 1 - 2
config-center-client/src/main/java/cn/com/qmth/framework/config/center/client/core/CustomEnvironmentPostProcessor.java

@@ -39,8 +39,7 @@ public class CustomEnvironmentPostProcessor implements CustomConfigProcessor, En
 
         CustomMapPropertySource customMapPropertySource = new CustomMapPropertySource(ConfigConstants.CUSTOM_PROPERTY_SOURCE, remoteProperties);
         environment.getPropertySources().addLast(customMapPropertySource);
-
-        this.printPropertySources(environment.getPropertySources());
+        // this.printPropertySources(environment.getPropertySources());
     }
 
     @Override

+ 6 - 8
config-center-client/src/main/java/cn/com/qmth/framework/config/center/client/core/CustomSpringApplicationRunListener.java

@@ -5,9 +5,10 @@
 
 package cn.com.qmth.framework.config.center.client.core;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.SpringApplicationRunListener;
-import org.springframework.boot.logging.DeferredLog;
 import org.springframework.context.ConfigurableApplicationContext;
 import org.springframework.core.Ordered;
 import org.springframework.core.env.ConfigurableEnvironment;
@@ -18,7 +19,7 @@ import org.springframework.core.env.ConfigurableEnvironment;
  */
 public class CustomSpringApplicationRunListener implements CustomConfigProcessor, SpringApplicationRunListener, Ordered {
 
-    private static final DeferredLog log = new DeferredLog();
+    private static final Logger log = LoggerFactory.getLogger(CustomSpringApplicationRunListener.class);
 
     private final SpringApplication application;
 
@@ -36,9 +37,6 @@ public class CustomSpringApplicationRunListener implements CustomConfigProcessor
         }
 
         this.notice(context.getEnvironment(), "started");
-
-        log.debug("started...");
-        log.replayTo(CustomSpringApplicationRunListener.class);
     }
 
     @Override
@@ -48,9 +46,6 @@ public class CustomSpringApplicationRunListener implements CustomConfigProcessor
         }
 
         this.notice(context.getEnvironment(), "failed");
-
-        log.debug("failed...");
-        log.replayTo(CustomSpringApplicationRunListener.class);
     }
 
     private void notice(ConfigurableEnvironment environment, String message) {
@@ -59,6 +54,8 @@ public class CustomSpringApplicationRunListener implements CustomConfigProcessor
             return;
         }
 
+        log.debug("notice {}...", message);
+
         RemotePropertyLoader.notice(
                 environment.getProperty(ConfigConstants.SYS_CONFIG_CENTER_ADDRESS),
                 environment.getProperty(ConfigConstants.SYS_CONFIG_CENTER_NAMESPACE),
@@ -66,6 +63,7 @@ public class CustomSpringApplicationRunListener implements CustomConfigProcessor
                 environment.getProperty(ConfigConstants.SPRING_PROFILES_ACTIVE),
                 message
         );
+
     }
 
     @Override

+ 11 - 22
config-center-client/src/main/java/cn/com/qmth/framework/config/center/client/core/RemotePropertyLoader.java

@@ -9,8 +9,6 @@ import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import okhttp3.*;
 import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -21,59 +19,51 @@ import java.util.Map;
  */
 public class RemotePropertyLoader {
 
-    private static final Logger log = LoggerFactory.getLogger(RemotePropertyLoader.class);
-
     private static final String CONTENT_TYPE = "application/json; charset=UTF-8";
 
     public static Map<String, Object> call(String address, String namespace, String appCode, String profile) {
         if (StringUtils.isEmpty(address)) {
-            log.warn("{} is not exist", ConfigConstants.SYS_CONFIG_CENTER_ADDRESS);
-            System.exit(-1);
+            throw new IllegalArgumentException(ConfigConstants.SYS_CONFIG_CENTER_ADDRESS + " is not exist");
         }
 
         if (StringUtils.isEmpty(namespace)) {
-            log.warn("{} is not exist", ConfigConstants.SYS_CONFIG_CENTER_NAMESPACE);
-            System.exit(-1);
+            throw new IllegalArgumentException(ConfigConstants.SYS_CONFIG_CENTER_NAMESPACE + " is not exist");
         }
 
         if (StringUtils.isEmpty(appCode)) {
-            log.warn("{} is not exist", ConfigConstants.CONFIG_CENTER_APP_CODE);
-            System.exit(-1);
+            throw new IllegalArgumentException(ConfigConstants.CONFIG_CENTER_APP_CODE + " is not exist");
         }
 
         if (StringUtils.isEmpty(profile)) {
-            log.warn("{} is not exist", ConfigConstants.SPRING_PROFILES_ACTIVE);
-            System.exit(-1);
+            throw new IllegalArgumentException(ConfigConstants.SPRING_PROFILES_ACTIVE + " is not exist");
         }
 
-        String configCenterUrl = String.format("http://%s/config/center/pull/%s/%s/%s", address, namespace, appCode, profile);
-        log.info("ConfigCenter call url {}", configCenterUrl);
+        final String configCenterUrl = String.format("http://%s/config/center/pull/%s/%s/%s",
+                address, namespace, appCode, profile);
 
         RequestBody formBody = FormBody.create(MediaType.parse(CONTENT_TYPE), "{}");
         Request.Builder request = new Request.Builder().url(configCenterUrl).post(formBody);
 
+        final String resp;
         try (Response response = new OkHttpClient.Builder().build().newCall(request.build()).execute();
              ResponseBody body = response.body();) {
-            String resp = body.string();
+            resp = body.string();
 
             if (response.isSuccessful()) {
                 ObjectMapper jsonMapper = new ObjectMapper();
                 JavaType javaType = jsonMapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class);
                 return jsonMapper.readValue(resp, javaType);
             }
-
-            log.error("ConfigCenter call fail... {}", resp);
         } catch (Exception e) {
-            log.error("ConfigCenter call err! {}", e.getMessage(), e);
+            throw new RuntimeException(configCenterUrl + " call err! " + e.getMessage(), e);
         }
 
-        return new HashMap<>();
+        throw new RuntimeException(configCenterUrl + " call fail! " + resp);
     }
 
     public static void notice(String address, String namespace, String appCode, String profile, String msg) {
-        String configCenterUrl = String.format("http://%s/config/center/notice/%s/%s/%s?msg=%s",
+        final String configCenterUrl = String.format("http://%s/config/center/notice/%s/%s/%s?msg=%s",
                 address, namespace, appCode, profile, msg);
-        log.info("ConfigCenter notice url {}", configCenterUrl);
 
         RequestBody formBody = FormBody.create(MediaType.parse(CONTENT_TYPE), "{}");
         Request.Builder request = new Request.Builder().url(configCenterUrl).post(formBody);
@@ -81,7 +71,6 @@ public class RemotePropertyLoader {
         try (Response response = new OkHttpClient.Builder().build().newCall(request.build()).execute();) {
         } catch (Exception e) {
             // ignore
-            log.warn(e.getMessage());
         }
     }