分布式服务器框架之Server.Common中通过Xml配置渠道、服务器集群、热更新信息代码解析Xml缓存进内存

简介: ChannelConfig.cs代码解析加载ChannelConfig.xml。使用了System.Xml.Linq的XDocument工具类来加载xml文件。其基本原理就是获取到Root节点下名为“Channel”的所有Element节点,然后使用迭代器循环,拿到每一个Element中的属性,先缓存ChannelEntity结构中,最后存到字典里。

服务器的配置分了三个文件:分别是ChannelConfig.xml、HotFixConfig.xml、ServerConfig.xml。


ChannelConfig.xml 标签中的属性        


ChannelId:            渠道Id


InnerVersion:        内部版本号


SourceVersion:     该渠道当前的资源版本号(根据当前的资源版本号和资源地址拼接出一个绝对路径)


SourceUrl:            资源站点的Ip和端口


RechargeUrl:        充值服务器的地址


PayServerNo:       充值服务器编号


TDAppId:              统计平台编号


IsOpenTD:            是否开启统计


Xml配置


<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <Channel ChannelId="0" InnerVersion="1001" SourceVersion="1.0.7" SourceUrl="http://127.0.0.1:8090" RechargeUrl="http://127.0.0.1" PayServerNo="" TDAppId="" IsOpenTD="" />
</Root>


ChannelConfig.cs代码解析加载ChannelConfig.xml。使用了System.Xml.Linq的XDocument工具类来加载xml文件。其基本原理就是获取到Root节点下名为“Channel”的所有Element节点,然后使用迭代器循环,拿到每一个Element中的属性,先缓存ChannelEntity结构中,最后存到字典里。


namespace Servers.Common
{
    public class ChannelConfig
    {
        public static Dictionary<string, ChannelEntity> m_dicChannel;
        //渠道数据初始化
        public static void Init()
        {
            m_dicChannel = new Dictionary<string, ChannelEntity>();
            string path = AppDomain.CurrentDomain.BaseDirectory + "Configs/ChannelConfig.xml";
            ChannelEntity entity;
            XDocument doc = XDocument.Load(path);
            XElement item;
            IEnumerator<XElement> iter = doc.Root.Elements("Channel").GetEnumerator();
            for (; iter.MoveNext();)
            {
                item = iter.Current;
                entity = new ChannelEntity();
                int.TryParse(item.Attribute("ChannelId").Value,out entity.ChannelId);
                int.TryParse(item.Attribute("InnerVersion").Value,out entity.InnerVersion);
                entity.SourceVersion = item.Attribute("SourceVersion").Value;
                entity.SourceUrl = item.Attribute("SourceUrl").Value;
                entity.RechargeUrl = item.Attribute("RechargeUrl").Value;
                int.TryParse(item.Attribute("PayServerNo").Value, out entity.InnerVersion);
                entity.TDAppId = item.Attribute("TDAppId").Value;
                bool.TryParse(item.Attribute("IsOpenTD").Value,out entity.IsOpenTD);
                //频道Id_内部版本
                m_dicChannel[entity.ChannelId + "_" + entity.InnerVersion] = entity;
            }
            Console.WriteLine(" ChannelConfig Load Complete");
        }
    }
}


HotFixConfig.xml中的属性

Key:Dll名字

Value:Dll文件名

Xml配置。热更新dll名称配置表格的原因是每次加载的时候都需要把名字往后+1,C#不允许加载相同名字的Dll


<?xml version="1.0" encoding="utf-8" ?>
<Root> 
  <Item  Key="HotFixAssemblyName"  Value="Servers.HotFix.dll"/>
</Root>


HotFixConfig.cs代码解析加载HotFixConfig.xml。


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
namespace Servers.Common
{
    //可以热更新的配置文件
    public class HotFixConfig
    {
        private static Dictionary<string, string> m_dicParams;
        //根据Key获取参数
        public static string GetParams(string key)
        {
            string value = null;
            m_dicParams.TryGetValue(key,out value);
            return value;
        }
        //加载热更新dll参数
        public static void Load()
        {
            //有可能会重复Load,所以每次Load需要Clear操作
            if (null == m_dicParams)
                m_dicParams = new Dictionary<string, string>();
            else 
                m_dicParams.Clear();
            //cs工程目录+Configs目录,拼接出来一个绝对目录
            string path = AppDomain.CurrentDomain.BaseDirectory + "Configs/HotFixConfig.xml";
            XElement item;
            XDocument doc = XDocument.Load(path);
            IEnumerator<XElement> iter = doc.Root.Elements("Item").GetEnumerator();
            for (; iter.MoveNext();)
            {
                item = iter.Current;
                string key = item.Attribute("Key").Value;
                string value = item.Attribute("Value").Value;
                m_dicParams[key] = value;
            }
            Console.WriteLine(" HotFixConfig Load Complete");
        }
    }
}


