简体   繁体   English

如何在tomcat7和JDBC jar中部署.war文件?

[英]How to deploy .war file in tomcat7 along with JDBC jar ?

I am trying to deploy a war file in tomcat7. 我正在尝试在tomcat7中部署War文件。 I am using MySQL JDBC driver to connect to database server. 我正在使用MySQL JDBC驱动程序连接数据库服务器。 The jar file of MySQL JDBC driver is copied to $CATALINA_HOME/lib/ directory and the web application works correctly. MySQL JDBC驱动程序的jar文件已复制到$CATALINA_HOME/lib/目录,并且Web应用程序可以正常工作。

Now, I want to deploy the .war file in Amazon Elastic BeanStalk service. 现在,我想在Amazon Elastic BeanStalk服务中部署.war文件。 By default, Amazon doesn't place the MySQL JDBC driver in $CATALINA_HOME/lib directory by default and I can't run a script which will ssh into each instance and download the jar in the directory. 默认情况下,默认情况下,Amazon不会将MySQL JDBC驱动程序放在$CATALINA_HOME/lib目录中,并且我无法运行将ssh放入每个实例并将该jar下载到该目录中的脚本。

Is there any way, in which I can bundle the jar file for JDBC driver , so that I don't have to download and place the jar file in $CATALINA_HOME/lib directory ? 有什么方法可以将jar文件捆绑为JDBC驱动程序使用,从而不必下载jar文件并将其放置在$CATALINA_HOME/lib目录中?

You can place the jar file in an S3 bucket, and then use a EB container command to copy the file to the lib directory, like: 您可以将jar文件放在S3存储桶中,然后使用EB容器命令将文件复制到lib目录,例如:

"copy-lib-file":
  mode: "000644"
  owner: root
  group: root
  source: https://s3.amazonaws.com/<MY_BUCKET>/<my-JDBC-driver>.jar

Tomcat, like many application servers, installs a variety of class loaders (implementation of java.lang.ClassLoader) that will give your application access to the JDBC jar resource. Tomcat与许多应用程序服务器一样,安装了各种类装入器(java.lang.ClassLoader的实现),这些类装入器将使您的应用程序可以访问JDBC jar资源。

The order is: 顺序是:

  1. Bootstrap 引导程序
  2. System 系统
  3. Common - $CATALINA_BASE/lib 常见-$ CATALINA_BASE / lib
  4. Webapp - /WEB-INF/lib Webapp-/ WEB-INF / lib

Put the jar into the WEB-INF/lib directory inside the application. 将jar放入应用程序内的WEB-INF / lib目录中。 Note, this JAR will be only visible to this specific application and no others. 请注意,此JAR仅对该特定应用程序可见,而对其他应用程序则不可见。

In order to do so you need to use . 为此,您需要使用。 ebextensions . ebextensions With .ebextensions you can copy files from your application package to the beanstalk file system. 使用.ebextensions,可以将文件从应用程序包复制到beantalk文件系统。

However keep in mind that no other application deployed in a beanstalk environment will use your driver. 但是请记住,在beantalk环境中部署的其他任何应用程序都不会使用您的驱动程序。 Your beanstalk instance is dedicated to one application. 您的beantalk实例专用于一个应用程序。 Every time you upload the application you have to upload the mysql driver in order to be deployed to the tomcat installation. 每次上载应用程序时,都必须上载mysql驱动程序,以便将其部署到tomcat安装中。

Therefore you do not gain anything in terms of upload size or memory footprint in a tomcat (shared jdbc connetion pool through jndi). 因此,对于tomcat(通过jndi共享的jdbc connetion池)中的上传大小或内存占用,您不会获得任何好处。

It seems that uploading your application with the jdbc driver included (non provided in your maven config) is a more beanstalk oriented solution. 似乎使用附带的jdbc驱动程序(您的maven配置中未提供)上载您的应用程序是一种更加面向beantalk的解决方案。

However there are cases you don't want to make any changes in the way your war gets packaged, for example your application gets deployed to an on premises tomcat server containing the jdbc driver and you want to upload it to an elastic beanstalk environment. 但是,在某些情况下,您可能不想对战争的打包方式进行任何更改,例如,将应用程序部署到包含jdbc驱动程序的本地tomcat服务器上,并且希望将其上传到弹性beantalk环境中。 In such cases you can consider docker with elastic beanstalk as an option too. 在这种情况下,您也可以考虑使用带有弹性beantalk的Docker

No you can't bundle JDBC JARS for Tomcat7: 不,您不能为Tomcat7捆绑JDBC JARS:

I've had this issue and it was a nightmare to debug. 我遇到了这个问题,这是调试的噩梦。 In my trials and in the documentation you can't bundle JDBC drivers inside your WAR file. 在我的试用版和文档中,您无法在WAR文件中捆绑JDBC驱动程序。 Or at least you can but the classloader will ignore JDBC classes that are not in the Tomcat Lib folder. 或者至少可以,但是类加载器将忽略不在Tomcat Lib文件夹中的JDBC类。 Its in the first paragraph of the Mysql section of the documentation here -> Tomcat7-JDBC I don't know of Tomcat8 or Tomcat9 beta... 它在文档的Mysql部分的第一段中-> Tomcat7-JDBC,我不知道Tomcat8或Tomcat9 beta ...

Short Term Solution 短期解决方案

What I do is exactly what you said you don't want to do and similar to Mark B's solution. 我所做的正是您所说的您不想做的事情,类似于Mark B的解决方案。 I use a script that copies it from s3 but this is really easy and is only 1 line of bash if you use the aws s3 cp command. 我使用了一个从s3复制它的脚本,但这确实很容易,如果使用aws s3 cp命令,则只有1行bash。 The aws s3 tool comes installed on the EC2 instance your application will be running on. 在您的应用程序将运行的EC2实例上安装了aws s3工具。

aws s3 cp s3://mybucket/mysql.jar /usr/share/tomcat7/lib/mysql.jar

*You will need to restart tomcat another reason you should see the longterm solution *您需要重新启动tomcat的另一个原因是您应该看到长期解决方案

Long Term Real Solution = Automate Your Build Steps 长期的实际解决方案=自动化您的构建步骤

In the end you'll eventually have to run provisioning scripts if your application becomes complex, which is why I stopped using elastic beanstalk and started using AWS cloudformation which gives you a "STEP" where you can throw in all your setup scripts similar to docker build steps. 最后,如果您的应用程序变得复杂,您最终将必须运行配置脚本,这就是为什么我停止使用弹性beantalk并开始使用AWS cloudformation的原因,这为您提供了一个“ STEP”,您可以在其中插入所有类似于docker的安装脚本建立步骤。 It will also run those configuration steps for every new instance that gets created so there is no needed for you to ssh onto every box. 它还将为创建的每个新实例运行这些配置步骤,因此您无需ssh进入每个框。

Cloudformation is all about turning your infrastructure setup into code which you could actually check into github and build without any manual intervention. Cloudformation就是将基础设施设置转换为代码,您实际上可以将它们检入github并进行构建,而无需任何手动干预。 You go through the headache of configuring your build scripts once then save your environment as a json or yaml file. 您需要一次配置构建脚本的麻烦,然后将环境另存为json或yaml文件。 You can include the MySQL server, tomcat version, firewalls, load balancers etc etc and build that all from a file. 您可以包括MySQL服务器,tomcat版本,防火墙,负载均衡器等,并通过文件进行构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM