2017年4月

[运维]基于域名系统的外网WEB端口共享实现

基于域名反向代理WEB端口共享

Named + Nginx + Apache

环境搭建:

Centos 7 * 3;
Server 1 :
  HostName: DNS
  Software: named
  Ip Address: 192.168.4.8
Server 2:
  HostName: Nginx
  Software: Nginx
  Ip Address: 192.168.113.20
Server 3:
  HostName: Apache
  Software: Apache
  Ip Address: 192.168.113.18

相关配置:

Dns → Named: 使用 named 程序为Nginx 提供Dns 查询服务. 参考网址: http://www.linuxde.net/2011/11/2000.html
yum install bind cache-nameserver –y 安装完毕后对相关域名进行设置 配置文件地址: /etc/named.conf
初始化域名空间, 文件地址: /etc/named.zones
对域名空间初始化后在 named 资源文件夹建立域名解析配置文件编辑域名解析文件, 编辑完毕后可以使用named-chkzone zone filename 指令对zone 域名解析配置文件进行格式校验, 如果格式校验不通过, 在重启named 服务时将会报错. 报错的内容可以通过systemctl status named.service 指令查看相关的错误日志.
重启服务检查配置是否正确解析

Nginx -> nginx: 安装完毕后编辑配置文件 /etc/nginx/nginx.conf 通过添加 proxy_xx 等控制语句设置相关的代理属性. 上图为 nginx 的全局配置, 关于具体站点的相关私有代理属性以及域名的监听等设置在~/conf.d/ 目录下, 以.conf 后缀结尾, 可以通过上图的倒数第二行看出. 此配置文件包含站点的监听端口,代理目的服务器等, 编辑完毕后通过nginx –t 指令校验配置文件是否有错误以下为默认站点配置, 可以通过proxy_pass 语句看出, 代理目的地可以不只是域名, 也可以是相关的ip 甚至是不同的端口.
以下为非默认站点配置举例, proxy_pass 指向为一个域名地址, 此域名通过设置的dns 服务器, 也就是resolve 指定的192.168.4.53 解析为对应的IP 地址后转发至相关的Ip 端口, 实现代理转发的目的. Proxy_pass通过与域名服务器的结合, 相互搭配达到外网共享80 端口的规划. 在实际配置中, 如果主机默认的DNS 主机为Dns SVR 可不设置resolve. 或者在HOSTS 文件中指定了静态解析也不需要配置resolve 语句.

Apache → apache 安装完毕后, 根据安全手册配置好相关的容器安全配置, 然后添加监听对应域名的虚拟主机即可, 不需要做额外的配置去适应Nginx 的反向代理.
以下给出 Apache 虚拟主机配置参考模板:

[NodeJs][运维] 运维手册 - 第一章: 服务保障