ServerConfig.xml 标签中的属性  


AreaServerId:                   区服编号      


CurrServerType:               当前服务器类型


CurrServerId:                    当前服务器编号


MongoConnectString:     MongoDB连接字符串


RedisConnectString:       Redis连接字符串


DataTablePath:                 数据表路径


Servers:                             服务器配置


Xml配置,这个是按照单服的结构设计的xml,如果以后有多服了,得把Servers、DB、Redis都得移到AreaServer里面去,外面套一层结构


<?xml version="1.0" encoding="utf-8" ?>
<Root>
  <AreaServerId>1</AreaServerId>
  <CurrServerType>0</CurrServerType>
  <CurrServerId>0</CurrServerId>
  <MongoConnectString>mongodb://127.0.0.1</MongoConnectString>
  <RedisConnectString>127.0.0.1:6379</RedisConnectString>
  <DataTablePath>C:\game\Server\Release\DataTable</DataTablePath>
  <Servers>
    <!--中心(世界)服务器-->>
    <Item ServerType="0" ServerId="1" Ip="10.0.24.10" Port="1301"/>
    <!--游戏服务器1-->>
    <Item ServerType="1" ServerId="1" Ip="10.0.24.10" Port="1302"/>
    <!--游戏服务器2-->>
    <Item ServerType="1" ServerId="2" Ip="10.0.24.10" Port="1303"/>
    <!--网关服务器1-->>
    <Item ServerType="2" ServerId="1" Ip="10.0.24.10" Port="1304"/>
    <!--网关服务器2-->>
    <Item ServerType="2" ServerId="2" Ip="10.0.24.10" Port="1305"/>
  </Servers>
  <Scenes>
    <!--这些json是啥?到时候写到寻路服务器的时候再看看吧!-->>
    <Item SceneId="1" ServerId="1" AOIJsonDataPath="" />
    <Item SceneId="2" ServerId="1" AOIJsonDataPath="" />
    <Item SceneId="3" ServerId="2" AOIJsonDataPath="" />
    <Item SceneId="4" ServerId="2" AOIJsonDataPath="" />
    <Item SceneId="5" ServerId="2" AOIJsonDataPath="" />
    <Item SceneId="6" ServerId="2" AOIJsonDataPath="" />
    <Item SceneId="7" ServerId="2" AOIJsonDataPath="" />
    <Item SceneId="8" ServerId="2" AOIJsonDataPath="" />
  </Scenes>
</Root>


