暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Tomcat的自动部署

中间件技术讨论圈 2017-01-17
476

前面两节,我们讲过了Tomcat的reload和重部署。

context.reload是Tomcat独有的应用重载功能,应用中的类和web.xml修改的话会开启这个快速的功能,当然前提之下,你得在Context级别配置<Loader reloadable=true></loader>。

重部署是当应用包发生变化了(通常是改名),或者context.xml发生改变后,应用需要重新卸载然后再部署,这个操作流程为重部署。


今天我们来聊聊,Tomcat自动部署。


什么是自动部署呢?

就是一个目录,该目录是允许用户往一个自动部署目录里扔一些部署介质的,有一个线程,不断的针对这个自动部署目录进行扫描:

1.如果发现该目录有新文件,那么调用部署接口继续部署

2.如果发现该目录文件的时间戳发生变化,或者context.xml发生变化,那么调用重部署接口进行重部署

3.如果发现该目录的对应文件消失,那么调用解部署接口进行应用解部署

可以分析,自动部署其实是一个部署机制,其最终都是调用实际的部署接口层的部署,解部署,重部署接口进行操作。


对于自动部署在Host的配置中有详细的文档描述:

Automatic Application Deployment

If you are using the standard Host implementation with default settings then applications in the appBase or with context files in the configBase are automatically deployed when Tomcat starts (thedeployOnStartup
 property defaults to true
) and reloaded or redeployed (as appropriate) when a change is detected while Tomcat is running (the autoDeploy
 attribute also defaults to true
).


一共Host有两个属性,可以支持自动部署,autoDeploy
 和 deployOnStartup
  :

autoDeploy

This flag value indicates if Tomcat should check periodically for new or updated web applications while Tomcat is running. If true, Tomcat periodically checks theappBase
 and xmlBase
 directories and deploys any new web applications or context XML descriptors found. Updated web applications or context XML descriptors will trigger a reload of the web application. The flag's value defaults to true. See Automatic Application Deployment for more information.

autoDeploy是一个Host虚拟主机属性,默认是开启的,其指示在当前虚拟主机下的应用放到webapp目录下,都可以进行自动部署;


其实这个属性,我们前面在HostConfig中看到过其开关的实现:

Tomcat的周期事件会触发check方法,该方法我们上一节讲重部署已经分析过,但是分析的主要是checkResources的细节,而忽略了最上面的autodeploy开关;


当这个autodeploy要设置为false的话,整个Tomcat的自动部署功能就不灵了,其体现为当你Tomcat启动起来的时候,你往webapps中扔部署war包是没有反应的,你修改原有部署文件也没有反应;


还有一个属性是初始化部署,deployOnStartup
 :

deployOnStartup

This flag value indicates if web applications from this host should be automatically deployed when Tomcat starts. The flag's value defaults to true. See Automatic Application Deployment for more information.

该属性的实现其实也很简单,StandardHost容器组件启动的时候,容器启动方法start方法:

读取属性是否为开启(默认就是开启的),如果开启直接调用deployApps接口,也就是部署接口进行webapps目录下的应用部署;

 

deployOnStartup
 and autoDeploy
 trigger execution of exactly the same code so the behaviour is very similar. However, there is one key difference. When Tomcat starts it has no knowledge of which files are the same, which have been changed and which are new. It therefore treats all files as new. While Tomcat is running, it can differentiate between unchanged, modified and new files. This leads to some differences in behaviour between files being modified while Tomcat is running and files being modified while Tomcat is stopped.


Tomcat的这两个属性其实代码执行的部分比较类似,区别在于一个是启动时,一个是运行时;


When you use automatic deployment, related files (a web application may have a context.xml file, a WAR and a directory) that exist in the Host's appBase and/or configBase must conform to the expectednaming convention. In short, this means files for the same web application must share the same base name.

The automatic deployment process identifies new and/or modified web applications using the following search order:

  1. Web applications with a context.xml file located in the Host's configBase.

  2. Web applications with a WAR file located in the Host's appBase that have not already been identified during the scan for context.xml files.

  3. Web applications with a directory located in the Host's appBase that have not already been identified during the scans for context.xml and/or WAR files.


那么Tomcat中的自动部署如何识别待部署的文件呢?

1.context.xml(conf文件中定义的)

2.新的war包(对应conf文件中的context.xml没有====》这也就意味着该war包需要部署,如果有了,代表着该应用已经被部署过了)

3.新的应用目录(对应conf文件中的context.xml没有 ,并且webapps中没有对应的同名war包)


When autoDeploy
 is true
, the automatic deployment process will monitor the deployed web applications for changes. Depending on exactly what changes, the web application will either be re-deployed or reloaded. Re-deployment involves the creation of a new web application and, if using the standard session manager, user sessions will not be retained. Reloading uses the existing web application but re-parses the web.xml and reloads any classes. If using the standard session manager, user sessions will be persisted.


这一段是讲解reload和redeploy的区别的,

reload是重载web.xml和classes,session会丢失;

redeploy是重部署,standardContext都需要重新创建;


Users may add to the files that the automatic deployment process monitors for reloading (i.e. any change to one of these files triggers a reload of the web application) by adding a WatchedResourceselement to the context.xml file. See the Context documentation for further details.


用户提供了WatchedResources 的配置,可以对自动部署进行监视; 


When using automatic deployment, the docBase
 defined by an XML Context file should be outside of the appBase
 directory. If this is not the case, difficulties may be experienced deploying the web application or the application may be deployed twice. The deployIgnore
 attribute can be used to avoid this situation.


docBase的属性可以将应用定义在webapps外面(默认的是appBase目录就是webapps)


Note that if you are defining contexts explicitly in server.xml, you should probably turn off automatic application deployment or specify deployIgnore
 carefully. Otherwise, the web applications will each be deployed twice, and that may cause problems for the applications.


Host配置中有一个可以针对于当前自动部署目录中待部署的应用过滤的一个功能:

deployIgnore

A regular expression defining paths to ignore when autoDeploy
 and deployOnStartup
 are set. This allows you to keep your configuration in a version control system, for example, and not deploy a .svn or CVS folder that happens to be in the appBase
.

This regular expression is relative to appBase
. It is also anchored, meaning the match is performed against the entire file/directory name. So, foo
 matches only a file or directory named foo
 but not foo.war
foobar
, or myfooapp
. To match anything with "foo", you could use .*foo.*
.

See Automatic Application Deployment for more information.

这个属性也有很大的作用,举例来说,.svn,.bak 这种干扰项可以根据这个配置排除;


其实现也很简单,在部署接口中会有一个filterAppPaths的过滤,当你配置这个属性的时候,在部署列表中进行过滤:

 


There are many possible combinations of settings, new files, changed files and deleted files. A separate page describes the expected behaviour of the automatic deployment process in many of these scenarios.


文章转载自中间件技术讨论圈,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论