Spring+struts联网正常,断网启动报错:java.net.UnknownHostException: struts.apache.org

很奇怪,程序在各个环境正常运行快一年来,这个月中旬,公司搬迁,机房和网络都换,服务器启动都正常,也就没有关注了。今天开发跟我说,应用起不来,报java.net.UnknownHostException: struts.apache.org错。觉得很奇怪,服务器自从搭建好后,一直就没有改过,怎么忽然出错了。

经过漫长的查找定位,发现了一个问题,就是之前所有环境的服务器都可以连接网络的。现在机房里的机器,都不能连接外网了。在本地验证,断开网络,程序启动报错,联通网络,程序又正常。估计问题就出现网络上了。

查看系统日志,发现解析struts.xml文件报错,而且是联网错误java.net.UnknownHostException: struts.apache.org。通过查看strtus.xml文件,发现只有
http://struts.apache.org/dtds/struts-2.3.dtd需要联网的。估计问题就在这里来。struts加载的时候,可能找不到/默认不从本地查找dtd文件,自动到网络上查找,而服务器没有来内网,导致校验出错。

通过查找项目中的struts-core-2.3.16.jar,发现里面已经包含了2.3.dtd的文件,但是不知道为什么还要联网查找,没有深入追究,先解决开发的问题再说。

解决方法:
将struts.xml文件声明的dtd修改为本地验证,不使用网络上的文件:
即将http://struts.apache.org/dtds/struts-2.3.dtd替换为classpath:struts-2.3.dtd。如下:
<!--DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd"-->

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "classpath:struts-2.3.dtd">

重启服务器,系统正常了。如果不行可能需要将struts-2.3.dtd文件copy到src(classpath)下面(我的没有修改)。


以下是系统启动的错误信息:
2016-01-27 10:16:32,561 INFO  org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh:510 - Refreshing Root WebApplicationContext: startup date [Wed Jan 27 10:16:32 CST 2016]; root of context hierarchy
2016-01-27 10:16:32,650 INFO  [b]org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions:316 - Loading XML bean definitions from URL [file:/usr/local/apache-tomcat-8.0.20/webapps/cms/WEB-INF/classes/applicationContext-profiles.xml][/b]
2016-01-27 10:16:32,896 INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions:316 - Loading XML bean definitions from class path resource [applicationContext.xml]
2016-01-27 10:16:35,139 INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions:316 - Loading XML bean definitions from class path resource [struts.xml]
2016-01-27 10:16:40,208 ERROR org.springframework.web.context.ContextLoader.initWebApplicationContext:220 - Context initialization failed
[b]org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [struts.xml]; nested exception is java.net.UnknownHostException: struts.apache.org[/b]
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:409)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
        ..............
Caused by: java.net.UnknownHostException: struts.apache.org
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        ..............
27-Jan-2016 10:16:40.240 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Error listener Start
27-Jan-2016 10:16:40.268 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [/cms] startup failed due to previous errors
27-Jan-2016 10:16:40.286 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThre ads The web application [cms] appears to have started a thread named [FileWatchdog] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Thread.sleep(Native Method)

猜你喜欢

转载自xieyanhua.iteye.com/blog/2274216