每次修改配置还得重启,感觉很麻烦
卤主现在用的是properties,redis做配置,因为没有用到mysql等关系型数据库
贴代码
全局Application Config 代码
package com.shield.device;import java.util.HashMap;import java.util.Map;import com.shield.cache.CacheKey;import com.shield.cache.CacheService;import com.shield.cache.springredis.RedisService;import com.shield.springcontext.ContextUtils;import com.shield.springcontext.CustomizedPropertyConfigurer;import com.shield.util.JsonUtil;public class AppConfig {private static final AppConfig appconfig = new AppConfig() ;private CacheService redisService ;private final Mapcache = new HashMap ();private AppConfig(){redisService = ContextUtils.getBean(RedisService.class);}public static boolean getBoolean(String key) {Object o = getObject(key);if(o instanceof Boolean){return (Boolean)o ;}return Boolean.parseBoolean(key);}public static String getString(String key){Object o = getObject(key);if(o instanceof String){return (String)o ;}return o.toString();}public static float getFloat(String key) {Object o = getObject(key);if(o instanceof Float){return (float)o ;}if(o instanceof String){return Float.parseFloat((String)o) ;}return Float.parseFloat(o.toString());}public static int getInt(String key){Object o = getObject(key);if(o instanceof Integer){return (int)o ;}if(o instanceof String){return Integer.parseInt((String)o) ;}return Integer.parseInt(o.toString());}public static void put(String key, String value) {appconfig.redisService.setHashValue(CacheKey.APP_CONFIG, key, value);appconfig.cache.put(key, value);}public static String cacheToString() {return JsonUtil.objectToJson(appconfig.cache);}public static String contextPropertyToString() {return JsonUtil.objectToJson(CustomizedPropertyConfigurer.getContextPropertyMap());}public static Object getObject(String key){Object o = appconfig.cache.get(key);if(o == null){o = appconfig.redisService.getHashValue(CacheKey.APP_CONFIG, key);if(o != null){appconfig.cache.put(key, o);}}if(o == null){o = CustomizedPropertyConfigurer.getContextProperty(key);if(o != null){appconfig.cache.put(key, o);}}if(o == null){throw new RuntimeException("Properties key: "+key +" value is empty, please check it.");}return o;}public static void flushAll(){appconfig.cache.clear();}}
flushAll就是清除掉数据,比如redis的配置修改了。
推荐在http访问时候调用
如:
curl localhost:8080/config/reload