|
@@ -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());
|
|
|
}
|
|
|
}
|
|
|
|