部署一个不依赖tomcat容器的应用
一个task项目,应用里边都是一些定时任务。我和新入职的高开商定程序部署不依赖于tomcat。
计划赶不上变化,任务开发完成还没等上线呢,哥们要离职了。工作交接时大概说了一下上线怎么部署。
结果呢,当我在linux测试服务器上部署时,可费了一些周折。之前都是把应用部署到tomcat下面的。那位高开说过,不依赖tomcat容器的部署方式已经不是新概念了。漫长的解决过程中,有同事建议我放弃,沿用tomcat吧。我觉得有必要坚持下来,最终也坚持下来了。
一.先介绍一下项目
工程如下图。assembly/bin下有一个emax-paycenter-task.sh文件,主要是通过nohup命令来运行LauncherMain。高开说了,部署时将该shell文件放到应用的根目录下,通过执行它来启动程序。
emax-paycenter-task.sh文件里是如下命令,start用的是nohup命令:
LauncherMain.java里是一个main方法,用来初始化环境:
import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import org.slf4j.ILoggerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.web.context.support.XmlWebApplicationContext; import java.net.URL; import java.util.concurrent.Semaphore; /** * Description 启动方法 * Date 2018/2/8 上午10:01 */ public class LauncherMain { private static Logger logger= LoggerFactory.getLogger(LauncherMain.class); public static void main(String[] args) throws Exception { logger.info("init @Prop"); Semaphore sp = new Semaphore(0); XmlWebApplicationContext xmlWeb = new XmlWebApplicationContext(); xmlWeb.setConfigLocation("classpath*:*spring/spring-applicationContext.xml"); String logbackCfg = "logback.xml"; URL logURL = new ClassPathResource(logbackCfg).getURL(); ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory(); LoggerContext loggerContext = (LoggerContext) loggerFactory; JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(loggerContext); configurator.doConfigure(logURL); xmlWeb.refresh(); xmlWeb.start(); sp.acquire(); } }
项目中涉及到的定时任务采用Spring的@Scheduled来实现:
@Component public class AgentPayTask { @Autowired private LimitConfigDataHolder limitConfigDataHolder; @Autowired private AgentPayTaskService agentPayTaskService; private static Logger logger = LoggerFactory.getLogger(AgentPayTask.class); @Scheduled(cron ="0/5 * * * * ?") public void process() { System.out.println("AgentPayTask111"); logger.info("AgentPayTask"); Map<String, RateLimiterConfig> LimiterMap = limitConfigDataHolder.getAgentPayLimiter(); agentPayTaskService.distributeTask(LimiterMap); } }
二.linux部署
我在本地直接运行LauncherMain#main方法,程序是没问题的,里面的定时任务都可以正常跑。
将maven打包后的emax-paycenter-task-1.0.0-SNAPSHOT-assembly.tar.gz解压部署到测试服务器上。最终执行命令
./emax-paycenter-task.sh start &
查看生成的nohup.out文件,发现总是找不到class com.emax.paycenter.LauncherMain。
后来咨询运维,发现是没有给目录分配权限所致。
技能娴熟的运维又帮忙把emax-paycenter-task.sh里的命令修正了一下。最终如下:
再次按照上面命令启动程序,查看日志文件发现只有如下一条日志:
2018-03-21 15:11:45.381 [main] INFO [com.emax.paycenter.LauncherMain] - init @Prop
多次测试发现,程序执行到sp.acquire();这条语句时,就不动了;我在这条语句后加打印日志结果也没打印出来;定时任务也没执行。
了解了一下Semaphore信号量,把permits改成0或1或其他数字,都不行。
。。。。。一个小时。。。。
。。。。。两个小时。。。。
找同事帮助,无果。
。。。。。若干小时。。。。
灵感咋现!我将怀疑的焦点放到了spring context的设置这里,是不是压根都没有设置成功呢?
xmlWeb.setConfigLocation("classpath*:*spring/spring-applicationContext.xml");
莫非是各jar包里有同名的配置文件? 将路径参数"classpath*:*spring/spring-applicationContext.xml"改成"classpath:spring/spring-applicationContext.xml"后,查看nohup.out发现出现了新的问题:
[root@localhost emax-paycenter-task]# tail -f nohup.out at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:531) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295) ... 11 more Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.emax.paycenter.api.service.IPayCenterFacade]
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:997) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:867) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503) ... 13 more
同时,logback日志文件里也发现类似问题:spring context初始化时出现了异常。项目中一个类“AgentPayTaskQueryServiceImpl”通过@Autowired依赖注入了另一个type为“IPayCenterFacade”的bean,而在spring context里找不到这个类型为“IPayCenterFacade”的bean(NoSuchBeanDefinitionException),从而导致spring context创建bean “agentPayTaskQueryServiceImpl”失败(BeanCreationException异常)。
[root@localhost emax-paycenter-task]# tail -n200 -f logback-task/task_info.log 2018-03-21 16:16:37.174 [main] INFO [com.emax.paycenter.LauncherMain] - init @Prop 2018-03-21 16:16:39.397 [main] WARN [org.springframework.web.context.support.XmlWebApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'agentPayTaskQueryServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.emax.paycenter.api.service.IPayCenterFacade com.emax.paycenter.service.impl.AgentPayTaskQueryServiceImpl.payCenterFacade; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.emax.paycenter.api.service.IPayCenterFacade] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) ~[spring-context-3.2.13.RELEASE.jar:3.2.13.RELEASE] at com.emax.paycenter.LauncherMain.main(LauncherMain.java:38) [emax-paycenter-task-1.0.0-SNAPSHOT.jar:na] Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.emax.paycenter.api.service.IPayCenterFacade com.emax.paycenter.service.impl.AgentPayTaskQueryServiceImpl.payCenterFacade; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.emax.paycenter.api.service.IPayCenterFacade] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:531) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] ... 11 common frames omitted Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.emax.paycenter.api.service.IPayCenterFacade] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:997) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:867) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503) ~[spring-beans-3.2.13.RELEASE.jar:3.2.13.RELEASE] ... 13 common frames omitted
IPayCenterFacade定义在dubbo配置文件里。因此,将spring applicationContext里对dubbo配置文件的import由
<import resource="classpath*:*spring/dubbo-applicationContext.xml"/>
改成
<import resource="classpath:spring/dubbo-applicationContext.xml"/>
就可以了。
当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--buguge
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/buguge/p/8616665.html