这周在实训期间尝试本地部署了一个反爬虫的数据源项目,这里将部署的时候遇到的坑以及解决方案记录一下:

部署环境:

Windows + VMWare 15
Ubuntu 18.04

正常部署maven项目步骤为:

将maven生成target目录下的war包导入到tomcat/webapps路径下。修改tomcat文件夹下的conf文件夹下的server.xml文件,定位到Host元素,加入节点Context并修改默认项目名,按照步骤启动tomcat服务器即可通过localhost:8080访问。

报错及解决

但是我的项目在部署之后收到tomcat:500报错。报错详细内容如下:

Configuration problem: Failed to import bean definitions from relative location [spring-mybatis.xml]
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [file:/usr/tomcat/apache-tomcat-8.5.57/webapps/acs/WEB-INF/classes/spring-mybatis.xml]; nested exception is java.lang.LinkageError: loader constraint violation: when resolving method "org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Lorg/w3c/dom/Element;)V" the class loader (instance of org/apache/catalina/loader/ParallelWebappClassLoader) of the current class, org/springframework/transaction/config/AnnotationDrivenBeanDefinitionParser$AopAutoProxyConfigurer, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/springframework/aop/config/AopNamespaceUtils, have different Class objects for the type org/springframework/beans/factory/xml/ParserContext used in the signature

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [file:/usr/tomcat/apache-tomcat-8.5.57/webapps/acs/WEB-INF/classes/spring-mybatis.xml]; nested exception is java.lang.LinkageError: loader constraint violation: when resolving method "org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Lorg/w3c/dom/Element;)V" the class loader (instance of org/apache/catalina/loader/ParallelWebappClassLoader) of the current class, org/springframework/transaction/config/AnnotationDrivenBeanDefinitionParser$AopAutoProxyConfigurer, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/springframework/aop/config/AopNamespaceUtils, have different Class objects for the type org/springframework/beans/factory/xml/ParserContext used in the signature

java.lang.LinkageError: loader constraint violation: when resolving method "org.springframework.aop.config.AopNamespaceUtils.registerAutoProxyCreatorIfNecessary(Lorg/springframework/beans/factory/xml/ParserContext;Lorg/w3c/dom/Element;)V" the class loader (instance of org/apache/catalina/loader/ParallelWebappClassLoader) of the current class, org/springframework/transaction/config/AnnotationDrivenBeanDefinitionParser$AopAutoProxyConfigurer, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, org/springframework/aop/config/AopNamespaceUtils, have different Class objects for the type org/springframework/beans/factory/xml/ParserContext used in the signature

和其他两项报错

spring 项目总是导致服务器cpu spring项目部署时出错_xml


发现报错和aop这个包有关,而这个包是我在maven package打包时手动添加的,因此我想到可能是移交项目依赖环境版本与我本地环境版本不一致导致的。

咨询了学长之后,学长说maven打包时不需要手动添加JAR包的,因此也更坚定了我自己的猜测。

使用命令查看本地maven依赖java版本:

mvn -version

spring 项目总是导致服务器cpu spring项目部署时出错_spring_02


发现依赖的JAVA version与我的本地为了该项目配置的JAVA版本并不一致,maven依赖的是JAVA11,而需要的配置是JAVA8。(这里提一点,听学长说Ubuntu18.04似乎内置了JAVA11,因此需要修改环境变量手动改成JAVA8,萌新并没有深入研究,希望有懂的大神能指点一下!)

通过命令

vim /etc/profile

打开文件中查看,发现我当时的确也配置了JAVA8的环境变量了,不过是将该环境编辑在了该文件的最顶部,可能被后面的内容重新覆盖了(这里不太清除。听学长的建议将配置环境变量内容移到文件最下方保存后source激活,再查看maven版本发现JAVA版本已经变成JAVA8了。

spring 项目总是导致服务器cpu spring项目部署时出错_java_03


再试着重新编译打包部署,继续报错…不过这次报错和上次不一样了而且编译顺利通过!继续解决当下的这个报错!

这个报错大致如下:

Invocation of init method failed; nested exception is java.sql.SQLException: The server time zone value 'PDT' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

这个报错就明了多了,是个SQL的exception说明项目编译已经顺利通过成功运行了,这个报错的大致意思是连接mysql时数据库和系统时区差异所造成的,在JDBC连接的URL后面加上serverTimezone=UTC即可解决问题。

我的具体解决方法:修改jdbc.properties

#原db.url
db.url=jdbc:mysql://localhost:3306/anti_crawler_system?verifyServerCertificate=false&useSSL=true

#新db.url
db.url=jdbc:mysql://localhost:3306/anti_crawler_system?serverTimezone=UTC&verifyServerCertificate=false&useSSL=true

成功解决报错,顺利部署项目