背景
今天將一個SpringBoot項目的配置參數從原有的.yml文件遷移到Apollo后,啟動報錯“Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded because @ConditionalOnBean (types: org.apache.rocketmq.client.producer.DefaultMQProducer; SearchStrategy: all) did not find any beans of type org.apache.rocketmq.client.producer.DefaultMQProducer”。花了兩個小時才最終搞清楚,原因是缺少了配置項 spring.rocketmq.producer.group 從而導致無法成功創建RocketMQAutoConfiguration這個Bean,從而導致一連串對此有依賴的Bean無法創建成功。
排查過程
啟動的錯誤日志
2019-04-02 15:21:33.689 WARN 17516 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myServiceAImpl': Unsatisfied dependency expressed through field 'myServiceBImpl'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myServiceAImpl': Unsatisfied dependency expressed through field 'rocketMQTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 2019-04-02 15:21:33.692 INFO 17516 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed 2019-04-02 15:21:33.693 INFO 17516 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 2019-04-02 15:21:33.693 INFO 17516 --- [ main] f.a.ReferenceAnnotationBeanPostProcessor : class com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor was destroying! 2019-04-02 15:21:33.702 INFO 17516 --- [ main] ConditionEvaluationReportLoggingListener : Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2019-04-02 15:21:33.842 ERROR 17516 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Field rocketMQTemplate in net.yourpackage.myServiceBImpl required a bean of type 'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' that could not be found. - Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded because @ConditionalOnBean (types: org.apache.rocketmq.client.producer.DefaultMQProducer; SearchStrategy: all) did not find any beans of type org.apache.rocketmq.client.producer.DefaultMQProducer Action: Consider revisiting the conditions above or defining a bean of type 'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' in your configuration.
問題原因
從上圖中可以看到RocketMQAutoConfiguration中的mqProducer方法會根據配置參數來創建DefaultMQProducer,其中有兩個必要的參數
- spring.rocketmq.nameServer
- spring.rocketmq.producer.group
在重新檢查了一遍配置文件后發現,的確是因為漏掉了spring.rocketmq.producer.group,將其加上之后便可以成功啟動項目了。
