|
@@ -0,0 +1,88 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2021 the original author, All Rights Reserved.
|
|
|
+ * Created by Deason on 2021-04-28 22:23:25
|
|
|
+ */
|
|
|
+
|
|
|
+package cn.com.qmth.framework.config.center.client.core;
|
|
|
+
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: Deason
|
|
|
+ * @since: 2021/4/21
|
|
|
+ */
|
|
|
+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);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(namespace)) {
|
|
|
+ log.warn("{} is not exist", ConfigConstants.SYS_CONFIG_CENTER_NAMESPACE);
|
|
|
+ System.exit(-1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(appCode)) {
|
|
|
+ log.warn("{} is not exist", ConfigConstants.CONFIG_CENTER_APP_CODE);
|
|
|
+ System.exit(-1);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (StringUtils.isEmpty(profile)) {
|
|
|
+ log.warn("{} is not exist", ConfigConstants.SPRING_PROFILES_ACTIVE);
|
|
|
+ System.exit(-1);
|
|
|
+ }
|
|
|
+
|
|
|
+ String configCenterUrl = String.format("http://%s/config/center/pull/%s/%s/%s", address, namespace, appCode, profile);
|
|
|
+ log.info("ConfigCenter call url {}", configCenterUrl);
|
|
|
+
|
|
|
+ RequestBody formBody = FormBody.create(MediaType.parse(CONTENT_TYPE), "{}");
|
|
|
+ Request.Builder request = new Request.Builder().url(configCenterUrl).post(formBody);
|
|
|
+
|
|
|
+ try (Response response = new OkHttpClient.Builder().build().newCall(request.build()).execute();
|
|
|
+ ResponseBody body = response.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);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ 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",
|
|
|
+ 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);
|
|
|
+
|
|
|
+ try (Response response = new OkHttpClient.Builder().build().newCall(request.build()).execute();) {
|
|
|
+ } catch (Exception e) {
|
|
|
+ // ignore
|
|
|
+ log.warn(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|