ServerConfig.cs代码解析加载ServerConfig.xml。SceneConfig结构存的是哪些服务器应该为哪些场景提供服务器的信息,一个服务器可能算量有限并不能为整个大区的提供计算,将场景为单位把玩家划分到了不同的服务器上进行计算。Server类存的是单个服务器的配置,这个服务器需要开启的场景列表信息。ServerConfig则是有点像是传统的Manager类,会记录所有的Server信息到map结构里。


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using Servers.Core;
namespace Servers.Common
{
    public class SceneConfig
    {
        //场景编号
        public int SceneId;
        //服务器编号
        public int ServerId;
        //AOI区域数据路径
        public string AOIJsonDataPath;
    }
    //单台服务器配置
    public class Server
    {
        //服务器类型
        public ConstDefine.ServerType CurrServerType;
        //服务器编号
        public int ServerId;
        //服务器Ip
        public string Ip;
        //服务器端口
        public int Port;
        //这个服务器需要开启的场景列表(这个服务器为特定的一些场景提供服务,跳转场景则切另一个服务器为玩家服务)
        public List<SceneConfig> SceneConfigs { get; }
        public Server()
        {
            SceneConfigs = new List<SceneConfig>(100);
        }
    }
    public class ServerConfig
    {
        #region 静态成员
        //区服编号
        public static int AreaServerId;
        //当前服务器类型
        public static ConstDefine.ServerType CurrServerType;
        //当前服务器编号
        public static int CurrServerId = 0;
        //Mongo连接字符串
        public static string MongoConnectionString;
        //Redis连接字符串
        public static string RedisConnectionString;
        //数据表路径
        public static string DataTablePath;
        //区服内服务器列表
        private static List<Server> ServerList;
        //场景和游戏服务器的对应字典(哪个场景在哪个服务器上运算 SceneId:SceneConfig)
        public static Dictionary<int,SceneConfig> DicSceneInServer;
        //游戏服务DB名(一个区服务,一个DB??一个区服如果只使用一个DB,很多个服务同时像这一个DB写数据的话不会造成拥堵吗??)
        private static string m_GameServerDBName = null;
        public static string GameServerDBName
        {
            get
            {
                if (string.IsNullOrEmpty(m_GameServerDBName))
                    m_GameServerDBName = string.Format("GameServer_{0}", AreaServerId);
                return m_GameServerDBName;
            }
        }
        //角色HashKey(就是个Key,看代码好像是一个区服只能有一个Role啊,目前没看到哪里用)
        private static string m_RoleHashKey = null;
        private static string RoleHashKey
        {
            get
            {
                if (string.IsNullOrEmpty(m_RoleHashKey))
                    m_RoleHashKey = string.Format("{0}_RoleHash", AreaServerId);
                return m_RoleHashKey;
            }
        }
        //角色昵称Key(玩家名称)
        private static string m_RoleNickNameKey = null;
        public static string RoleNickNameKey
        {
            get 
            {
                if (string.IsNullOrEmpty(m_RoleNickNameKey))
                    m_RoleNickNameKey = string.Format("{0}_NickName", AreaServerId);
                return m_RoleNickNameKey;
            }
        }
        public static void Init()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "Configs/ServerConfig.xml";
            XDocument doc = XDocument.Load(path);
            XElement itemRoot = doc.Root;
            AreaServerId = itemRoot.Element("AreaServerId").Value.ToInt();
            CurrServerType = (ConstDefine.ServerType)itemRoot.Element("CurrServerType").Value.ToInt();
            CurrServerId = itemRoot.Element("CurrServerId").Value.ToInt();
            MongoConnectionString  = itemRoot.Element("MongoConnectString").Value;
            RedisConnectionString = itemRoot.Element("RedisConnectString").Value;
            DataTablePath = itemRoot.Element("DataTablePath").Value;
            ServerList = new List<Server>();
            DicSceneInServer = new Dictionary<int, SceneConfig>();
            //初始化服务器节点
            XElement itemServer;
            IEnumerator<XElement> iterServer = itemRoot.Element("Servers").Elements("Item").GetEnumerator();
            for (; iterServer.MoveNext();)
            {
                itemServer = iterServer.Current;
                ServerList.Add(new Server()
                {
                    CurrServerType = (ConstDefine.ServerType)itemServer.Attribute("ServerType").Value.ToInt(),
                    ServerId = itemServer.Attribute("ServerId").Value.ToInt(),
                    Ip = itemServer.Attribute("Ip").Value,
                    Port = itemServer.Attribute("Port").Value.ToInt()
                }) ;
            }
            //初始化场景对应的服务器
            XElement itemScene;
            SceneConfig sceneCfg;
            Server server;
            int sceneId;
            int serverId;
            string aoiJsonDataPath;
            IEnumerator<XElement> iterScene = itemRoot.Element("Scenes").Elements("Item").GetEnumerator();
            for (; iterScene.MoveNext();)
            {
                itemScene = iterScene.Current;
                sceneId = itemScene.Attribute("SceneId").Value.ToInt();
                serverId = itemScene.Attribute("ServerId").Value.ToInt();
                aoiJsonDataPath = itemScene.Attribute("AOIJsonDataPath").Value;
                sceneCfg = new SceneConfig()
                {
                    SceneId = sceneId,
                    ServerId = serverId,
                    AOIJsonDataPath = aoiJsonDataPath
                };
                //保存场景对应的服务器
                DicSceneInServer[sceneId] = sceneCfg;
                //获取对应的服务器,正向写入该服务器会服务哪些场景
                server = GetServer(ConstDefine.ServerType.GameServer, serverId);
                if (null != server)
                {
                    server.SceneConfigs.Add(sceneCfg);
                }
            }
            Console.WriteLine("ServerConfig Init Complete");
        }
        public static Server GetServer(ConstDefine.ServerType type,int serverId)
        {
            int iLen = ServerList.Count;
            Server server = null;
            for (int i = 0; i < iLen; ++i)
            {
                server = ServerList[i];
                if (server.CurrServerType == type && server.ServerId == serverId)
                    return server;
            }
            return null;
        }
        #endregion
    }
}


这个笔记写完啦!

