我的 webapp 文件夹中有 3 场战争。其中两个是建立在第三个的服务之上的。我处于测试环境中,即我无法控制他们的架构,所以我无法改变任何事情。那么...
问题:有没有办法在 tomcat 中强制执行部署顺序?
我在 stackoverflow 中这里遇到了一个问题,但没有解决办法。
好吧,实际上这个人建议将网络应用程序的名称更改为按字母顺序排列会有所帮助。但是,我不愿意每次需要测试这些战争和不同的战争时都这样做。
我确信有一种方法可以配置 tomcat conf .xml 文件。我只是不知道是哪一个。
I have 3 wars in my webapp folder. Two of them are built on services of the third one. I'm in a testing environment, i.e. I don't have control over their architectures, so I'm no able to change a thing. So...
Question: Is there a way to enforce a deployment order in tomcat?
I've ran into one question here in stackoverflow, but there's no solution.
Well, actually the guy suggests that changing the name of the webapps to an alphabetical order would help. However, I'm not willing to do that everytime I need to test those and different wars.
I'm positive that there's a way of doing that configuring one of the tomcat conf .xml files. I just don't know which one.
发布评论
评论(8)
来自 Tomcat Wiki - webapp 启动的顺序是什么(或如何更改启动顺序)?< /a>
Tomcat 从不支持指定 Web 应用程序的加载顺序。还有其他容器,例如 JBoss ,但 Tomcat 从来没有。任何看起来像通过 Web 应用程序名称的字母顺序进行加载排序的明显行为都是巧合,并且不能保证在所有情况下都有效。
如果 元素>。
web.xml
指定加载servlet
也就是说,有一个使用服务发现协议的优雅解决方案。
一种解决方案是使用 ZeroConf 之类的内容并在服务启动时注册您的服务,然后让依赖的应用程序查看当这些服务可用并让它们连接并在服务准备好时执行它们需要执行的操作。这就是我多年来处理多项依赖服务的方式。我有 Python、Java 和 Erlang 服务,它们都通过 ZeroConf 无缝地发现彼此。
From the Tomcat Wiki - What order do webapps start (or How can I change startup order)?
Tomcat has never supported specifying load order of webapps. There are other containers like JBoss that do, but Tomcat never has. Any apparent behavior that looks like load ordering via the alphabetical order of the names of the web apps is coincidental and not guaranteed to work in all cases.
What you are probably thinking of is the
<load-on-startup/>
element if theweb.xml
that specifies order of loadingservlets
.That said there is an elegant solution using a service discovery protocol.
One solution is to use something like ZeroConf and register your services when they start up, and then have the dependent apps look for when these services come available and have them connect and do what ever they need to do when the service is ready. This is how I have been handling multiple dependent services for years now. I have Python, Java and Erlang services all discovering each other via ZeroConf seemlessly.
确实,tomcat 没有提供任何强制部署顺序的方法。
*Tomcat 按以下顺序部署 Web 应用程序:*
1. 将首先部署任何上下文描述符。
2.然后将部署未被任何上下文描述符引用的分解的Web应用程序。如果它们在 appBase 中有关联的 .WAR 文件,并且它比分解的 Web 应用程序更新,则分解的目录将被删除,并且 web 应用程序将从 .WAR 重新部署
。 3.WAR 文件将被部署
>这里是一个建议的解决方案:
如果您想指定部署顺序,请在 $CATALINA_BASE/conf/[enginename]/[hostname]/MyApp.xml 中为您的 Web 应用程序定义上下文
Tomcat 扫描 $CATALINA_BASE/conf/ [enginename]/[hostname]/ 通过执行 File listFiles() 返回按哈希值排序的 File 数组(取决于操作系统)。
您可以使用以下代码来检查 webapp 的部署顺序
It is true that tomcat does not provide any way to enforce deployment order.
*Tomcat deploys webapps in following order:*
1.Any Context Descriptors will be deployed first.
2.Exploded web applications not referenced by any Context Descriptor will then be deployed. If they have an associated .WAR file in the appBase and it is newer than the exploded web application, the exploded directory will be removed and the webapp will be redeployed from the .WAR
3.WAR files will be deployed
> Here is a proposed solution:
If you want to specify the deployment order then define a context for your web app in $CATALINA_BASE/conf/[enginename]/[hostname]/MyApp.xml
Tomcat scans $CATALINA_BASE/conf/[enginename]/[hostname]/ by performing File listFiles() which returns a File array sorted by hash value (OS dependent).
You may use the following code to check in which order webapps will be deployed
在 Tomcat 中部署
webapp.war
的方法有3种。在
$CATALINA_BASE/conf/server.xml
文件的Host
元素内添加Context
元素。创建
$CATALINA_BASE/conf/[engineName]/[hostName]/[webappName].xml
文件,内容如下:添加
webapp.war直接在
文件。$CATALINA_BASE/webapps/
目录中添加Tomcat 启动时将发生以下部署顺序:
1→2→3
一些说明:
$CATALINA_BASE
<块引用>
引用解析大多数相对路径的基目录。如果您尚未通过设置 CATALINA_BASE 目录来为多个实例配置 Tomcat,则 $CATALINA_BASE 将设置为 $CATALINA_HOME 的值,即您安装 Tomcat 的目录。
文档库
<块引用>
Web 应用程序 WAR 文件的路径名。您可以为此 WAR 文件指定绝对路径名,或相对于所属主机的 appBase 目录的路径名。
引擎名称:
与上下文关联的引擎的名称。默认名称为
Catalina
。主机名:
与上下文关联的主机的名称。默认名称为
localhost
。假设:
a.war
、b.war
和c.war
。b.war
依赖于a.war
。c.war
依赖于b.war
。请尝试以下步骤:
$CATALINA_BASE/webapps/
目录。创建
$CATALINA_BASE/conf/Catalina/localhost/b.xml
文件,内容如下:在
中添加
文件:Context
元素>$CATALINA_BASE/conf/server.xml参考:
There are three ways to deploy the
webapp.war
in Tomcat.Add
Context
element inside aHost
element in the$CATALINA_BASE/conf/server.xml
file.Create the
$CATALINA_BASE/conf/[engineName]/[hostName]/[webappName].xml
file with content:Add the
webapp.war
file directly in the$CATALINA_BASE/webapps/
directory.The following deployment sequence will occur on Tomcat startup:
1→2→3
Some explanations:
$CATALINA_BASE
docBase
engineName:
The name of the engine associated to the context. The default name is
Catalina
.hostName:
The name of the host associated to the context. The default name is
localhost
.Assume that:
a.war
,b.war
andc.war
.b.war
depends ona.war
.c.war
depends onb.war
.Try the following steps:
$CATALINA_BASE/webapps/
directory.create the
$CATALINA_BASE/conf/Catalina/localhost/b.xml
file with content:Add
Context
element in the$CATALINA_BASE/conf/server.xml
file:Reference:
对交叉发布表示歉意(Tomcat - 以特定顺序启动 webapps) ,但我认为这也与此相关:
您可以在 server.xml 中定义多个服务,这些服务在不同的端口上运行。服务根据它们在 server.xml 中出现的顺序依次启动。这意味着您可以 - 例如 - 在第一个服务中运行配置服务,然后在第二个服务中运行依赖于它的应用程序(我对其余服务使用默认的 Catalina...)
您可以查看更多信息这里:
http://wiki.apache.org/tomcat/FAQ/Miscellaneous#Q27
这是我在 Catalina 服务之前包含的服务:
如您所见,我使用 docbase 而不是 appBase,但是如果您更喜欢...
注意,更改服务和引擎的名称很重要。
华泰
Apologies for cross posting (Tomcat - starting webapps in a specific order), but I thought it was relevant here too:
You can define mulitple services in your server.xml, which run on different ports. The services are started sequentially according to the order they appear in the server.xml. This means that you can have - for example - a configuration service running in the first service and then the apps that depend on it in the second (I use the default Catalina one for the rest of them...)
You can see more info here:
http://wiki.apache.org/tomcat/FAQ/Miscellaneous#Q27
And this is the service that I include before the Catalina Service:
As you can see, I use docbase rather than appBase, but you should be able t configure a different appBase if you prefer...
NB it's important to change the name of both the service and the engine.
HTH
您始终可以将第一个 .war 文件移至 tomcat 目录,然后等待其部署,然后将接下来的两个文件移至 tomcat 目录。这将部署第一个,然后部署另外两个。
或者,您可以使用 Tomcat 将扫描目录并按字母顺序部署第一个战争,然后是第二个战争,然后是第三个战争的事实
You could always move the first .war file into the tomcat directory, then wait for it to deploy, then move the next two into the tomcat directory. This would deploy the first, then deploy the other two.
Alternatively you can possibly use the fact that Tomcat will scan the directory and deploy the first war, then second war, then third war alphabetically
如果您不介意修改一些 tomcat 代码并创建自己的 Host 实例,那么这很容易实现:
1) 创建 org.apache.catalina.core.StandardHost 的子类,例如 MyHost:
2) 在服务器上注册您的类xml Host tag ()
看起来令人难以置信,只要您在 Host 标记内以正确的顺序声明所有 Web 应用程序,它就可以解决问题:
无论您使用哪个 SO,Thaen app1 都会在 app2 之前启动。
That's quite easy to achieve if you don't care hacking a bit of tomcat code and creating your own Host instance
1) Create a subClass of org.apache.catalina.core.StandardHost, say MyHost:
2) register your class on your server's xml Host tag ()
Incredible as it may seem, it solves the problem as long as you have all your web app declared in the correct order inside of Host tag:
Thaen app1 will start before app2, no matter which SO you used.
我们运行多个雄猫。我们可以通过将第一组应用程序放入一个 tomcat 中并将后面的应用程序放入第二个 tomcat 中来独立地启动应用程序组。
这假设它们没有尝试使用 JVM 相互通信。
We run multiple tomcats. We can bring up groups of apps independently by putting the first group in one tomcat and the later ones in a second.
This assumes that they are not trying to communicate with each other using the JVM.
基于 @Luiz 对 Tomcat 9 的回答(其中
children
现在是最终版本),我们必须重写几个方法,在某些情况下复制基本功能。这没有针对任何内容进行优化,但它确实按照
server.xml
中定义的顺序加载上下文。获取 tomcat-catalina-*.jar,例如从 Maven 存储库并将其编译为
DeterministicDeployOrderHost.class
:然后将编译后的
.class
文件复制到$CATALINA_HOME/lib
并将以下内容添加到主机:Building on from @Luiz's answer for Tomcat 9 (in which
children
is now final), we had to override several methods, copying the basic functionality in some instances.This isn't optimised for anything, but it does load the contexts in the order they're defined in your
server.xml
.Get the tomcat-catalina-*.jar, e.g. from Maven Repository and compile this into
DeterministicDeployOrderHost.class
:Then copy the compiled
.class
file into$CATALINA_HOME/lib
and add the following to the host: