前言
实现一个快速读取consul配置的工具类。
代码
使用时直接调用枚举的
defaultValueIfNull()
方法即可
import com.google.gson.reflect.TypeToken;
import com.idiudiu.common.model.LoginRequest;
import com.idiudiu.common.utils.ConsulUtil;
import com.idiudiu.common.utils.GsonUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
@Getter
public enum ConsulConfig {
TEST_INT("test/int", 100, TypeToken.get(Integer.class).getType()),
TEST_DOUBLE("test/double", 100, TypeToken.get(Integer.class).getType()),
TEST_BOOL("test/bool", false, TypeToken.get(Boolean.class).getType()),
TEST_STRING("test/string", "test", TypeToken.get(String.class).getType()),
TEST_LIST("test/list", null, new TypeToken<List<Integer>>(){}.getType()),
TEST_MAP("test/map", null, new TypeToken<Map<Integer, String>>(){}.getType()),
TEST_MAP2("test/map2", null, new TypeToken<Map<Integer, Map<String, Double>>>(){}.getType()),
TEST_JSON("test/json", null, TypeToken.get(LoginRequest.class).getType()),
;
private final String key;
private final Object defaultValue;
private final Type type;
ConsulConfig(String key, Object defaultValue, Type type) {
this.key = key;
this.defaultValue = defaultValue;
this.type = type;
}
@SuppressWarnings("unchecked")
public <T> T defaultValueIfNull() {
String config = ConsulUtil.getConfig(key);
if (config == null) {
return (T) defaultValue;
}
if ("java.lang.String".equals(type.getTypeName())) {
return (T) config;
}
return GsonUtil.convert2Object(config, type);
}
}
import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import com.idiudiu.common.config.ConsulConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Map;
@Slf4j
public class ConsulUtil {
private static final String KV_URL = "http://localhost:8500/v1/kv/";
private static Map<String, String> getRequestHeader() {
Map<String, String> header = Maps.newHashMap();
header.put("Accept", "application/json");
header.put("Content-Type", "application/json");
return header;
}
public static String getConfig(String key) {
try {
String url = KV_URL + key;
String response = HttpClientUtil.sendHttpGet(url, null, getRequestHeader());
log.info("getConfig key: {}, response: {}", key, response);
List<Map<String, Object>> results = GsonUtil.convert2Object(response, new TypeToken<List<Map<String, Object>>>() {
}.getType());
if (CollectionUtil.isEmpty(results) || ObjectUtils.isEmpty(results.get(0).get("Value"))) {
return null;
}
return Base64Util.decoder(results.get(0).get("Value").toString());
} catch (Exception e) {
log.error("getConfig error: ", e);
}
return null;
}
public static boolean addConfig(String key, String value) {
try {
String url = KV_URL + key;
String response = HttpClientUtil.sendHttpPut(url, value, getRequestHeader());
log.info("addConfig key: {}, value: {}, response: {}", key, value, response);
return StringUtils.equals(response, "true");
} catch (Exception e) {
log.error("addConfig error: ", e);
}
return false;
}
public static boolean updateConfig(String key, String value) {
try {
String url = KV_URL + key;
String response = HttpClientUtil.sendHttpPut(url, value, getRequestHeader());
log.info("updateConfig key: {}, value: {}, response: {}", key, value, response);
return StringUtils.equals(response, "true");
} catch (Exception e) {
log.error("updateConfig error: ", e);
}
return false;
}
public static boolean deleteConfig(String key) {
try {
String url = KV_URL + key;
String response = HttpClientUtil.sendHttpDelete(url, getRequestHeader());
log.info("deleteConfig key: {}, response: {}", key, response);
return StringUtils.equals(response, "true");
} catch (Exception e) {
log.error("deleteConfig error: ", e);
}
return false;
}
public static void main(String[] args) {
// System.out.println(deleteConfig("test/long2"));
Map<Integer, Map<String, Double>> a = ConsulConfig.TEST_MAP2.defaultValueIfNull();
System.out.println(a);
}
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Type;
public class GsonUtil {
private static final Gson GSON = new GsonBuilder().enableComplexMapKeySerialization().create();
public static String dump2JsonString(Object object) {
return GSON.toJson(object);
}
public static Object convert2Object(String s) {
return GSON.fromJson(s, Object.class);
}
public static <T> T convert2Object(String s, Type type) {
return GSON.fromJson(s, type);
}
public static <T> T convert2Object(String s, Class<T> aclass) {
return GSON.fromJson(s, aclass);
}
public static <T> T deepCopy(T object, Type type) {
return convert2Object(dump2JsonString(object), type);
}
}