Beispiel #1
0
        // private static readonly string localConfig = "<RedisConfig><Node NodeName=\"default\" IsMasterSlave=\"false\"><Item pool = \"0\" IsMaster=\"false\" server=\"{0}\" port=\"{1}\" allowadmin=\"true\" connectTimeout=\"0\" ssl=\"false\" pwd=\"\"  /></Node></RedisConfig>";

        /// <summary>
        /// 初始化服务器配置
        /// </summary>
        /// <returns>服务器配置列表</returns>
        // public static Dictionary<string,RedisNode> InitlizeServerList(XElement root)
        // {
        //    if (root != null && root.Element("RedisConfig") != null &&
        //        root.Element("RedisConfig").Elements("Node") != null)
        //    {
        //        IEnumerable nodes =
        //            from em in root.Element("RedisConfig").Elements("Node")
        //            select em;
        //        foreach (XElement it in nodes)
        //        {
        //            if (it.Elements("Item") == null)
        //            {
        //                continue;
        //            }
        //            RedisNode node = new RedisNode()
        //            {
        //                NodeName = it.Attribute("NodeName").Value.ToLower(),
        //                IsMasterSlave = it.Attribute("IsMasterSlave").Value == "true" ? true : false,
        //            };
        //            if (redisNodes.ContainsKey(node.NodeName))
        //            {
        //                Log.GrantLogTextWriter.Write(new Exception(string.Format("不能配置业务节点同名的redis服务器,{0}节点的配置将被忽略", node.NodeName)));
        //                continue;
        //            }
        //            redisNodes[node.NodeName] = node;
        //            IEnumerable srvs = from sr in it.Elements("Item")
        //                               select sr;
        //            var servers = new List<RedisServer>();
        //            foreach (XElement s in srvs)
        //            {
        //                var srv = new RedisServer
        //                {
        //                    Node = node,
        //                    IsMaster = s.Attribute("IsMaster").Value == "true" ? true : false,
        //                    Pool = int.Parse(s.Attribute("pool").Value),
        //                    ConnectTimeout = int.Parse(s.Attribute("connectTimeout").Value),
        //                    Server = s.Attribute("server").Value,
        //                    Port = int.Parse(s.Attribute("port").Value),
        //                    AllowAdmin = s.Attribute("allowadmin").Value == "true" ? true : false,
        //                    Ssl = s.Attribute("ssl").Value == "true" ? true : false,
        //                    Pwd = s.Attribute("pwd").Value,
        //                };
        //                if (node.IsMasterSlave && srv.IsMaster)
        //                {
        //                    RedisConnectionManager.GetConnection(srv); // 提前预初始化
        //                    node.MasterServer = srv; // 如果配置了多个主,这里只会把最后一个做为主
        //                }
        //                else
        //                {
        //                    servers.Add(srv); // 如果是非主从模式,将所有的都加到从上,没有主次
        //                    RedisConnectionManager.GetConnection(srv);
        //                }
        //            }
        //            node.SlaveServers = servers.ToArray();
        //        }
        //        return redisNodes;
        //    }
        //    throw new Exception("无法解析RedisConfig配置");
        //    // else
        //    //    Log.LogEx.LogError("redis clinet config is null");
        // }
        public static Dictionary <string, RedisNode> InitlizeServerList(Config.RedisConfig config)
        {
            if (config != null && config.Nodes != null && config.Nodes.Count > 0)
            {
                foreach (Config.RedisNode it in config.Nodes)
                {
                    RedisNode node = new RedisNode()
                    {
                        NodeName      = it.NodeName.ToLower(),
                        IsMasterSlave = it.IsMasterSlave,
                    };

                    // 这里的初始化方法需要满足 重复调用. 所以注释掉这句, 覆盖原来的配置
                    //if (redisNodes.ContainsKey(node.NodeName))
                    //{
                    //    Log.GrantLogTextWriter.Write(new Exception(string.Format("不能配置业务节点同名的redis服务器,{0}节点的配置将被忽略", node.NodeName)));
                    //    continue;
                    //}
                    redisNodes[node.NodeName] = node;
                    var servers = new List <RedisServer>();
                    foreach (Config.RedisItem sItem in it.Items)
                    {
                        var srv = new RedisServer
                        {
                            Node           = node,
                            IsMaster       = sItem.IsMaster,
                            Pool           = sItem.Pool,
                            ConnectTimeout = sItem.ConnectTimeout,
                            Server         = sItem.Server,
                            Port           = sItem.Port,
                            AllowAdmin     = sItem.AllowAdmin,
                            Ssl            = sItem.Ssl,
                            Ssl2           = sItem.Ssl2,
                            Pwd            = sItem.Pwd,
                            DbIndex        = sItem.DbIndex,
                        };
                        if (node.IsMasterSlave && srv.IsMaster)
                        {
                            RedisConnectionManager.GetConnection(srv); // 提前预初始化
                            node.MasterServer = srv;                   // 如果配置了多个主,这里只会把最后一个做为主
                        }
                        else
                        {
                            servers.Add(srv); // 如果是非主从模式,将所有的都加到从上,没有主次
                            RedisConnectionManager.GetConnection(srv);
                        }
                    }

                    node.SlaveServers = servers.ToArray();
                }

                return(redisNodes);
            }

            throw new Exception("无法解析RedisConfig配置");
        }
Beispiel #2
0
        /// <summary>
        /// 在用之前需要初始化,这里只所以不做成单例的原因是不想做锁判断
        /// </summary>
        // [InitlizeMethod]
        internal static void Initlize(Config.RedisConfig redisConfig)
        {
            if (redisConfig == null)
            {
                return;
            }
            Redis.RedisConfig.Initlize(redisConfig);
            instance = new RedisCache();

            ResourceCache.Initlize(); // 初始化ResourceCache
        }
Beispiel #3
0
 /// <summary>
 /// 初始化redis集群的节点
 /// </summary>
 public static void Initlize(Config.RedisConfig config)
 {
     try
     {
         readerWriterLock.AcquireWriterLock(800);
         InitlizeServerList(config);
     }
     catch (Exception ex)
     {
         logger.LogError(ex, "GrantMicroService.Redis.redisConfig.Initlize error");
         throw ex;
     }
     finally
     {
         if (readerWriterLock.IsWriterLockHeld)
         {
             readerWriterLock.ReleaseWriterLock();
         }
     }
 }