您的位置:首页 > 运维架构 > Tomcat

Java中间件 - Tomcat 7 配置和部署

2016-06-03 11:00 721 查看
Copyright 2016 by 蔡舒啸 保持署名-非商业性使用-相同方式共享 Creative Commons BY-NC-ND 3.0

目录

Tomcat

热部署
webapps加载顺序

bin目录
bincatalinabat和setclasspathbat
指定Tomcat使用的JDK版本

conf目录
指定启动Tomcat时是否解压war包
confserverxml

配置数据库链接资源在项目中用JNDI获得该资源
confcontextxml

confserverxml

contextxml加密

tomcat-userxml 通过网页配置tomcat

webxml
定义全局常量

中间件 - 通过中间件, 各种编程语言之间, 甚至各种异构的系统之间都可以相互通信, 比如, php项目访问Java中间件的网址, 如http://127.0.0.1:8080/WebApp并获得返回值. 这种跨编程语言的调用太方便了!

小项目,或者是个人开发tomcat,apache hpptd, jboss,glasshfish,jetty;

大型的项目(基于EJB)就用JBOSS,webloigc/webshere

Tomcat

文件结构如下:

${tomcat目录}
|--bin/
|--conf/
|  |--context.xml
|  |--server.xml
|  |--tomcat-users.xml
|  +--web.xml
|
|--lib/
|--logs/
+--webapps/


其中,bin/是启动tomcat的脚本startup.bat

webapps/是项目war包的位置

conf/是tomcat全局配置文件的位置

热部署

请看http://www.cnblogs.com/xing901022/p/4463896.html

部署时,涉及到一个变量appBase。这个变量标识了一个目录,该目录存放着部署的web应用。

一般默认情况下,appBase为CATALINA_HOME/webapps,配置信息位于server.xml中。

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">


appBase指定了部署的目录;

autoDeploy设置是否自动部署(下小节动态部署中会介绍);

unpackWARs设置了部署的形式,如果为true,则会以展开的形式部署;如果为false,则会以war包的形式部署。

webapps加载顺序

文档中说明了不同种类的应用的部属顺序。

1 有context配置的应用优先启动;

2 以目录形式部署的应用随后启动;

3 以war文件部署的应用最后启动。

bin目录

1 bin/catalina.bat和setclasspath.bat

指定Tomcat使用的JDK版本

同时修改tomcat启动配置tomcat/bin/catalina.bat和/bin/setclasspath.bat,分别增加

set JAVA_HOME=xxxxx


可以指定启动的JDK版本

conf目录

本文的重点就是conf/目录下各个xml文件的配置

1 指定启动Tomcat时是否解压war包

conf/server.xml

Host name="localhost"  appBase="webapps"
unpackWARs="false" <!--tomcat启动时是否解压appBase指定目录下的.war文件并覆盖已有的文件夹-->
autoDeploy="true"
xmlValidation="false"
xmlNamespaceAware="false"


2 配置数据库链接资源,在项目中用JNDI获得该资源

JNDI - Java Name and Directory Interface(Java名字和目录接口),说人话就是,有一个姑娘叫小芳,“小芳”是姑娘在JNDI中定义的名字,我们通过“小芳”就找到了姑娘,然后我们可以和姑娘聊天,约会。

我们以添加一个JNDI中叫做“foo”的数据库JDBC链接为例:

配置JNDI需要修改conf/context.xml和conf/server.xml

conf/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
此处省略license声明
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 -->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- define JNDI name="foo" -->
<ResourceLink global="foo" name="foo" type="javax.sql.DataSource"/>
</Context>


conf/server.xml

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
<Resource auth="Container" name="foo" driverClassName="org.postgresql.Driver" maxActive="100" maxIdle="30" maxWait="10000" password="xxxx" type="javax.sql.DataSource" url="jdbc:postgresql://xxx.xxx.xxx.xxx:xxxx/xxxx" username="xxxx"/>
</GlobalNamingResources>
</Server>


如何使用呢?

以Spring框架为例子,在WEB-INF/[项目名]-servlet.xml中添加:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Bean头部 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <!-- 配置JNDI为"foo"的数据源 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
lazy-init="true">
<property name="jndiName" value="name="foo" />
<property name="resourceRef" value="true" />
</bean>
<!-- 配置Spring JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
</beans>


在代码里使用

@Service
public class Api1_ProjectDetail extends AbstractSPService{
@Autowired
private JdbcTemplate jdbcTemplate; // 通过JdbcTemplate获得tomcat配置好的JNDI:"foo"数据库资源

public String anyMethod(String... paras) {
Connection conn = null;
try{
conn =jdbcTemplate.getDataSource().getConnection();
}catch(Exception e){
//
} finally {
if(conn!=null){
conn.close();
}
}
}
}


3 context.xml加密

转自http://www.cnblogs.com/mabaishui/archive/2011/07/14/2106469.html

近日客户要求对tomcat配置文件context的数据库用户名和密码加密。

解密程序已写好,只需在取得用户名密码的时候调用解密程序即可。

context配置如下:

<Context path="/web" docBase="E:\izumi_workspace\izumi\WebContent"  reloadable="false">
<Resource
name="jdbc/izumidb1"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
password="67461e40361806c4"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="10"
maxWait="5000"
username="67461e40361806c4"
removeAbandoned="true"
removeAbandonedTimeout="60"
validationQuery="select count(*) from izumi_user"
logAbandoned="true"
url="jdbc:microsoft:sqlserver://XX.XXX.XX.XXX:1433;DataBaseName=test2"
maxActive="30"/>
</Context>


为何要修改tomcat代码?

程序中使用这种方式取得Resource中定义的数据库

Context ctx = (Context) new InitialContext().lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/izumidb1");


因为javax.sql.DataSource是一个接口,是tomcat实现了这个接口,所以解密过程需要修改tomcat的代码。

修改哪里?

修改org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory的createDataSource()方法即可。

在apache.org找到 BasicDataSourceFactory的源码(最好是你当前使用的tomcat的版本的源码)

自建 org.apache.tomcat.dbcp.dbcp包,把BasicDataSourceFactory源码扔到包里,如果源码有import到其他类,把其他类也扔到包里,做解密修改。

修改完后放哪里?

在tomcat/common/lib中有关于dbcp的包,不同版本的tomcat可能包名不同。找到org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory所在的包,将编译好的我们的BasicDataSourceFactory类的.class文件放到包里,覆盖原来的BasicDataSourceFactory.class。

4 tomcat-user.xml 通过网页配置tomcat

For example, to add the manager-gui role to a user named tomcat with a password of s3cret, add the following to the config file listed above.

<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>


Note that for Tomcat 7, the manager application four roles:
manager-gui - allows access to the HTML GUI and the status pages

manager-script - allows access to the text interface and the status pages

manager-jmx - allows access to the JMX proxy and the status pages

manager-status - allows access to the status pages only

配置好了重启tomcat,就可以通过 http://localhost:8080/manager/html页面配置tomcat了,包括上传war包

web.xml

每个WebApp项目都有一个自己的web.xml,位于
[项目名]/WEB-INF/web.xml


tomcat/conf/web.xml顾名思义和项目里的web.xml功能类似,只不过它定义的变量对所有项目都有效

定义全局常量

<context-param>
<param-name>foo</param-name>
<param-value>foo.properties</param-value>
</context-param>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  tomcat 中间件