1.Tomcat 6多应用的Context配置
tomcat 5,6关于context的配置不同于tomcat 4,不推荐在server.xml中进行配置,在context.xml文件中进行配置才是更好的方法。回想以前所有的项目都是在server.xml中配置context的,每次部署一个新的应用或者修改context内容都要重新启动tomcat,导致应用总是要中断服务一小段时间,比较不方便,而且多个context都在server.xml中,显得有些杂乱。于是决定使用在context.xml中进行配置应用的方式。在tomcat_home\conf目录下有一个context..xml文件,修改其中内容之后,tomcat 能够自动reload所有的应用。
但是tomcat是不支持在一个context.xml中配置多个不同应用的。在项目工程用下面的META-INF文件夹下建立context.xml文件就可以满足应用的定制需求。具体路径就是:tomcat_home\webapps\yourApp\META-INF\context.xml,这样每个应用都可以拥有各自的配置,修改了该文件中的内容,tomcat可以自动重新装载该应用,非常的方便。下面是一个可以自动重新装载servlet和javabean(方便debug),带有数据库连接池(以Oracle为例)的context.xml文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" crossContext="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
username="scott"
password="tiger"
maxActive="20"
maxIdle="10"
maxWait="-1"/>
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_DBTest_log." suffix=".txt" timestamp="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve"
prefix="localhost_access_log." suffix=".txt" pattern="common" />
</Context>
2.热部署
1.找到 server.xml,这个文件在 %catalina_home%\conf下
2.再找到文件:
<Host><Context path="\" docBase="D:\Workspace6.6\Mywork\WebRoot" reloadable="true" ></Context></host>
3.保存重启
3.APR
许多朋友可能在启动tomcat的时候都会看到类似这样的信息:
Java代码 http://www.javaeye.com/images/icon_copy.gif
org.apache.catalina.core.AprLifecycleListenerinit
信息:TheApacheTomcatNativelibrarywhichallowsoptimalperformanceinproductionenvironmentswasnotfoundonthejava.library.path:C:\Java\jre\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS
org.apache.catalina.core.AprLifecycleListener init
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Java\jre\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS
出现这种情况是这表示没有找到APR
使用APR提高Tomcat性能
Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术.
APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTTP Server 2.x的核心。APR有很多用途,包括访问高级IO功能(例如sendfile,epoll和OpenSSL),OS级别功能(随机数生成,系统状态等等),本地进程管理(共享内存,NT管道和UNIX sockets)。这些功能可以使Tomcat作为一个通常的前台WEB服务器,能更好地和其它本地web技术集成,总体上让Java更有效率作为一个高性能web服务器平台而不是简单作为后台容器。
在产品环境中,特别是直接使用Tomcat做WEB服务器的时候,应该使用Tomcat Native来提高其性能
要测APR给tomcat带来的好处最好的方法是在慢速网络上(模拟Internet),将Tomcat线程数开到300以上的水平,然后模拟一大堆并发请求。如果不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待。但是配上APR之后,并发的线程数量明显下降,从原来的300可能会马上下降到只有几十,新的请求会毫无阻塞的进来。
在局域网环境测,就算是400个并发,也是一瞬间就处理/传输完毕,但是在真实的Internet环境下,页面处理时间只占0.1%都不到,绝大部分时间都用来页面传输。如果不用APR,一个线程同一时间只能处理一个用户,势必会造成阻塞。所以生产环境下用apr是非常必要的。
简要解决办法:去 http://tomcat.heanet.ie/native/ 下载编译好的tcnative-1.dll文件,目前最新为1.1.14,拷贝至jdk\bin下,再启动就可以成功加载APR了。
Java代码 http://www.javaeye.com/images/icon_copy.gif
org.apache.catalina.core.AprLifecycleListenerinit
信息:LoadedApacheTomcatNativelibrary1.1 .14 .
org.apache.catalina.core.AprLifecycleListenerinit
信息:APRcapabilities:IPv6[false ],sendfile[true ],acceptfilters[false ],random[true ].
org.apache.catalina.core.AprLifecycleListener init
信息: Loaded Apache Tomcat Native library 1.1.14.
org.apache.catalina.core.AprLifecycleListener init
信息: APR capabilities: IPv6 [false], sendfile [true], accept filters [false], random [true].
4.URIEncoding
有时候在做开发的时候经常发现文本框输入的中文到了程序中成了乱码,其实是因为在端口监听部分缺少编码。
Xml代码 http://www.javaeye.com/images/icon_copy.gif
URIEncoding="UTF-8"
解决方法如下:
原始部分
8080端口上
Xml代码 http://www.javaeye.com/images/icon_copy.gif
<Connector port ="8080" protocol ="HTTP/1.1"
connectionTimeout ="20000"
redirectPort ="8443" />
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改后
Xml代码 http://www.javaeye.com/images/icon_copy.gif
<Connector port ="8080" protocol ="HTTP/1.1"
connectionTimeout ="20000"
redirectPort ="8443" URIEncoding ="UTF-8" />
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
8009端口 ajp跳转服务上,关于这个端口在apache http 做跳转时,要相当注意
Xml代码 http://www.javaeye.com/images/icon_copy.gif
<Connector port ="8009" protocol ="AJP/1.3" redirectPort ="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改后
Xml代码 http://www.javaeye.com/images/icon_copy.gif
<Connector port ="8009" protocol ="AJP/1.3" redirectPort ="8443" URIEncoding ="UTF-8" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
这样,服务器得到的中文字符就不会再有乱码了。
5.设置Tomcat管理员账号
修改tomcat-users.xml文件,在</tomcat-users> 的标签前添加一行
Xml代码 http://www.javaeye.com/images/icon_copy.gif
<user username ="tomcat" password ="tomcat" roles ="admin,manager" />
<user username="tomcat" password="tomcat" roles="admin,manager"/>
让tomcat用户拥有管理员权限。
6.设置SSL
首先,我们要创建密钥:
Shell代码 http://www.javaeye.com/images/icon_copy.gif
keytool-genkey-aliastomcat-keyalgRSA
keytool -genkey -alias tomcat -keyalg RSA
此时,用户主目录下会生成一个.keystore 文件。
然后,我们配置server.xml文件,找到SSLEnabled="true"所在的标签,将其解除注释,同时填补两个属性:
1.keystoreFile="C:/Users/Zlex/.keystore"
2.keystorePass="123456"
keystoreFile 指的是你的密钥文件存储的路径,keystorePass指的是你的密码。
举例如下:
Xml代码 http://www.javaeye.com/images/icon_copy.gif
<!--
DefineaSSLHTTP/1.1Connectoronport8443Thisconnectorusesthe
JSSEconfiguration,whenusingAPR,theconnectorshouldbeusingthe
OpenSSLstyleconfigurationdescribedintheAPRdocumentation
-->
<!---->
<Connector
SSLEnabled ="true"
clientAuth ="false"
keystoreFile ="C:/Users/Zlex/.keystore"
keystorePass ="123456"
maxThreads ="150"
port ="8443"
protocol ="HTTP/1.1"
scheme ="https"
secure ="true"
sslProtocol ="TLS" />
<!--
Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the
JSSE configuration, when using APR, the connector should be using the
OpenSSL style configuration described in the APR documentation
-->
<!---->
<Connector
SSLEnabled="true"
clientAuth="false"
keystoreFile="C:/Users/Zlex/.keystore"
keystorePass="123456"
maxThreads="150"
port="8443"
protocol="HTTP/1.1"
scheme="https"
secure="true"
sslProtocol="TLS" />
最后,重启tomcat,在地址栏中访问 https://localhost:8443/ 。
将上述port="8443" 配置改为port="443" ,可以通过https://localhost/ 直接访问
7.在一台机器上同时启动2个tomcat
1.特别要注意:不要设置CATALINA_HOME
2.分别修改安装目录下的conf子目录中的server.xml文件:
a.修改http访问端口(默认为8080端口),将8080修改为tomcat不在使用的端口号。此处所设的端口号即是以后访问web时所用的端口号。
b.修改Shutdown端口(默认为8005端口),将8005修改为没有在使用的端口号,例如8055。
c.修改8009端口,将8009修改为没有在使用的端口号,例如8099
(注意:两个文件中对应的端口号要不一样)
3.依次启动两个tomcat。
版本最好不要一样
也就是修改server.xml
把共用的端口分开就行了
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com