<h2 style="margin-left:0cm;text-indent:0cm;"></h2><h2 style="margin-left:0cm;text-indent:0cm;"></h2><h2 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:宋体;">架构设计</span><span></span></h2><h3 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">  </span><span style="font-family:&quot;font-weight:normal;">NodeJs </span><span style="font-family:宋体;font-weight:normal;">的优势</span><span style="font-family:&quot;"></span></h3><p class="MsoNormal" align="left"> <span style="font-size:12.0pt;">         </span><span style="font-family:宋体;">事件型非阻塞异步高速处理架构引擎</span><span>. </span><span style="font-family:宋体;">具备单线程多进程处理能力</span><span>, </span><span style="font-family:宋体;">对于高并发的</span><span> URI </span><span style="font-family:宋体;">请求有良好的处理能力</span><span>. </span><span style="font-family:宋体;">常用于前端代理</span><span>, </span><span style="font-family:宋体;">前端解析</span><span>, </span><span style="font-family:宋体;">后台代理</span><span>, WEB </span><span style="font-family:宋体;">请求代理等数据处理并发量较大领域</span><span>. </span><span style="font-family:宋体;">同时因为较好的事件处理能力应用于包括但不限于</span><span>WEB/</span><span style="font-family:宋体;">大数据</span><span>/</span><span style="font-family:宋体;">嵌入式</span> <span style="font-family:宋体;">等方面</span><span>.</span></p><h3 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">  </span><span style="font-family:&quot;font-weight:normal;">NodeJs </span><span style="font-family:宋体;font-weight:normal;">代理结构简述</span></h3><p class="MsoNormal" align="left"> <span style="font-size:12.0pt;">         </span><span style="font-family:宋体;">相对于旧有的负载均衡体系</span><span>, </span><span style="font-family:宋体;">如</span><span> Nginx+Apache </span><span style="font-family:宋体;">反向代理架构</span><span>, NodeJs </span><span style="font-family:宋体;">具备针对性强</span><span>, </span><span style="font-family:宋体;">并发处理</span><span>, </span><span style="font-family:宋体;">异常反馈等优于</span><span> Nginx </span><span style="font-family:宋体;">等轻量化容器的处理能力</span><span>, </span><span style="font-family:宋体;">并且具备</span><span>Nginx </span><span style="font-family:宋体;">反向代理架构不具备的简易部署</span><span>,</span><span style="font-family:宋体;">方便架设</span><span>/</span><span style="font-family:宋体;">错误定位</span><span>/</span><span style="font-family:宋体;">二次开发等优点</span><span>. </span><span style="font-size:12.0pt;"></span></p><h3 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">  </span><span style="font-family:&quot;font-weight:normal;">NodeJs </span><span style="font-family:宋体;font-weight:normal;">代理架构设计</span><span style="font-family:&quot;"></span></h3><p> <span style="font-family:宋体;font-weight:normal;">
</span>
</p><p class="MsoNormal" align="left"> <span style="font-size:12.0pt;"></span></p><p class="MsoNormal" align="left"> <span style="font-size:12.0pt;">         </span><span style="font-family:宋体;">旧有的</span><span> MVC </span><span style="font-family:宋体;">等</span><span> WEB </span><span style="font-family:宋体;">架构基于一台</span><span> SVR, </span><span style="font-family:宋体;">数据服务以及功能应用在同一台</span><span> SVR </span><span style="font-family:宋体;">上</span><span>, </span><span style="font-family:宋体;">因为资源有限</span><span>, </span><span style="font-family:宋体;">在代码以及架构的优化上偏向于保守以及轻量化</span><span>, </span><span style="font-family:宋体;">此架构适用于中小型非门户论坛级别的公司或者个人博客页面</span><span>, </span><span style="font-family:宋体;">一旦当用户访问量增大</span><span>, </span><span style="font-family:宋体;">数据量达到百万级</span><span>, </span><span style="font-family:宋体;">并发达到</span><span> 5k+ </span><span style="font-family:宋体;">时</span><span>, </span><span style="font-family:宋体;">此架构会带来隐患</span><span>, </span><span style="font-family:宋体;">传统的</span><span> J2EE/ASP.NET/PHP </span><span style="font-family:宋体;">等架构服务器因为资源不足</span><span>, </span><span style="font-family:宋体;">服务器长期处于高负载状态</span><span>, </span><span style="font-family:宋体;">应用访问错误</span><span>, </span><span style="font-family:宋体;">资源响应缓慢等都可能导致客户流失</span><span>.  </span><span style="font-family:宋体;">在此基础上</span><span>, </span><span style="font-family:宋体;">虽然有</span><span>: </span><span style="font-family:宋体;">服务器集群</span><span>, </span><span style="font-family:宋体;">负载均衡</span><span>, </span><span style="font-family:宋体;">前后端分离等技术</span><span>, </span><span style="font-family:宋体;">但是也存在如</span><span>: </span><span style="font-family:宋体;">技术能力要求偏高</span><span>, </span><span style="font-family:宋体;">后期维护工作量巨大</span><span>, </span><span style="font-family:宋体;">故障定位复杂等问题</span><span>.</span></p><p class="MsoNormal" align="left"> <span>        NodeJs </span><span style="font-family:宋体;">的出现让基于</span> <span style="font-family:宋体;">前后端分离</span> <span style="font-family:宋体;">技术的</span><span> WEB </span><span style="font-family:宋体;">应用能够在高并发以及应用响应间取得较为可观的缓冲</span><span>, </span><span style="font-family:宋体;">并进一步降低了技术门槛以及部署和运维成本</span><span>. </span></p><p class="MsoNormal" align="left" style="text-indent:28.9pt;"> <span style="font-family:宋体;">在</span> <span>2014 </span><span style="font-family:宋体;">年</span><span> 4 </span><span style="font-family:宋体;">月</span><span>, </span><span style="font-family:宋体;">淘宝的前端团队提出的</span> <span>“</span><span style="font-family:宋体;">中途岛</span><span>” </span><span style="font-family:宋体;">架构</span><span>, </span><span style="font-family:宋体;">便是基于</span><span> NodeJs </span><span style="font-family:宋体;">代理的一种前端高并发前后端分离的服务架构</span><span>. </span><span style="font-family:宋体;">在此架构中</span><span>, </span><span style="font-family:宋体;">用户</span><span>(Client) </span><span style="font-family:宋体;">访问网站提供的基于</span><span> JS</span><span style="font-family:宋体;">架构纯前端</span><span> View</span><span style="font-family:宋体;">层</span><span>(UI Layer), </span><span style="font-family:宋体;">通过</span><span> Ajax</span><span style="font-family:宋体;">等交互控制与代理端</span><span>(NodeServer)</span><span style="font-family:宋体;">进行交互</span><span>, </span><span style="font-family:宋体;">而后代理端</span><span>(NS)</span><span style="font-family:宋体;">再与纯后端的服务端</span><span>(JavaServer)</span><span style="font-family:宋体;">进行数据交互后对数据进行呈现然后返回给</span><span>View</span><span style="font-family:宋体;">层</span><span>. </span><span style="font-family:宋体;">这种架构可以针对服务器资源进行高效的分配</span><span>, </span><span style="font-family:宋体;">并能够快速定位故障发生的位置层</span><span>. </span><span style="font-family:宋体;">方便运维及开发人员及时修复</span><span>.</span></p><p class="MsoNormal" align="left"> <span style="font-size:12.0pt;"></span><span style="font-size:12.0pt;"></span></p><h2 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:宋体;">高服务性</span><span></span></h2><p class="MsoNormal" style="margin-left:21.0pt;"> <span>  </span><span style="font-family:宋体;">应用需要可持续的提供有效服务</span><span>, </span><span style="font-family:宋体;">并尽量避免服务中断</span><span>. </span><span style="font-family:宋体;">当</span> <span>NodeJs </span><span style="font-family:宋体;">代码出现异常错误的时候程序往往会中断在异常发生的代码处</span><span>, </span><span style="font-family:宋体;">并杀死整个进程</span><span>, </span><span style="font-family:宋体;">这样将会导致服务不可用</span><span>. </span><span style="font-family:宋体;">所以需要尽量保障服务的正常运行</span><span>, </span><span style="font-family:宋体;">哪怕是代码运行出现异常</span><span>(</span><span style="font-family:宋体;">我们应该尽量确保自己的代码有足够的容错而不会导致程序中断</span><span>).</span></p><h3 style="margin-left:0cm;text-indent:0cm;"> <span>  </span><span style="font-family:宋体;">进程守护</span><span></span></h3><p class="MsoNormal" style="margin-left:21.0pt;"> <span>  </span><span style="font-family:宋体;">当我们使用一个</span> <span>Session </span><span style="font-family:宋体;">开启一个</span> <span>node </span><span style="font-family:宋体;">的进程</span><span>, </span><span style="font-family:宋体;">这个进程即会挂在到我们当前的</span> <span>Session </span><span style="font-family:宋体;">下</span><span>, </span><span style="font-family:宋体;">当</span> <span>Session </span><span style="font-family:宋体;">结束时</span><span>, </span><span style="font-family:宋体;">因为资源回收制度</span><span>, </span><span style="font-family:宋体;">挂在我们这个</span> <span>Session </span><span style="font-family:宋体;">下的所有进程都会被中断或者杀死</span><span>. </span><span style="font-family:宋体;">所以需要一个进程守护机制来保护我们的进程</span><span>, </span><span style="font-family:宋体;">当</span><span> Session </span><span style="font-family:宋体;">结束时不会被杀死</span><span>. </span><span style="font-family:宋体;">依旧能够持续提供服务</span><span>.</span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-size:12.0pt;line-height:156%;font-weight:normal;">            Nohup</span></h4><p class="MsoNormal" style="margin-left:21.0pt;"> <span>         </span><span>Nohup </span><span style="font-family:宋体;">是</span> <span>Linux </span><span style="font-family:宋体;">下的一个命令</span><span>, </span><span style="font-family:宋体;">可将应用进程运行于后台而不会中断</span><span>.</span><span></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-size:12.0pt;line-height:156%;font-family:&quot;font-weight:normal;">    Screen</span></h4><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-size:12.0pt;font-family:&quot;">   </span><span>Screen </span><span style="font-family:宋体;">是</span> <span>Linux </span><span style="font-family:宋体;">常用的一个进程守护程序</span><span>, </span><span style="font-family:宋体;">可以达到当用户</span> <span>Session </span><span style="font-family:宋体;">结束时保持进程活性的功能</span><span>, </span><span style="font-family:宋体;">从附图可以看到</span><span>, </span><span style="font-family:宋体;">甚至有些进程是四个月前开启的</span><span>.(</span><span style="font-family:宋体;">个人有写脚本判断服务是否死掉</span><span>, </span><span style="font-family:宋体;">死掉后会重启服务</span><span>)</span><span style="font-family:&quot;font-weight:normal;"></span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-size:12.0pt;"></span><span style="font-size:12.0pt;font-family:&quot;"></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-size:12.0pt;line-height:156%;font-family:&quot;font-weight:normal;">    Forever</span></h4><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-size:12.0pt;font-family:&quot;">   </span><span>Forever </span><span style="font-family:宋体;">可以做到当进程对应的文件修改时自动重启的功能</span><span>. </span><span style="font-family:宋体;">同时也能确保进程不会被</span> <span>Session </span><span style="font-family:宋体;">异常事件给中断</span><span>. </span><span style="font-family:宋体;">但同时也有配置困难等问题</span><span>.</span><span style="font-family:宋体;"></span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-size:12.0pt;"></span><span style="font-size:12.0pt;font-family:&quot;"></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-size:12.0pt;line-height:156%;font-family:&quot;font-weight:normal;">    Pm2</span></h4><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-family:宋体;">     经过大量工程师的努力现在已经不输于 <span>Forever </span>了<span>, </span>在集群以及大规模部署下比 <span>Forever </span>要方便不少<span>.</span></span><span style="font-family:宋体;font-weight:normal;"></span></p><p class="MsoNormal" align="left" style="margin-left:21pt;"> <span style="font-size:12.0pt;"></span><span style="font-size:12.0pt;font-family:&quot;"></span></p><h3 style="margin-left:0cm;text-indent:0cm;"> <span>  </span><span style="font-family:宋体;">多进程代理</span><span></span></h3><p class="MsoNormal"> <span></span><span></span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-family:宋体;">    当一个进程因为异常事件不得不关闭的时候</span><span>, </span><span style="font-family:宋体;">服务将会处于不可用状态</span><span>, </span><span style="font-family:宋体;">如何确保当进程因为异常退出时能够自动恢复</span><span>, </span><span style="font-family:宋体;">是首要</span><span>. </span><span style="font-family:宋体;">并能够对资源的利用达到最大化</span><span>, </span><span style="font-family:宋体;">改善</span> <span>Node </span><span style="font-family:宋体;">本身的缺点提高性能也是重点</span><span>.<span style="font-family:&quot;font-weight:normal;"></span></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-size:12.0pt;line-height:156%;font-family:&quot;font-weight:normal;">    Cluster</span></h4><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-size:12.0pt;font-family:&quot;">   </span><span style="font-family:宋体;">通过 <span>cluster </span>能够建立多个基于同一<span> Socket </span>的进程来对数据进行处理<span>, </span>善用多核机制<span>.</span></span><span style="font-family:宋体;font-weight:normal;"></span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-family:宋体;"></span><span style="font-family:宋体;font-weight:normal;"></span></p><p class="MsoNormal" style="margin-left:21.0pt;text-indent:21.0pt;"> <span style="font-family:宋体;">Cluster </span><span style="font-family:宋体;">能够将请求根据系统负载分配到空闲的进程上对请求进行处理<span>, </span>但是有个不足的情况是<span>, </span>在不涉及 <span>IO </span>操作下<span>,</span>当连接为<span> “</span>短连接<span>”</span>时将会导致 <span>QPS </span>以及可用率的下降<span>, </span>但是也能保持<span> QPS </span>在<span> 3K+, </span>利用率在<span> 99%/10W </span>中<span>. </span>所以尽量在建立连接时使用 <span>kepp-alive </span>方式<span>.</span></span><span style="font-family:宋体;font-weight:normal;"></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">    </span><span style="font-family:宋体;font-weight:normal;">子进程代理</span><span style="font-family:&quot;font-weight:normal;"></span></h4><p class="MsoNormal" style="margin-left:21.0pt;"> <span style="font-size:12.0pt;font-family:&quot;">  </span><span style="font-family:宋体;">模拟</span><span style="font-family:&quot;"> <span>Cluster </span></span><span style="font-family:宋体;">建立子进程</span><span style="font-family:&quot;">, </span><span style="font-family:宋体;">主要业务使用放置于子进程中</span><span style="font-family:&quot;">, </span><span style="font-family:宋体;">使用父进程对子进程进行监控</span><span style="font-family:&quot;">, </span><span style="font-family:宋体;">当子进程因为业务异常中断时自动重启子进程</span><span style="font-family:&quot;">. </span><span style="font-family:宋体;">确保业务的可持续利用</span><span style="font-family:&quot;">.</span><span style="font-family:&quot;font-weight:normal;"></span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span></span><span></span></p><h3 style="margin-left:0cm;text-indent:0cm;"> <span>  </span><span style="font-family:宋体;">异常触发</span><span></span></h3><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">    Try..catch..</span><span style="font-family:宋体;font-weight:normal;">语句</span><span style="font-family:&quot;font-weight:normal;"></span></h4><p class="MsoNormal"> <span style="font-family:&quot;">      </span><span style="font-family:宋体;">在开发中善于使用</span><span style="font-family:&quot;"> <span>try catch </span></span><span style="font-family:宋体;">语句能够将大部分的异常事件处理掉</span><span style="font-family:&quot;">, </span><span style="font-family:宋体;">但是</span><span style="font-family:&quot;"> try </span><span style="font-family:宋体;">语句有个问题</span><span style="font-family:&quot;">, </span><span style="font-family:宋体;">那就是当异常问题处于</span><span style="font-family:&quot;"> <span>callback </span></span><span style="font-family:宋体;">函数中</span><span style="font-family:&quot;">, catch </span><span style="font-family:宋体;">语句并不能够捕获到异常信息</span><span style="font-family:&quot;">. </span><span style="font-family:宋体;">将会导致程序中断</span><span style="font-family:&quot;">.</span><span style="font-family:&quot;font-weight:normal;"></span></p><p class="MsoNormal"> <span style="font-family:&quot;">         </span><span></span><span style="font-family:&quot;font-weight:normal;"></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">    </span><span style="font-family:宋体;font-weight:normal;">使用</span><span style="font-family:&quot;font-weight:normal;"> <span>Domain </span></span><span style="font-family:宋体;font-weight:normal;">捕获</span><span style="font-family:&quot;font-weight:normal;">Callback </span><span style="font-family:宋体;font-weight:normal;">类型异常</span><span style="font-family:&quot;font-weight:normal;"></span></h4><p class="MsoNormal"> <span style="font-family:&quot;">      </span><span style="font-family:宋体;">进程抛出了异常</span><span>, </span><span style="font-family:宋体;">没有被任何的</span><span style="font-size:12pt;font-family:Consolas;background:#FCFAFA;">try catch</span><span style="font-family:宋体;">捕获到</span><span>, </span><span style="font-family:宋体;">这时候将会触发整个</span><span>process</span><span style="font-family:宋体;">的</span><span style="font-size:12pt;font-family:Consolas;background:#FCFAFA;">processFatal</span><span>, </span><span style="font-family:宋体;">此时如果在</span><span>domain</span><span style="font-family:宋体;">包裹之中</span><span>, </span><span style="font-family:宋体;">将会在</span><span>domain</span><span style="font-family:宋体;">上触发</span><span style="font-size:12pt;font-family:Consolas;background:#FCFAFA;">error</span><span style="font-family:宋体;">事件</span><span>,</span><span style="font-family:宋体;">反之将会在</span><span>process</span><span style="font-family:宋体;">上触发</span><span style="font-size:12pt;font-family:Consolas;background:#FCFAFA;">uncaughtException</span><span style="font-family:宋体;">事件</span><span>.</span></p><p class="MsoNormal"> <span></span><span style="font-family:Calibri, sans-serif;font-weight:normal;"></span></p><h3 style="margin-left:0cm;text-indent:0cm;"> <span>  </span><span style="font-family:宋体;">心跳检查</span><span></span></h3><p class="MsoNormal" style="margin-left:21.0pt;text-indent:10.5pt;"> <span style="font-family:宋体;">服务上线期间</span><span>, </span><span style="font-family:宋体;">服务是否可用不能依靠用户反馈</span><span>, </span><span style="font-family:宋体;">当用户察觉服务异常时</span><span>, </span><span style="font-family:宋体;">服务可能已经处于崩溃多时的状态</span><span>. </span><span style="font-family:宋体;">所以对服务是否可用的状态监测是非常重要的</span><span>. </span><span style="font-family:宋体;">可以使用脚本定期访问判断的形式来检查服务是否在线</span><span>.</span></p><h2 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:宋体;">异常处理</span><span></span></h2><p class="MsoNormal" align="left"> <span style="font-family:宋体;">    一旦遇到异常事件<span>, </span>首先需要记录异常日志<span>, </span>对异常的触发条件<span>, </span>处理过程<span>, </span>异常模块<span>, </span>输出内容进行捕获<span>,</span>交付日志处理系统对异常进行下一步处理<span>.</span></span></p><h3> <span style="font-family:&quot;font-weight:normal;">  </span><span style="font-family:宋体;font-weight:normal;">异常捕获</span><span style="font-family:&quot;"></span></h3><p class="MsoNormal" style="text-indent:21.0pt;"> <span style="font-family:宋体;">通常在程序执行过程中<span>, </span>一旦遇到异常事件可能导致程序中断或者异常退出<span>,</span>直接返回异常数据<span>, </span>在<span> WEB </span>事件中<span>, </span>当返回一个异常<span>, </span>可能导致前端处理错误<span>,</span>用户交互出错等不友好行为<span>. </span>通过在<span> Node </span>的入口文件添加异常处理模块<span>, </span>将异常事件捕获<span>, </span>并通过日志的形式保存异常事件详细信息<span>, </span>为开发人员提供修复参考</span><span style="font-size:12.0pt;font-family:宋体;">.</span><span></span></p><p class="MsoNormal" align="center" style="text-align:center;"> <span style="font-size:12.0pt;font-family:宋体;"></span><span style="font-size:12.0pt;font-family:宋体;"></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">    </span><span style="font-family:宋体;font-weight:normal;">异常信息的判断</span><span style="font-family:&quot;font-weight:normal;"></span></h4><p class="MsoNormal" style="margin-left:21.0pt;"> <span>         </span><span style="font-family:宋体;">一般来说</span><span>, </span><span style="font-family:宋体;">异常指的是程序运行出错</span><span>, </span><span style="font-family:宋体;">如常见的</span> <span>Error,</span><span style="font-family:宋体;">溢出</span> <span style="font-family:宋体;">等将会导致程序崩溃的</span><span> BUG/</span><span style="font-family:宋体;">错误</span> <span style="font-family:宋体;">诸如此类的警告信息</span><span>. </span><span style="font-family:宋体;">但是在正常的应用服务过程中</span><span>, </span><span style="font-family:宋体;">我们要关心的不单单只是这些错误信息</span><span>, </span><span style="font-family:宋体;">这些会造成服务中断的</span><span> WRONG </span><span style="font-family:宋体;">等级是非常之高的</span><span>. </span><span style="font-family:宋体;">我们在开发以及运维过程中应当尽量避免这些错误的出现</span><span>, </span><span style="font-family:宋体;">将他们的出现的概率降低到最小</span><span>. </span><span style="font-family:宋体;">同时我们也要收集一些看起来不太重要的信息</span><span>, </span><span style="font-family:宋体;">用来统计</span><span>, </span><span style="font-family:宋体;">分析</span><span>, </span><span style="font-family:宋体;">将错误尽量的解决在发生之前</span><span>.</span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span>          </span><span style="font-family:宋体;">首先是对异常信息的分级制度</span><span>, </span><span style="font-family:宋体;">但是找了几个搜索引擎</span><span>, </span><span style="font-family:宋体;">看到的都是软件测试中所定义的错误分级</span><span>. </span><span style="font-family:宋体;">对于程序内部产生的日志信息分级</span><span>, </span><span style="font-family:宋体;">则是没见到有公开的标准</span><span>. </span><span style="font-family:宋体;">虽然像</span><span> Apache, Nginx </span><span style="font-family:宋体;">等具备如</span> <span>Debug, Info, Warn, Error, Fatal </span><span style="font-family:宋体;">此类的日志等级</span><span>, </span><span style="font-family:宋体;">但是在我们的程序运行过程中</span><span>, </span><span style="font-family:宋体;">是没有的</span><span>, </span><span style="font-family:宋体;">需要我们自己来对输出的日志进行定义</span><span>, </span><span style="font-family:宋体;">并将其输出</span><span>.</span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span>         </span><span style="font-family:宋体;">如</span><span>: Java </span><span style="font-family:宋体;">的异常参考</span><span></span></p><p class="MsoNormal" style="margin-left:21.0pt;"> <span></span><span></span></p><h4 style="margin-left:0cm;text-indent:0cm;"> <span style="font-family:&quot;font-weight:normal;">    </span><span style="font-family:宋体;font-weight:normal;">异常信息的存储</span><span style="font-weight:normal;"></span></h4><p class="MsoNormal" align="left" style="margin-left:21pt;"> <span style="font-size:12.0pt;font-family:宋体;">   </span><span style="font-family:宋体;">异常信息产生之后</span><span>, </span><span style="font-family:宋体;">我们需要将其输出</span><span>, </span><span style="font-family:宋体;">并妥善的保存起来</span><span>, </span><span style="font-family:宋体;">为开发人员后续的修复工作提供可供分析的有效参考</span><span>. </span><span style="font-family:宋体;">所以异常信息的存储</span><span>, </span><span style="font-family:宋体;">是很有必要的</span><span>.</span></p><p class="MsoNormal" align="left" style="margin-left:21pt;"> <span>          </span><span style="font-family:宋体;">通常来说异常信息都是直接输出到控制台中</span><span>, </span><span style="font-family:宋体;">也有写入文件的</span><span>, </span><span style="font-family:宋体;">也有同时输出在控制台和写入文件的</span><span>. </span><span style="font-family:宋体;">或者是在线存储</span><span>, </span><span style="font-family:宋体;">通过反馈机制将异常信息通过</span> <span>Ajax </span><span style="font-family:宋体;">等操作将异常信息回传给日志服务器统一存储</span><span>.</span><span style="font-size:12.0pt;font-family:宋体;"></span></p><p class="MsoNormal" align="left"> <span style="font-size:12.0pt;font-family:宋体;">    </span><span style="font-family:宋体;">这将在第二章中详细说明</span><span></span></p>