相关文章
|
存储 缓存 网络协议
阿里云特惠云服务器99元与199元配置与性能和适用场景解析:高性价比之选
2025年,阿里云长效特惠活动继续推出两款极具吸引力的特惠云服务器套餐:99元1年的经济型e实例2核2G云服务器和199元1年的通用算力型u1实例2核4G云服务器。这两款云服务器不仅价格亲民,而且性能稳定可靠,为入门级用户和普通企业级用户提供了理想的选择。本文将对这两款云服务器进行深度剖析,包括配置介绍、实例规格、使用场景、性能表现以及购买策略等方面,帮助用户更好地了解这两款云服务器,以供参考和选择。
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
3422 1
|
存储 缓存 负载均衡
阿里云服务器实例选择指南:热门实例性能、适用场景解析对比参考
2025年,在阿里云的活动中,主售的云服务器实例规格除了轻量应用服务器之外,还有经济型e、通用算力型u1、计算型c8i、通用型g8i、计算型c7、计算型c8y、通用型g7、通用型g8y、内存型r7、内存型r8y等,以满足不同用户的需求。然而,面对众多实例规格,用户往往感到困惑,不知道如何选择。本文旨在全面解析阿里云服务器实例的各种类型,包括经济型、通用算力型、计算型、通用型和内存型等,以供参考和选择。
|
存储 域名解析 弹性计算
阿里云上云流程参考:云服务器+域名+备案+域名解析绑定,全流程图文详解
对于初次通过阿里云完成上云的企业和个人用户来说,很多用户不仅是需要选购云服务器,同时还需要注册域名以及完成备案和域名的解析相关流程,从而实现网站的上线。本文将以上云操作流程为核心,结合阿里云的活动政策与用户系统梳理云服务器选购、域名注册、备案申请及域名绑定四大关键环节,以供用户完成线上业务部署做出参考。
利用Private Zone DNS - 搭建AD但不搭建DNS服务器如何加域
利用Private Zone DNS - 搭建AD但不搭建DNS服务器如何加域
利用Private Zone DNS - 搭建AD但不搭建DNS服务器如何加域
|
XML 数据采集 API
用Lxml高效解析XML格式数据:以天气API为例
免费Python教程:实战解析中国天气网XML数据,详解Lxml库高效解析技巧、XPath用法、流式处理大文件及IP封禁应对策略,助你构建稳定数据采集系统。
527 0
|
机器学习/深度学习 存储 缓存
LLM高效推理:KV缓存与分页注意力机制深度解析
随着大型语言模型(LLM)规模和复杂性的增长,高效推理变得至关重要。KV缓存和分页注意力是优化LLM推理的两项关键技术。KV缓存通过存储键值对减少重复计算,而分页注意力则通过将序列分割成小块来降低内存消耗,从而有效处理长序列。本文深入剖析这些技术的工作原理及其在仅解码器模型中的应用,探讨其优势与挑战,并展示其实现示例。
1378 16
LLM高效推理:KV缓存与分页注意力机制深度解析
|
存储 机器学习/深度学习 应用服务中间件
阿里云服务器架构解析:从X86到高性能计算、异构计算等不同架构性能、适用场景及选择参考
当我们准备选购阿里云服务器时,阿里云提供了X86计算、ARM计算、GPU/FPGA/ASIC、弹性裸金属服务器以及高性能计算等多种架构,每种架构都有其独特的特点和适用场景。本文将详细解析这些架构的区别,探讨它们的主要特点和适用场景,并为用户提供选择云服务器架构的全面指南。
1488 18
|
XML JavaScript Android开发
【Android】网络技术知识总结之WebView,HttpURLConnection,OKHttp,XML的pull解析方式
本文总结了Android中几种常用的网络技术,包括WebView、HttpURLConnection、OKHttp和XML的Pull解析方式。每种技术都有其独特的特点和适用场景。理解并熟练运用这些技术,可以帮助开发者构建高效、可靠的网络应用程序。通过示例代码和详细解释,本文为开发者提供了实用的参考和指导。
738 15
|
存储 弹性计算 安全
阿里云服务器ECS通用型规格族解析:实例规格、性能基准与场景化应用指南
作为ECS产品矩阵中的核心序列,通用型规格族以均衡的计算、内存、网络和存储性能著称,覆盖从基础应用到高性能计算的广泛场景。通用型规格族属于独享型云服务器,实例采用固定CPU调度模式,实例的每个CPU绑定到一个物理CPU超线程,实例间无CPU资源争抢,实例计算性能稳定且有严格的SLA保证,在性能上会更加稳定,高负载情况下也不会出现资源争夺现象。本文将深度解析阿里云ECS通用型规格族的技术架构、实例规格特性、最新价格政策及典型应用场景,为云计算选型提供参考。

热门文章

最新文章

推荐镜像

更多
  • DNS