设为首页 收藏本站
查看: 438|回复: 0

[经验分享] Tomcat源码分析(十)--部署器

[复制链接]

尚未签到

发表于 2017-1-31 08:26:05 | 显示全部楼层 |阅读模式
  我们知道,在Tomcat的世界里,一个Host容器代表一个虚机器资源,Context容器代表一个应用,所谓的部署器就是能够把Context容器添加进Host容器中去的一个组件。显然,一个Host容器应该拥有一个部署器组件。简单的部署代码应该是下面这样的:

Context context = new StandardContext();
Host host = new StandardHost();
host.addChild(context);
  别看这简单,其实这就是核心的部署代码。当然,Tomcat的部署器绝不是这么点东西,但其实也是比较简单的东西。在Catalina的createStartDigester()方法中(具体怎么调用到这个方法,详细参考Tomcat源码分析(一)--服务启动),向StandardHost容器中添加了一个HostConfig的实例。HostConfig类实现了LifecycleListener接口,也就是说它是个监听器类,能监听到组件的生命周期事件(有关生命周期的东西请参看
Tomcat源码分析(七)--单一启动/关闭机制(生命周期))。 下面看接受事件的方法lifecycleEvent(LifecycleEvent)做了写什么工作:


public void lifecycleEvent(LifecycleEvent event) {
// Identify the host we are associated with
try {
host = (Host) event.getLifecycle();
if (host instanceof StandardHost) { //如果监听到的事件对象类型是StandardHost就设置相关属性。
int hostDebug = ((StandardHost) host).getDebug();
if (hostDebug > this.debug) {
this.debug = hostDebug;
}
setDeployXML(((StandardHost) host).isDeployXML());//是否发布xml文件的标识,默认为true
setLiveDeploy(((StandardHost) host).getLiveDeploy());//是否动态部署标识,默认为true
setUnpackWARs(((StandardHost) host).isUnpackWARs());//是否要将war文件解压缩,默认为true
}
} catch (ClassCastException e) {
log(sm.getString("hostConfig.cce", event.getLifecycle()), e);
return;
}
// Process the event that has occurred
if (event.getType().equals(Lifecycle.START_EVENT)) //监听到容器开始,则调用start方法,方法里面调用了部署应用的代码
start();
else if (event.getType().equals(Lifecycle.STOP_EVENT))
stop();
}
  如果监听到StandardHost容器启动开始了,则调用start方法来,下面看start方法:


protected void start() {
if (debug >= 1)
log(sm.getString("hostConfig.start"));
if (host.getAutoDeploy()) {
deployApps();//发布应用
}
if (isLiveDeploy()) {
threadStart();//动态发布应用,因为HostConfig也实现了Runnable接口,threadStart启动该线程来实现动态发布
}
}
--------------------》deployApps方法,该方法会把webapps目录下的所有目录都看作成一个应用程序
protected void deployApps() {
if (!(host instanceof Deployer))
return;
if (debug >= 1)
log(sm.getString("hostConfig.deploying"));
File appBase = appBase();//返回webapps目录
if (!appBase.exists() || !appBase.isDirectory())
return;
String files[] = appBase.list();//列出webapps目录下的所有文件
deployDescriptors(appBase, files);//通过描述符发布应用
deployWARs(appBase, files);//发布war文件的应用
deployDirectories(appBase, files);//发布目录型的应用
}

  以上三个发布应用的方式大同小异,所以只说说常用的发布方式--目录型的应用,下面看看deployDirectories方法,只写了关键的逻辑:

   protected void deployDirectories(File appBase, String[] files) {
for (int i = 0; i < files.length; i++) {
if (files.equalsIgnoreCase("META-INF"))
continue;
if (files.equalsIgnoreCase("WEB-INF"))
continue;
if (deployed.contains(files))
continue;
File dir = new File(appBase, files);
if (dir.isDirectory()) {
deployed.add(files);
// Make sure there is an application configuration directory
// This is needed if the Context appBase is the same as the
// web server document root to make sure only web applications
// are deployed and not directories for web space.
File webInf = new File(dir, "/WEB-INF");
if (!webInf.exists() || !webInf.isDirectory() ||
!webInf.canRead())
continue;
// Calculate the context path and make sure it is unique
String contextPath = "/" + files;
if (files.equals("ROOT"))
contextPath = "";
if (host.findChild(contextPath) != null)
continue;
// Deploy the application in this directory
log(sm.getString("hostConfig.deployDir", files));
try {
URL url = new URL("file", null, dir.getCanonicalPath());//得到应用的路径,路径的写法是   file://应用名称
((Deployer) host).install(contextPath, url); //安装应用到目录下
} catch (Throwable t) {
log(sm.getString("hostConfig.deployDir.error", files),
t);
}
}
}
}
  


((Deployer) host).install(contextPath, url);会调用到StandardHost的install方法,再由StandardHost转交给StandardHostDeployer的install方法,StandardHostDeployer是一个辅助类,帮助StandardHost来实现发布应用,它实现了Deployer接口,看它的install(URL config, URL war)方法(它有两个install方法,分别用来发布上面不同方式的应用):


public synchronized void install(String contextPath, URL war)
throws IOException {
..............................................
// Calculate the document base for the new web application
host.log(sm.getString("standardHost.installing",
contextPath, war.toString()));
String url = war.toString();
String docBase = null;
if (url.startsWith("jar:")) {   //如果是war类型的应用
url = url.substring(4, url.length() - 2);
}
if (url.startsWith("file://"))//如果是目录类型的应用
docBase = url.substring(7);
else if (url.startsWith("file:"))
docBase = url.substring(5);
else
throw new IllegalArgumentException
(sm.getString("standardHost.warURL", url));
// Install the new web application
try {
Class clazz = Class.forName(host.getContextClass());//host.getContextClass得到的其实是StandardContext,
Context context = (Context) clazz.newInstance();
context.setPath(contextPath);//设置该context的访问路径为contextPath,即我们的应用访问路径
context.setDocBase(docBase);//设置该应用在磁盘的路径
if (context instanceof Lifecycle) {
clazz = Class.forName(host.getConfigClass());//实例化host的监听器类,并关联上context
LifecycleListener listener =
(LifecycleListener) clazz.newInstance();
((Lifecycle) context).addLifecycleListener(listener);
}
host.fireContainerEvent(PRE_INSTALL_EVENT, context);
host.addChild(context);           //添加到host实例,即把context应用发布到host。
host.fireContainerEvent(INSTALL_EVENT, context);
} catch (Exception e) {
host.log(sm.getString("standardHost.installError", contextPath),
e);
throw new IOException(e.toString());
}
}

  


经过上面的代码分析,已经完全了解了怎么发布一个目录型的应用到StandardHost中,其他war包和文件描述符类型的应用发布跟StandardHost大体类似,在这里就不说了,有兴趣的可以自己查看源代码。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-335544-1-1.html 上篇帖子: Tomcat的jsp缓存问题[转] 下篇帖子: tomcat数据源断开重连
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表