美文网首页
ApolloConfigUtils

ApolloConfigUtils

作者: 吕小凯 | 来源:发表于2019-06-27 14:10 被阅读0次

    ApolloConfigUtils工具类

    package com.redxun.apollo;
    
    import com.ctrip.framework.apollo.Config;
    import com.ctrip.framework.apollo.ConfigService;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    
    /**
     * 封装Apollo接收数据/校验用户身份等方法
     * @author lvxk 2019-06-28 14:10
     */
    public class ApolloConfigUtils {
    
        private static Config userconfig = null;
    
        /**
         * 判断用户权限
         *
         * @return
         */
        public static void checkUser(String environment, String username, String password) {
            userconfig = ConfigService.getConfig("user-" + environment);
            Set<String> propertyNames = userconfig.getPropertyNames();
            for (String s : propertyNames) {
                if (s.equals(username) && userconfig.getProperty(s, s).equals(password)) {
                    System.out.println("tips:用户校验通过,正在加载程序");
                    return;
                }
            }
            System.out.println("tips:用户校验失败,系统已停止运行,请输入正确的帐号密码重启项目");
            System.exit(-99);
        }
    
        public static void doApolloEnvironmentApi(String environment, String apolloUsername, String apolloPassword, String propertiesFile) {
            System.out.println("Config初始化中...");
            System.out.println("绑定数据至" + propertiesFile + "中");
            Map<String, String> map = initConfig(environment, apolloUsername,apolloPassword);
            PropertiesUtils.init(environment);
            PropertiesUtils.remove();
            Set<String> keys = map.keySet();
            for (String key : keys) {
                PropertiesUtils.update(key, map.get(key));
            }
        }
    
        public static Map<String, String> initConfig(String environment, String apolloUsername, String apolloPassword) {
            Map<String, String> map = new HashMap<String, String>();
            Config config = ConfigService.getConfig(environment);
            int count = 0;
            Set<String> propertyNames = config.getPropertyNames();
            //主服务器宕机或无法连接,连接从服务器
            if ("[]".equals(propertyNames.toString())) {
                String responseJsonString = HttpClientUtils.doGet(environment);
                map = JsonMapUtils.doJsonToMap(responseJsonString);
                System.out.println("主服务器连接失败,连接从服务器成功");
                return map;
            }
            //连接成功,从服务器校验用户权限
            ApolloConfigUtils.checkUser(environment, apolloUsername, apolloPassword);
            //权限通过遍历配置信息
            for (String s : propertyNames) {
                map.put(s, config.getProperty(s, s));
                System.out.println(s + "=" + config.getProperty(s, s));
                count++;
            }
            System.out.println("本次共计添加" + count + "条配置");
            return map;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:ApolloConfigUtils

          本文链接:https://www.haomeiwen.com/subject/mhiscctx.html