﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-我希望你是我独家记忆-随笔分类-WEB</title><link>http://www.cppblog.com/Hero/category/21144.html</link><description> 一段永远封存的记忆，随风而去</description><language>zh-cn</language><lastBuildDate>Thu, 14 Apr 2016 21:31:47 GMT</lastBuildDate><pubDate>Thu, 14 Apr 2016 21:31:47 GMT</pubDate><ttl>60</ttl><item><title>【转载】浅谈JavaScript的事件响应</title><link>http://www.cppblog.com/Hero/archive/2016/04/14/213274.html</link><dc:creator>Hero</dc:creator><author>Hero</author><pubDate>Thu, 14 Apr 2016 03:12:00 GMT</pubDate><guid>http://www.cppblog.com/Hero/archive/2016/04/14/213274.html</guid><wfw:comment>http://www.cppblog.com/Hero/comments/213274.html</wfw:comment><comments>http://www.cppblog.com/Hero/archive/2016/04/14/213274.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Hero/comments/commentRss/213274.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Hero/services/trackbacks/213274.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 每当猴子们问我JavaScript和DOM里啥东西最牛逼时，我都会一巴掌打回去：卧槽还用问么当然是事件响应了啊！没它你能有时间和我讨论这个？你早去工地搬砖去了好么！浏览器没有事件响应就没有行为交互，那简直就是一夜回到解放前的感觉啊。此外，以事件驱动使得功能解耦也是个相当高端大气的技巧了，嘛，以此为主的Node.js&nbsp;现在可是风生水起的说。那现在我们就再抠抠事件监听的相关基础，让大家在心情...&nbsp;&nbsp;<a href='http://www.cppblog.com/Hero/archive/2016/04/14/213274.html'>阅读全文</a><img src ="http://www.cppblog.com/Hero/aggbug/213274.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Hero/" target="_blank">Hero</a> 2016-04-14 11:12 <a href="http://www.cppblog.com/Hero/archive/2016/04/14/213274.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【转载】负载均衡一（四层和七层负载均衡的区别）</title><link>http://www.cppblog.com/Hero/archive/2016/04/12/213250.html</link><dc:creator>Hero</dc:creator><author>Hero</author><pubDate>Tue, 12 Apr 2016 13:02:00 GMT</pubDate><guid>http://www.cppblog.com/Hero/archive/2016/04/12/213250.html</guid><wfw:comment>http://www.cppblog.com/Hero/comments/213250.html</wfw:comment><comments>http://www.cppblog.com/Hero/archive/2016/04/12/213250.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Hero/comments/commentRss/213250.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Hero/services/trackbacks/213250.html</trackback:ping><description><![CDATA[<p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">（一）</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　</strong>简单理解四层和七层负载均衡:</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　</strong><strong style="margin: 0px; padding: 0px; color: #333333;">&#9312;&nbsp;</strong>所谓四层就是基于IP+端口的负载均衡；七层就是基于URL等应用层信息的负载均衡；同理，还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡。 换句换说，二层负载均衡会通过一个虚拟MAC地址接收请求，然后再分配到真实的MAC地址；三层负载均衡会通过一个虚拟IP地址接收请求，然后再分配到真实的IP地址；四层通过虚拟IP+端口接收请求，然后再分配到真实的服务器；七层通过虚拟的URL或主机名接收请求，然后再分配到真实的服务器。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　<strong style="margin: 0px; padding: 0px; color: #333333;">&#9313;&nbsp;</strong>所谓的四到七层负载均衡，就是在对后台的服务器进行负载均衡时，依据四层的信息或七层的信息来决定怎么样转发流量。 比如四层的负载均衡，就是通过发布三层的IP地址（VIP），然后加四层的端口号，来决定哪些流量需要做负载均衡，对需要处理的流量进行NAT处理，转发至后台服务器，并记录下这个TCP或者UDP的流量是由哪台服务器处理的，后续这个连接的所有流量都同样转发到同一台服务器处理。七层的负载均衡，就是在四层的基础上（没有四层是绝对不可能有七层的），再考虑应用层的特征，比如同一个Web服务器的负载均衡，除了根据VIP加80端口辨别是否需要处理的流量，还可根据七层的URL、浏览器类别、语言来决定是否要进行负载均衡。举个例子，如果你的Web服务器分成两组，一组是中文语言的，一组是英文语言的，那么七层负载均衡就可以当用户来访问你的域名时，自动辨别用户语言，然后选择对应的语言服务器组进行负载均衡处理。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　<strong style="margin: 0px; padding: 0px; color: #333333;">&#9314;&nbsp;</strong>负载均衡器通常称为四层交换机或七层交换机。四层交换机主要分析IP层及TCP/UDP层，实现四层流量负载均衡。七层交换机除了支持四层负载均衡以外，还有分析应用层的信息，如HTTP协议URI或Cookie信息。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　1、负载均衡分为L4 switch（四层交换），即在OSI第4层工作，就是TCP层啦。此种Load Balance不理解应用协议（如HTTP/FTP/MySQL等等）。例子：LVS，F5。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　2、另一种叫做L7 switch（七层交换），OSI的最高层，应用层。此时，该Load Balancer能理解应用协议。例子： &nbsp;haproxy，MySQL Proxy。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　注意：上面的很多Load Balancer既可以做四层交换，也可以做七层交换。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　（二）</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　负载均衡设备也常被称为"四到七层交换机"，那么四层和七层两者到底区别在哪里？</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　第一，技术原理上的区别。</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　所谓四层负载均衡</strong>，也就是主要通过报文中的目标地址和端口，再加上负载均衡设备设置的服务器选择方式，决定最终选择的内部服务器。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　以常见的TCP为例，负载均衡设备在接收到第一个来自客户端的SYN 请求时，即通过上述方式选择一个最佳的服务器，并对报文中目标IP地址进行修改(改为后端服务器IP），直接转发给该服务器。TCP的连接建立，即三次握手是客户端和服务器直接建立的，负载均衡设备只是起到一个类似路由器的转发动作。在某些部署情况下，为保证服务器回包可以正确返回给负载均衡设备，在转发报文的同时可能还会对报文原来的源地址进行修改。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; text-align: center; background-color: #faffff;"><img src="http://images.cnitblog.com/kb/1/201401/03114159-1d39f32589b04705b65bf5bda16d2252.jpg" alt="" style="margin: 0px; padding: 0px; font-size: 12px; border: none; max-width: 620px;" /></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　所</strong><strong style="margin: 0px; padding: 0px; color: #333333;">谓七层负载均衡</strong>，也称为&#8220;内容交换&#8221;，也就是主要通过报文中的真正有意义的应用层内容，再加上负载均衡设备设置的服务器选择方式，决定最终选择的内部服务器。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　以常见的TCP为例，负载均衡设备如果要根据真正的应用层内容再选择服务器，只能先代理最终的服务器和客户端建立连接(三次握手)后，才可能接受到客户端发送的真正应用层内容的报文，然后再根据该报文中的特定字段，再加上负载均衡设备设置的服务器选择方式，决定最终选择的内部服务器。负载均衡设备在这种情况下，更类似于一个代理服务器。负载均衡和前端的客户端以及后端的服务器会分别建立TCP连接。所以从这个技术原理上来看，七层负载均衡明显的对负载均衡设备的要求更高，处理七层的能力也必然会低于四层模式的部署方式。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　第二，应用场景的需求。</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　七层应用负载的好处，是使得整个网络更"<strong style="margin: 0px; padding: 0px; color: #333333;">智能化</strong>"。例如访问一个网站的用户流量，可以通过七层的方式，将对图片类的请求转发到特定的图片服务器并可以使用缓存技术；将对文字类的请求可以转发到特定的文字服务器并可以使用压缩技术。当然这只是七层应用的一个小案例，从技术原理上，这种方式可以对客户端的请求和服务器的响应进行任意意义上的修改，极大的提升了应用系统在网络层的灵活性。很多在后台，例如Nginx或者Apache上部署的功能可以前移到负载均衡设备上，例如客户请求中的Header重写，服务器响应中的关键字过滤或者内容插入等功能。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　另外一个常常被提到功能就<strong style="margin: 0px; padding: 0px; color: #333333;">是</strong>安全性。网络中最常见的SYN Flood攻击，即黑客控制众多源客户端，使用虚假IP地址对同一目标发送SYN攻击，通常这种攻击会大量发送SYN报文，耗尽服务器上的相关资源，以达到Denial of Service(<em style="margin: 0px; padding: 0px; color: #333333;">DoS</em>)的目的。从技术原理上也可以看出，四层模式下这些SYN攻击都会被转发到后端的服务器上；而七层模式下这些SYN攻击自然在负载均衡设备上就截止，不会影响后台服务器的正常运营。另外负载均衡设备可以在七层层面设定多种策略，过滤特定报文，例如SQL&nbsp;Injection等应用层面的特定攻击手段，从应用层面进一步提高系统整体安全。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　现在的7层负载均衡，主要还是着重于<strong style="margin: 0px; padding: 0px; color: #333333;">应用</strong>HTTP协议，所以其应用范围主要是众多的网站或者内部信息平台等基于B/S开发的系统。 4层负载均衡则对应其他TCP应用，例如基于C/S开发的ERP等系统。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　第三，七层应用需要考虑的问题。<a href="http://hi.baidu.com/aking_roc" target="_blank" style="margin: 0px; padding: 0px; color: #1a64a2; text-decoration: none;"><br style="margin: 0px; padding: 0px; line-height: 10px;" /></a></strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　1：是否真的必要</strong>，七层应用的确可以提高流量智能化，同时必不可免的带来设备配置复杂，负载均衡压力增高以及故障排查上的复杂性等问题。在设计系统时需要考虑四层七层同时应用的混杂情况。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　2：是否真的可以提高安全性</strong>。例如SYN Flood攻击，七层模式的确将这些流量从服务器屏蔽，但负载均衡设备本身要有强大的抗DDoS能力，否则即使服务器正常而作为中枢调度的负载均衡设备故障也会导致整个应用的崩溃。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　3：是否有足够的灵活度</strong>。七层应用的优势是可以让整个应用的流量智能化，但是负载均衡设备需要提供完善的七层功能，满足客户根据不同情况的基于应用的调度。最简单的一个考核就是能否取代后台Nginx或者Apache等服务器上的调度功能。能够提供一个七层应用开发接口的负载均衡设备，可以让客户根据需求任意设定功能，才真正有可能提供强大的灵活性和智能性。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　（本节出自 &#8220;ADC技术博客&#8221; 博客，请务必保留此出处http://virtualadc.blog.51cto.com/3027116/591396）</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　（三）</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　负载均衡四七层介绍:</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　负载均衡（Load Balance）建立在现有网络结构之上，它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　负载均衡有两方面的含义：首先，大量的并发访问或数据流量分担到多台节点设备上分别处理，减少用户等待响应的时间；其次，单个重负载的运算分担到多台节点设备上做并行处理，每个节点设备处理结束后，将结果汇总，返回给用户，系统处理能力得到大幅度提高。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　本文所要介绍的负载均衡技术主要是指在均衡服务器群中所有服务器和应用程序之间流量负载的应用，目前负载均衡技术大多数是用于提高诸如在Web服务器、FTP服务器和其它关键任务服务器上的Internet服务器程序的可用性和可伸缩性。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　负载均衡技术分类</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　目前有许多不同的负载均衡技术用以满足不同的应用需求，下面从负载均衡所采用的设备对象、应用的网络层次（指OSI参考模型）及应用的地理结构等来分类。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　软/硬件负载均衡</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　软件负载均衡解决方案是指在一台或多台服务器相应的操作系统上安装一个或多个附加软件来实现负载均衡，如DNS Load Balance，CheckPoint Firewall-1 ConnectControl等，它的优点是基于特定环境，配置简单，使用灵活，成本低廉，可以满足一般的负载均衡需求。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　软件解决方案缺点也较多，因为每台服务器上安装额外的软件运行会消耗系统不定量的资源，越是功能强大的模块，消耗得越多，所以当连接请求特别大的时候，软件本身会成为服务器工作成败的一个关键；软件可扩展性并不是很好，受到操作系统的限制；由于操作系统本身的Bug，往往会引起安全问题。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　硬件负载均衡解决方案是直接在服务器和外部网络间安装负载均衡设备，这种设备我们通常称之为负载均衡器，由于专门的设备完成专门的任务，独立于操作系统，整体性能得到大量提高，加上多样化的负载均衡策略，智能化的流量管理，可达到最佳的负载均衡需求。&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　负载均衡器有多种多样的形式，除了作为独立意义上的负载均衡器外，有些负载均衡器集成在交换设备中，置于服务器与Internet链接之间，有些则以两块网络适配器将这一功能集成到PC中，一块连接到Internet上，一块连接到后端服务器群的内部网络上。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　一般而言，硬件负载均衡在功能、性能上优于软件方式，不过成本昂贵。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　本地/全局负载均衡<a href="http://hi.baidu.com/aking_roc" target="_blank" style="margin: 0px; padding: 0px; color: #1a64a2; text-decoration: none;"><br style="margin: 0px; padding: 0px; line-height: 10px;" /></a></strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　负载均衡从其应用的地理结构上分为本地负载均衡(Local Load Balance)和全局负载均衡(Global Load Balance，也叫地域负载均衡)，本地负载均衡是指对本地的服务器群做负载均衡，全局负载均衡是指对分别放置在不同的地理位置、有不同网络结构的服务器群间作负载均衡。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　本地负载均衡能有效地解决数据流量过大、网络负荷过重的问题，并且不需花费昂贵开支购置性能卓越的服务器，充分利用现有设备，避免服务器单点故障造成数据流量的损失。其有灵活多样的均衡策略把数据流量合理地分配给服务器群内的服务器共同负担。即使是再给现有服务器扩充升级，也只是简单地增加一个新的服务器到服务群中，而不需改变现有网络结构、停止现有的服务。&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　全局负载均衡主要用于在一个多区域拥有自己服务器的站点，为了使全球用户只以一个IP地址或域名就能访问到离自己最近的服务器，从而获得最快的访问速度，也可用于子公司分散站点分布广的大公司通过Intranet（企业内部互联网）来达到资源统一合理分配的目的。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;"><strong style="margin: 0px; padding: 0px; color: #333333;">　　网络层次上的负载均衡</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　针对网络上负载过重的不同瓶颈所在，从网络的不同层次入手，我们可以采用相应的负载均衡技术来解决现有问题。&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　随着带宽增加，数据流量不断增大，网络核心部分的数据接口将面临瓶颈问题，原有的单一线路将很难满足需求，而且线路的升级又过于昂贵甚至难以实现，这时就可以考虑采用链路聚合（Trunking）技术。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　链路聚合技术（第二层负载均衡）将多条物理链路当作一条单一的聚合逻辑链路使用，网络数据流量由聚合逻辑链路中所有物理链路共同承担，由此在逻辑上增大了链路<a href="http://hi.baidu.com/aking_roc" target="_blank" style="margin: 0px; padding: 0px; color: #1a64a2; text-decoration: none;">的</a>容量，使其能满足带宽增加的需求。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　现代负载均衡技术通常操作于网络的第四层或第七层。第四层负载均衡将一个Internet上合法注册的IP地址映射为多个内部服务器的IP地址，对每次 TCP连接请求动态使用其中一个内部IP地址，达到负载均衡的目的。在第四层交换机中，此种均衡技术得到广泛的应用，一个目标地址是服务器群VIP（虚拟 IP，Virtual IP address）连接请求的数据包流经交换机，交换机根据源端和目的IP地址、TCP或UDP端口号和一定的负载均衡策略，在服务器IP和VIP间进行映射，选取服务器群中最好的服务器来处理连接请求。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　第七层负载均衡控制应用层服务的内容，提供了一种对访问流量的高层控制方式，适合对HTTP服务器群的应用。第<strong style="margin: 0px; padding: 0px; color: #333333;">七层负载均衡技术通过检查流经的HTTP报头，根据报头内的信息来执行负载均衡任务</strong>。&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　第七层负载均衡优点表现在如下几个方面：&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　通过对HTTP报头的检查，可以检测出HTTP400、500和600系列的错误信息，因而能透明地将连接请求重新定向到另一台服务器，避免应用层故障。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　可根据流经的数据类型（如判断数据包是图像文件、压缩文件或多媒体文件格式等），把数据流量引向相应内容的服务器来处理，增加系统性能。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　能根据连接请求的类型，如是普通文本、图象等静态文档请求，还是asp、cgi等的动态文档请求，把相应的请求引向相应的服务器来处理，提高系统的性能及安全性。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　第七层负载均衡受到其所支持的协议限制（一般只有HTTP），这样就限制了它应用的广泛性，并且检查HTTP报头会占用大量的系统资源，势必会影响到系统的性能，在大量连接请求的情况下，负载均衡设备自身容易成为网络整体性能的瓶颈。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　<strong style="margin: 0px; padding: 0px; color: #333333;">负载均衡策略</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　在实际应用中，我们可能不想仅仅是把客户端的服务请求平均地分配给内部服务器，而不管服务器是否宕机。而是想使Pentium III服务器比Pentium II能接受更多的服务请求，一台处理服务请求较少的服务器能分配到更多的服务请求，出现故障的服务器将不再接受服务请求直至故障恢复等等。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　选择合适的负载均衡策略，使多个设备能很好的共同完成任务，消除或避免现有网络负载分布不均、数据流量拥挤反应时间长的瓶颈。在各负载均衡方式中，针对不同的应用需求，在OSI参考模型的第二、三、四、七层的负载均衡都有相应的负载均衡策略。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　负载均衡策略的优劣及其实现的难易程度有两个关键因素：一、负载均衡算法，二、对网络系统状况的检测方式和能力。&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　考虑到服务请求的不同类型、服务器的不同处理能力以及随机选择造成的负载分配不均匀等问题，为了更加合理的把负载分配给内部的多个服务器，就需要应用相应的能够正确反映各个服务器处理能力及网络状态的<strong style="margin: 0px; padding: 0px; color: #333333;">负载均衡算法</strong>：</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　轮循均衡（Round Robin）：每一次来自网络的请求轮流分配给内部中的服务器，从1至N然后重新开始。此种均衡算法适合于服务器组中的所有服务器都有相同的软硬件配置并且平均服务请求相对均衡的情况。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　权重轮循均衡（Weighted Round Robin）：根据服务器的不同处理能力，给每个服务器分配不同的权值，使其能够接受相应权值数的服务请求。例如：服务器A的权值被设计成1，B的权值是 3，C的权值是6，则服务器A、B、C将分别接受到10%、30％、60％的服务请求。此种均衡算法能确保高性能的服务器得到更多的使用率，避免低性能的服务器负载过重。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　随机均衡（Random）：把来自网络的请求随机分配给内部中的多个服务器。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　权重随机均衡（Weighted Random）：此种均衡算法类似于权重轮循算法，不过在处理请求分担时是个随机选择的过程。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　响应速度均衡（Response Time）：负载均衡设备对内部各服务器发出一个探测请求（例如Ping），然后根据内部中各服务器对探测请求的最快响应时间来决定哪一台服务器来响应客户端的服务请求。此种均衡算法能较好的反映服务器的当前运行状态，但这最快响应时间仅仅指的是负载均衡设备与服务器间的最快响应时间，而不是客户端与服务器间的最快响应时间。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　最少连接数均衡（Least Connection）：客户端的每一次请求服务在服务器停留的时间可能会有较大的差异，随着工作时间加长，如果采用简单的轮循或随机均衡算法，每一台服务器上的连接进程可能会产生极大的不同，并没有达到真正的负载均衡。最少连接数均衡算法对内部中需负载的每一台服务器都有一个数据记录，记录当前该服务器正在处理的连接数量，当有新的服务连接请求时，将把当前请求分配给连接数最少的服务器，使均衡更加符合实际情况，负载更加均衡。此种均衡算法适合长时处理的请求服务，如FTP。&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　处理能力均衡：此种均衡算法将把服务请求分配给内部中处理负荷（根据服务器CPU型号、CPU数量、内存大小及当前连接数等换算而成）最轻的服务器，由于考虑到了内部服务器的处理能力及当前网络运行状况，所以此种均衡算法相对来说更加精确，尤其适合运用到第七层（应用层）负载均衡的情况下。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　DNS响应均衡（Flash DNS）：在Internet上，无论是HTTP、FTP或是其它的服务请求，客户端一般都是通过域名解析来找到服务器确切的IP地址的。在此均衡算法下，分处在不同地理位置的负载均衡设备收到同一个客户端的域名解析请求，并在同一时间内把此域名解析成各自相对应服务器的IP地址（即与此负载均衡设备在同一位地理位置的服务器的IP地址）并返回给客户端，则客户端将以最先收到的域名解析IP地址来继续请求服务，而忽略其它的IP地址响应。在种均衡策略适合应用在全局负载均衡的情况下，对本地负载均衡是没有意义的。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　尽管有多种的负载均衡算法可以较好的把数据流量分配给服务器去负载，但如果负载均衡策略没有对网络系统状况的检测方式和能力，一旦在某台服务器或某段负载均衡设备与服务器网络间出现故障的情况下，负载均衡设备依然把一部分数据流量引向那台服务器，这势必造成大量的服务请求被丢失，达不到不间断可用性的要求。所以良好的负载均衡策略应有对网络故障、服务器系统故障、应用服务故障的检<strong style="margin: 0px; padding: 0px; color: #333333;">测方式和能力</strong>：</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　Ping侦测：通过ping的方式检测服务器及网络系统状况，此种方式简单快速，但只能大致检测出网络及服务器上的操作系统是否正常，对服务器上的应用服务检测就无能为力了。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　TCP Open侦测：每个服务都会开放某个通过TCP连接，检测服务器上某个TCP端口（如Telnet的23口，HTTP的80口等）是否开放来判断服务是否正常。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　HTTP URL侦测：比如向HTTP服务器发出一个对main.html文件的访问请求，如果收到错误信息，则认为服务器出现故障。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　负载均衡策略的优劣除受上面所讲的两个因素影响外，在有些应用情况下，我们需要将来自同一客户端的所有请求都分配给同一台服务器去负担，例如服务器将客户端注册、购物等服务请求信息保存的本地数据库的情况下，把客户端的子请求分配给同一台服务器来处理就显的至关重要了。有两种方式可以解决此问题，一是根据IP地址把来自同一客户端的多次请求分配给同一台服务器处理，客户端IP地址与服务器的对应信息是保存在负载均衡设备上的；二是在客户端浏览器 cookie内做独一无二的标识来把多次请求分配给同一台服务器处理，适合通过代理服务器上网的客户端。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　还有一种路径外返回模式（Out of Path Return），当客户端连接请求发送给负载均衡设备的时候，中心负载均衡设备将请求引向某个服务器，服务器的回应请求不再返回给中心负载均衡设备，即绕过流量分配器，直接返回给客户端，因此中心负载均衡设备只负责接受并转发请求，其网络负担就减少了很多，并且给客户端提供了更快的响应时间。此种模式一般用于HTTP服务器群，在各服务器上要安装一块虚拟网络适配器，并将其IP地址设为服务器群的VIP，这样才能在服务器直接回应客户端请求时顺利的达成三次握手。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　<strong style="margin: 0px; padding: 0px; color: #333333;">负载均衡实施要素</strong></p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　负载均衡方案应是在网站建设初期就应考虑的问题，不过有时随着访问流量的爆炸性增长，超出决策者的意料，这也就成为不得不面对的问题。当我们在引入某种负载均衡方案乃至具体实施时，像其他的许多方案一样，首先是确定当前及将来的应用需求，然后在代价与收效之间做出权衡。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　针对当前及将来的应用需求，分析网络瓶颈的不同所在，我们就需要确立是采用哪一类的负载均衡技术，采用什么样的均衡策略，在可用性、兼容性、安全性等等方面要满足多大的需求，如此等等。&nbsp;</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　不管负载均衡方案是采用花费较少的软件方式，还是购买代价高昂在性能功能上更强的第四层交换机、负载均衡器等硬件方式来实现，亦或其他种类不同的均衡技术，下面这几项都是我们在引入均衡方案时可能要考虑的问题：</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　性能：性能是我们在引入均衡方案时需要重点考虑的问题，但也是一个最难把握的问题。衡量性能时可将每秒钟通过网络的数据包数目做为一个参数，另一个参数是均衡方案中服务器群所能处理的最大并发连接数目，但是，假设一个均衡系统能处理百万计的并发连接数，可是却只能以每秒2个包的速率转发，这显然是没有任何作用的。性能的优劣与负载均衡设备的处理能力、采用的均衡策略息息相关，并且有两点需要注意：一、均衡方案对服务器群整体的性能，这是响应客户端连接请求速度的关键；二、负载均衡设备自身的性能，避免有大量连接请求时自身性能不足而成为服务瓶颈。有时我们也可以考虑采用混合型负载均衡策略来提升服务器群的总体性能，如DNS负载均衡与NAT负载均衡相结合。另外，针对有大量静态文档请求的站点，也可以考虑采用高速缓存技术，相对来说更节省费用，更能提高响应性能；对有大量ssl/xml内容传输的站点，更应考虑采用ssl/xml加速技术。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　可扩展性：IT技术日新月异，一年以前最新的产品，现在或许已是网络中性能最低的产品；业务量的急速上升，一年前的网络，现在需要新一轮的扩展。合适的均衡解决方案应能满足这些需求，能均衡不同操作系统和硬件平台之间的负载，能均衡HTTP、邮件、新闻、代理、数据库、防火墙和 Cache等不同服务器的负载，并且能以对客户端完全透明的方式动态增加或删除某些资源。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　灵活性：均衡解决方案应能灵活地提供不同的应用需求，满足应用需求的不断变化。在不同的服务器群有不同的应用需求时，应有多样的均衡策略提供更广泛的选择。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　可靠性：在对服务质量要求较高的站点，负载均衡解决方案应能为服务器群提供完全的容错性和高可用性。但在负载均衡设备自身出现故障时，应该有良好的冗余解决方案，提高可靠性。使用冗余时，处于同一个冗余单元的多个负载均衡设备必须具有有效的方式以便互相进行监控，保护系统尽可能地避免遭受到重大故障的损失。</p><p style="margin-right: 0px; margin-left: 0px; padding: 0px; font-family: verdana, Arial, Helvetica, sans-serif; color: #232323; line-height: 28px; background-color: #faffff;">　　易管理性：不管是通过软件还是硬件方式的均衡解决方案，我们都希望它有灵活、直观和安全的管理方式，这样便于安装、配置、维护和监控，提高工作效率，避免差错。在硬件负载均衡设备上，目前主要有三种管理方式可供选择：一、命令行接口（CLI：Command Line Interface），可通过超级终端连接负载均衡设备串行接口来管理，也能telnet远程登录管理，在初始化配置时，往往要用到前者；二、图形用户接口（GUI：Graphical User Interfaces），有基于普通web页的管理，也有通过Java Applet 进行安全管理，一般都需要管理端安装有某个版本的浏览器；三、SNMP（Simple Network Management Protocol，简单网络管理协议）支持，通过第三方网络管理软件对符合SNMP标准的设备进行管理。<br /><br />原文链接<br /><span style="font-family: verdana, 'courier new'; line-height: 21px;">http://kb.cnblogs.com/page/188170/</span></p><img src ="http://www.cppblog.com/Hero/aggbug/213250.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Hero/" target="_blank">Hero</a> 2016-04-12 21:02 <a href="http://www.cppblog.com/Hero/archive/2016/04/12/213250.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Tomcat Manager用户配置</title><link>http://www.cppblog.com/Hero/archive/2015/10/29/212146.html</link><dc:creator>Hero</dc:creator><author>Hero</author><pubDate>Thu, 29 Oct 2015 07:54:00 GMT</pubDate><guid>http://www.cppblog.com/Hero/archive/2015/10/29/212146.html</guid><wfw:comment>http://www.cppblog.com/Hero/comments/212146.html</wfw:comment><comments>http://www.cppblog.com/Hero/archive/2015/10/29/212146.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Hero/comments/commentRss/212146.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Hero/services/trackbacks/212146.html</trackback:ping><description><![CDATA[<p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Tomcat Manager是Tomcat自带的、用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用。Tomcat是Java领域使用最广泛的服务器之一，因此Tomcat Manager也成为了使用非常普遍的功能应用。</p><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">在默认情况下，Tomcat Manager是处于禁用状态的。准确地说，Tomcat Manager需要以用户角色进行登录并授权才能使用相应的功能，不过Tomcat并没有配置任何默认的用户，因此需要我们进行相应的用户配置之后才能使用Tomcat Manager。</p><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Tomcat Manager的用户配置是在<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">Tomcat安装目录/conf/tomcat-users.xml</code>文件中进行管理的。</p><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">Tomcat Manager的用户配置非常简单，下面我们以一个具体的配置为例：</p><pre linenum"="" style="margin-top: 0px; margin-bottom: 20px; padding: 10px; border: 1px solid #999999; outline: 0px; font-size: 12px; vertical-align: baseline; word-wrap: break-word; word-break: break-all; overflow: auto; position: relative; color: #333333; font-stretch: normal; line-height: 1.5; font-family: 'Courier New', Consola, SimSun; border-radius: 8px; -webkit-box-shadow: #999999 0px 2px 4px; box-shadow: #999999 0px 2px 4px; background: #f7f7f7;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">&lt;tomcat-users&gt;</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #000000; background: transparent;"><br /></span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">&lt;role</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">rolename</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"manager-gui"</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">/&gt;</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #000000; background: transparent;"><br /></span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">&lt;role</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">rolename</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"manager-script"</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">/&gt;</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #000000; background: transparent;"><br /></span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">&lt;user</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">username</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"tomcat"</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">password</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"tomcat"</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">roles</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"manager-gui"</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">/&gt;</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #000000; background: transparent;"><br /></span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">&lt;user</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">username</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"<span id="2_nwp" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; width: auto; height: auto; float: none; background: transparent;"><a id="2_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&amp;app_id=0&amp;c=news&amp;cf=1001&amp;ch=0&amp;di=128&amp;fv=18&amp;is_app=0&amp;jk=afd7f39aa3cae07f&amp;k=admin&amp;k0=admin&amp;kdi0=0&amp;luki=1&amp;n=10&amp;p=baidu&amp;q=97086099_cpr&amp;rb=0&amp;rs=1&amp;seller_id=1&amp;sid=7fe0caa39af3d7af&amp;ssp2=1&amp;stid=0&amp;t=tpclicked3_hc&amp;td=1691595&amp;tu=u1691595&amp;u=http%3A%2F%2Fwww%2E365mini%2Ecom%2Fpage%2Ftomcat%2Dmanager%2Duser%2Dconfiguration%2Ehtm&amp;urlid=0" target="_blank" mpid="2" style="color: #660099; text-decoration: none;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; width: auto; height: auto; background: transparent;">admin</span></a></span>"</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">password</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"123456"</span> <span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: red; background: transparent;">roles</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #666600; background: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; background: transparent;">"manager-script"</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">/&gt;</span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #000000; background: transparent;"><br /></span><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #880000; background: transparent;">&lt;/tomcat-users&gt;</span></pre><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">如上所示，我们只需要在<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">tomcat-users</code>节点中配置相应的<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">role</code>(角色/权限)和<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">user</code>(用户)即可。一个<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">user</code>节点表示单个用户，属性<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">username</code>和<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">password</code>分别表示登录的用户名和密码，属性<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">roles</code>表示该用户所具备的权限。</p><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">user</code>节点的<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">roles</code>属性值与<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">role</code>节点的<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">rolename</code>属性值相对应，表示当前用户具备该role节点所表示的角色权限。当然，一个用户可以具备多种权限，因此属性<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">roles</code>的值可以是多个<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">rolename</code>，多个<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">rolename</code>之间以英文逗号隔开即可。</p><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">稍加思考，我们就应该猜测到，<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">rolename</code>的属性值并不是随意的内容，否则Tomcat怎么能够知道我们随便定义的<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">rolename</code>表示什么样的权限呢。实际上，Tomcat已经为我们定义了4种不同的角色&#8212;&#8212;也就是4个<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">rolename</code>，我们只需要使用Tomcat为我们定义的这几种角色就足够满足我们的工作需要了。</p><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">以下是Tomcat Manager 4种角色的大致介绍(下面URL中的*为通配符)：</p><dl style="margin: 1em 0px; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; font-size: 13px; line-height: 19.5px;"><dt style="margin-top: 0px; color: #008800;">manager-gui</dt><dd style="margin-top: 1px; margin-bottom: 0.5em;">允许访问html接口(即URL路径为/manager/html/*)</dd><dt style="margin-top: 0px; color: #008800;">manager-script</dt><dd style="margin-top: 1px; margin-bottom: 0.5em;">允许访问纯文本接口(即URL路径为/manager/text/*)</dd><dt style="margin-top: 0px; color: #008800;">manager-jmx</dt><dd style="margin-top: 1px; margin-bottom: 0.5em;">允许访问JMX<span id="1_nwp" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; width: auto; height: auto; float: none; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><a id="1_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&amp;app_id=0&amp;c=news&amp;cf=1001&amp;ch=0&amp;di=128&amp;fv=18&amp;is_app=0&amp;jk=afd7f39aa3cae07f&amp;k=%B4%FA%C0%ED&amp;k0=%B4%FA%C0%ED&amp;kdi0=0&amp;luki=2&amp;n=10&amp;p=baidu&amp;q=97086099_cpr&amp;rb=0&amp;rs=1&amp;seller_id=1&amp;sid=7fe0caa39af3d7af&amp;ssp2=1&amp;stid=0&amp;t=tpclicked3_hc&amp;td=1691595&amp;tu=u1691595&amp;u=http%3A%2F%2Fwww%2E365mini%2Ecom%2Fpage%2Ftomcat%2Dmanager%2Duser%2Dconfiguration%2Ehtm&amp;urlid=0" target="_blank" mpid="1" style="color: #660099; text-decoration: none;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; width: auto; height: auto; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">代理</span></a></span>接口(即URL路径为/manager/jmxproxy/*)</dd><dt style="margin-top: 0px; color: #008800;">manager-status</dt><dd style="margin-top: 1px; margin-bottom: 0.5em;">允许访问Tomcat只读状态页面(即URL路径为/manager/status/*)</dd></dl><p style="margin: 1.2em 0px 1.3em; padding: 0px; border: 0px; outline: 0px; font-size: 13px; vertical-align: baseline; color: #333333; font-family: 'Courier New', 宋体, 'Times New Roman', serif; line-height: 19.5px; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">从Tomcat Manager内部配置文件中可以得知，<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-gui</code>、<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-script</code>、<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-jmx</code>均具备<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-status</code>的权限，也就是说，<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-gui</code>、<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-script</code>、<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-jmx</code>三种角色权限无需再额外添加<code style="margin: 0px 3px; padding: 1px 4px; border: 1px solid #e1e1e8; outline: 0px; vertical-align: baseline; font-family: Consolas, Monaco, Menlo, 宋体; color: #dd1144; border-radius: 3px; background: #f7f7f9;">manager-status</code>权限，即可直接访问路径<q style="margin: 0px; padding: 0px; border: 1px dotted #778855; outline: 0px; vertical-align: baseline; background: none 0px 0px repeat scroll #f5f5f5;">/manager/<span id="0_nwp" style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; width: auto; height: auto; float: none; background: transparent;"><a id="0_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&amp;app_id=0&amp;c=news&amp;cf=1001&amp;ch=0&amp;di=128&amp;fv=18&amp;is_app=0&amp;jk=afd7f39aa3cae07f&amp;k=status&amp;k0=status&amp;kdi0=0&amp;luki=5&amp;n=10&amp;p=baidu&amp;q=97086099_cpr&amp;rb=0&amp;rs=1&amp;seller_id=1&amp;sid=7fe0caa39af3d7af&amp;ssp2=1&amp;stid=0&amp;t=tpclicked3_hc&amp;td=1691595&amp;tu=u1691595&amp;u=http%3A%2F%2Fwww%2E365mini%2Ecom%2Fpage%2Ftomcat%2Dmanager%2Duser%2Dconfiguration%2Ehtm&amp;urlid=0" target="_blank" mpid="0" style="color: #660099; text-decoration: none;"><span style="margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; color: #0000ff; width: auto; height: auto; background: transparent;">status</span></a></span>/*</q>。</p><img src ="http://www.cppblog.com/Hero/aggbug/212146.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Hero/" target="_blank">Hero</a> 2015-10-29 15:54 <a href="http://www.cppblog.com/Hero/archive/2015/10/29/212146.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【hibernate】hbm2ddl.auto配置详解</title><link>http://www.cppblog.com/Hero/archive/2015/08/26/211687.html</link><dc:creator>Hero</dc:creator><author>Hero</author><pubDate>Wed, 26 Aug 2015 07:19:00 GMT</pubDate><guid>http://www.cppblog.com/Hero/archive/2015/08/26/211687.html</guid><wfw:comment>http://www.cppblog.com/Hero/comments/211687.html</wfw:comment><comments>http://www.cppblog.com/Hero/archive/2015/08/26/211687.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Hero/comments/commentRss/211687.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Hero/services/trackbacks/211687.html</trackback:ping><description><![CDATA[<div style="border:none;border-bottom:solid #4F81BD 1.0pt;padding:0cm 0cm 4.0pt 0cm">  <p><span style="font-family: Courier;"></span></p></div><div style="border:none;border-bottom:solid #4F81BD 1.0pt;padding:0cm 0cm 4.0pt 0cm">  <h1 style="text-align: center; ">hibernate.hbm2ddl.auto<span style="font-family:宋体;">配置详解</span></h1>  </div>  <h1>一、<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;">配置使用方式</span></h1>  <p>hibernate.cfg.xml <span style="font-family:宋体;Courier New&quot;;Courier New&quot;">中</span>hibernate.hbm2ddl.auto<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">配置节点如下：</span></p>  <div style="border:solid windowtext 1.0pt;padding:1.0pt 4.0pt 1.0pt 4.0pt; background:#CCCCCC;">  <p style="border: none; padding: 0cm; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><span style="background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">&lt;properties&gt;<br /> &nbsp; &lt;property name="hibernate.show_sql" value="true" /&gt;</span>&nbsp;<span style="background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><br /> &nbsp; &lt;property name="hibernate.hbm2ddl.auto" value="create" /&gt;<br /> &lt;/properties&gt;</span></p>  </div>  <h1>二、<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;">官方解释</span></h1>  <p>Hibernate Reference Documentation 3.3.1<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">解释如下：</span><br /> Automatically validate or export schema DDL to the database when the SessionFactory is created.With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.<br /> eg. validate | update | create | create-drop</p>  <h1>三、<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;">自己理解</span></h1>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">其实这个</span>hibernate.hbm2ddl.auto<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">参数的作用主要用于：</span></p>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">自动创建</span>|<span style="font-family: 宋体;Courier New&quot;;Courier New&quot;">更新</span>|<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">验证数据库表结构。</span></p>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">如果不是此方面的需求建议不配置该条语句。</span></p>  <h2>1.<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>create<span style="font-family:宋体;">：</span></h2>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">每次加载</span>hibernate<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">时都会删除上一次的生成的表，然后根据你的</span>model<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">类再重新来生成新表，哪怕两次没有任何改变也要这样执行，这就是导致数据库表数据丢失的一个重要原因。</span></p>  <h2>2.<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>create-drop <span style="font-family:宋体;">：</span></h2>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">每次加载</span>hibernate<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">时根据</span>model<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">类生成表，但是</span>SessionFactory<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">一关闭</span>,<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">表就自动删除。</span></p>  <h2>3.<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>update<span style="font-family:宋体;">：</span></h2>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">最常用的属性，第一次加载</span>hibernate<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">时根据</span>model<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">类会自动建立起表的结构（前提是先建立好数据库），以后加载</span>hibernate<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">时根据</span> model<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">类自动更新表结构，即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后，表结构是不会被马上建立起来的，是要等</span> <span style="font-family:宋体;Courier New&quot;;Courier New&quot;">应用第一次运行起来后才会。</span></p>  <h2>4.<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>validate <span style="font-family:宋体;">：</span></h2>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">每次加载</span>hibernate<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">时，验证创建数据库表结构，只会和数据库中的表进行比较，不会创建新表，但是会插入新值。</span></p>  <h1>四、<span style="font-weight: normal; font-stretch: normal; font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-family:宋体;">小结</span></h1>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">当我们把</span>hibernate.hbm2ddl.auto=create<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">时</span>hibernate<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">先用</span>hbm2ddl<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">来生成数据库</span>schema<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">。</span></p>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">当我们把</span>hibernate.cfg.xml<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">文件中</span>hbm2ddl<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">属性注释掉，这样我们就取消了在启动时用</span>hbm2ddl<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">来生成数据库</span>schema<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">。通常只有在不断重复进行单元测试的时候才需要打开它，但再次运行</span>hbm2ddl<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">会把你保存的一切都删除掉（</span>drop<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">）</span>---- create<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">配置的含义是：&#8220;在创建</span>SessionFactory<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">的时候，从</span>scema<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">中</span>drop<span style="font-family: 宋体;Courier New&quot;;Courier New&quot;">掉所以的表，再重新创建它们&#8221;。</span></p>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;;color:red">我们在项目中使用</span><span style="color:red">Hibernate</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;; color:red">映射</span><span style="color:red">oracle</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;; color:red">的视图的时候，发现会出现某种特殊条件下</span><span style="color:red">hibernate</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;; color:red">自动创建了一张和视图同名的</span><span style="color:red">Table</span><span style="font-family:宋体;Courier New&quot;;Courier New&quot;; color:red">，导致获取视图信息时，实际上获取的总是空表中的内容。</span></p>  <p><span style="font-family:宋体;Courier New&quot;;Courier New&quot;">注意，很多</span>Hibernate<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">新手在这一步会失败，我们不时看到关于</span>Table not found<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">错误信息的提问。但是，只要你根据上面描述的步骤来执行，就不会有这个问题，因为</span>hbm2ddl<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">会在第一次运行的时候创建数据库</span>schema<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">，</span> <span style="font-family:宋体;Courier New&quot;;Courier New&quot;">后续的应用程序重启后还能继续使用这个</span>schema<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">。假若你修改了映射，或者修改了数据库</span>schema,<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">你必须把</span>hbm2ddl<span style="font-family:宋体;Courier New&quot;;Courier New&quot;">重新打开一次。</span></p><p><span style="font-family: Courier;"></span></p><img src ="http://www.cppblog.com/Hero/aggbug/211687.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Hero/" target="_blank">Hero</a> 2015-08-26 15:19 <a href="http://www.cppblog.com/Hero/archive/2015/08/26/211687.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>