示例#1
0
 /// <summary>
 /// 获得配置中心唯一实例
 /// </summary>
 /// <returns></returns>
 public static ConfigMangerHelper GetInstance(string configkey)
 {
     if (_sigleconfig == null)
     {
         lock (_singletonlock)
         {
             if (_sigleconfig == null)
             {
                 if (string.IsNullOrEmpty(CommonConfig.ProjectName) || CommonConfig.ProjectName == "未命名项目")
                 {
                     LogHelper.WriteError("请选择请在web.config或AppSettings.config中配置ProjectName");
                     throw new Exception("请选择请在web.config或AppSettings.config中配置ProjectName");
                 }
                 if (string.IsNullOrEmpty(CommonConfig.ConfigManagerConnectString))
                 {
                     LogHelper.WriteError("请在web.config或AppSettings.config中配置ConfigRedisConnectString");
                     throw new Exception("请在web.config或AppSettings.config中配置ConfigRedisConnectString");
                 }
                 SystemConfigDal redisdal    = new SystemConfigDal();
                 string          redisserver = "";
                 redisserver = redisdal.GetRedisServer(CommonConfig.ConfigManagerConnectString).ConfigValue;
                 AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;
                 AppDomain.CurrentDomain.ProcessExit  += CurrentDomain_ProcessExit;
                 ConfigContext context = new ConfigContext();
                 context.ConfigParams = new ConfigParams()
                 {
                     ProjectName = CommonConfig.ProjectName,
                     ConfigManagerConnectString = CommonConfig.ConfigManagerConnectString,
                     RedisServer = redisserver
                 };
                 AppDomainContext.Context = context;
                 ConfigHeartbeatProtect.Instance().LoadConfig(true);
                 _sigleconfig = new ConfigMangerHelper();
             }
         }
     }
     return(_sigleconfig);
 }
        /// <summary>
        /// 是否重新下载项目信息
        /// </summary>
        /// <param name="updatejson">是否重新下载配置分类</param>
        public void LoadConfig(bool isload)
        {
            lock (_lockconfig)
            {
                string jasonpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "json", "json.txt");
                try
                {
                    bool     isupdatelocal = false;
                    DateTime updatetime    = DateTime.Now;

                    if (isload)
                    {
                        SystemConfigDal scdal = new SystemConfigDal();
                        SystemConfig    sc    = scdal.GetRedisServer(AppDomainContext.Context.ConfigParams.ConfigManagerConnectString);
                        if (sc != null)
                        {
                            AppDomainContext.Context.ConfigParams.RedisServer = sc.ConfigValue;
                        }

                        //更新项目信息
                        ProjectDal prodal = new ProjectDal();
                        AppDomainContext.Context.ProjectModel = prodal.GetByName(AppDomainContext.Context.ConfigParams.ConfigManagerConnectString, AppDomainContext.Context.ConfigParams.ProjectName);

                        //更新分类信息
                        CategoryDal catedal = new CategoryDal();
                        AppDomainContext.Context.CategoryModels = catedal.GetListByIds(AppDomainContext.Context.ConfigParams.ConfigManagerConnectString, AppDomainContext.Context.ProjectModel.CategoryIds);
                        long[] cids = AppDomainContext.Context.CategoryModels.Select(p => p.Id).ToArray();
                        //更新配置信息
                        ConfigDal          configdal    = new ConfigDal();
                        List <Config>      configs      = configdal.GetListByCategoryIds(AppDomainContext.Context.ConfigParams.ConfigManagerConnectString, cids, updatetime);
                        List <ConfigModel> configmodels = ToConfigModel(configs);

                        ConfigInfoOfKeyDic dic = new ConfigInfoOfKeyDic();
                        foreach (var item in configmodels)
                        {
                            dic.SetConfig(item);
                        }
                        AppDomainContext.Context.ConfigInfoOfKeyDic = dic;

                        if (configs.Count > 0)
                        {
                            configdal.SetUpdatetime(AppDomainContext.Context.ConfigParams.ConfigManagerConnectString, cids, updatetime);
                            isupdatelocal = true;
                        }
                    }
                    else
                    {
                        ConfigDal configdal = new ConfigDal();
                        long[]    cids      = AppDomainContext.Context.CategoryModels.Select(p => p.Id).ToArray();

                        List <Config>      configs      = configdal.GetListByCategoryIds(AppDomainContext.Context.ConfigParams.ConfigManagerConnectString, cids, updatetime);
                        List <ConfigModel> configmodels = ToConfigModel(configs);
                        foreach (var item in configmodels)
                        {
                            AppDomainContext.Context.ConfigInfoOfKeyDic.SetConfig(item);
                        }
                        if (configs.Count > 0)  //本次有配置更新
                        {
                            configdal.SetUpdatetime(AppDomainContext.Context.ConfigParams.ConfigManagerConnectString, cids, updatetime);
                            isupdatelocal = true;
                        }
                    }

                    if (isupdatelocal)
                    {
                        string json1 = JsonConvert.SerializeObject(AppDomainContext.Context);
                        IOHelper.Write(jasonpath, json1);  //写入磁盘
                        LogHelper.WriteInfo("写入磁盘成功");
                    }
                }
                catch (Exception)
                {
                    string json2 = IOHelper.Read(jasonpath);
                    JsonConvert.DeserializeObject <ConfigContext>(json2);   //从磁盘获得上次正确的配置
                    LogHelper.WriteInfo("从磁盘读取配置");
                }
            }
        }