停止 spring boot 应用程序启动,直到 spring cloud config server 启动

Posted

技术标签:

【中文标题】停止 spring boot 应用程序启动,直到 spring cloud config server 启动【英文标题】:Halt spring boot application startup until spring cloud config server is up 【发布时间】:2018-03-18 08:44:23 【问题描述】:

我正在尝试使用以下属性集运行我的 Spring Boot 应用程序,我希望它会继续重试从配置服务器加载属性 50 次,在任何两次尝试之间等待 6 秒,即使之后它也无法连接到配置服务器,它应该恢复启动或退出(我对重试后弹簧引导能够做什么无动于衷)。但它的行为似乎不像预期的那样。

我的启动命令——

java -jar -Dspring.application.name=maas-gateway -Dspring.cloud.config.uri=http://localhost:8888 -Dspring.cloud.config.failFast=false -Dspring.cloud.config.retry.initialInterval=5000 -Dspring.cloud.config.retry.maxInterval=7000 -Dspring.cloud.config.retry.maxAttempts=50 maas-gateway-1.0-SNAPSHOT.jar

但我收到以下错误,因为它在一次尝试后恢复启动。

    2017-10-06 09:55:05.881  INFO [] 3598 --- [                main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@53e25b76: startup date [Fri Oct 06 09:55:05 EDT 2017]; root of context hierarchy
    2017-10-06 09:55:06.136  INFO [] 3598 --- [                main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    2017-10-06 09:55:06.166  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$bbc9d7cf] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
      __  __              _____        _____       _
     |  \/  |            / ____|      / ____|     | |
     | \  / | __ _  __ _| (___ ______| |  __  __ _| |_ _____      ____ _ _   _
     | |\/| |/ _` |/ _` |\___ \______| | |_ |/ _` | __/ _ \ \ /\ / / _` | | | |
     | |  | | (_| | (_| |____) |     | |__| | (_| | ||  __/\ V  V / (_| | |_| |
     |_|  |_|\__,_|\__,_|_____/       \_____|\__,_|\__\___| \_/\_/ \__,_|\__, |
                                                                          __/ |
                                                                         |___/
    2017-10-06 09:55:06.494  INFO [] 3598 --- [                main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8888
    2017-10-06 09:55:06.579  WARN [] 3598 --- [                main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/maas-gateway/default": Connection refused; nested exception is java.net.ConnectException: Connection refused
    2017-10-06 09:55:06.580  INFO [] 3598 --- [                main] com.solace.maas.Application              : No active profile set, falling back to default profiles: default
    2017-10-06 09:55:06.601  INFO [] 3598 --- [                main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2ed94a8b: startup date [Fri Oct 06 09:55:06 EDT 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@53e25b76
    2017-10-06 09:55:08.889  INFO [] 3598 --- [                main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'getManagerExecutorService' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=gatewayServiceConfiguration; factoryMethodName=getManagerExecutorService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/solace/maas/gateway/configuration/GatewayServiceConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=iamServiceConfiguration; factoryMethodName=getManagerExecutorService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/solace/maas/iam/configuration/IamServiceConfiguration.class]]
    2017-10-06 09:55:08.890  INFO [] 3598 --- [                main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'getMaasProperties' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=gatewayServiceConfiguration; factoryMethodName=getMaasProperties; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/solace/maas/gateway/configuration/GatewayServiceConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=iamServiceConfiguration; factoryMethodName=getMaasProperties; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/solace/maas/iam/configuration/IamServiceConfiguration.class]]
    2017-10-06 09:55:08.898  INFO [] 3598 --- [                main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'getManagerExecutorService' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=iamServiceConfiguration; factoryMethodName=getManagerExecutorService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/solace/maas/iam/configuration/IamServiceConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=signupManagerConfiguration; factoryMethodName=getManagerExecutorService; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/solace/maas/signupManager/configuration/SignupManagerConfiguration.class]]
    2017-10-06 09:55:09.126  INFO [] 3598 --- [                main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'managementServletContext' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration; factoryMethodName=managementServletContext; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcHypermediaManagementContextConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration; factoryMethodName=managementServletContext; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/actuate/autoconfigure/EndpointWebMvcAutoConfiguration.class]]
    2017-10-06 09:55:09.750  WARN [] 3598 --- [                main] o.s.c.a.ConfigurationClassPostProcessor  : Cannot enhance @Configuration bean definition 'refreshScope' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.
    2017-10-06 09:55:09.992  INFO [] 3598 --- [                main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=27bcc9d7-c3a9-3801-b696-3f3650ee7946
    2017-10-06 09:55:10.038  INFO [] 3598 --- [                main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    2017-10-06 09:55:10.137  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.sleuth.instrument.async.AsyncDefaultAutoConfiguration$DefaultAsyncConfigurerSupport' of type [class org.springframework.cloud.sleuth.instrument.async.AsyncDefaultAutoConfiguration$DefaultAsyncConfigurerSupport$$EnhancerBySpringCGLIB$$dcf17295] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.237  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$9fafd4d2] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.467  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [class org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$385d9d0c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.486  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'objectPostProcessor' of type [class org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.494  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@736d6a5c' of type [class org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.506  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [class org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$5d323fbe] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.571  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [class org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.606  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration' of type [class org.springframework.cloud.netflix.metrics.MetricsInterceptorConfiguration$MetricsRestTemplateConfiguration$$EnhancerBySpringCGLIB$$d1dc7b13] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:10.659  INFO [] 3598 --- [                main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [class org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$bbc9d7cf] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2017-10-06 09:55:11.342  INFO [] 3598 --- [                main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2017-10-06 09:55:11.353  INFO [] 3598 --- [                main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2017-10-06 09:55:11.354  INFO [] 3598 --- [                main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
    2017-10-06 09:55:11.429  INFO [] 3598 --- [ocalhost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2017-10-06 09:55:11.429  INFO [] 3598 --- [ocalhost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4828 ms
    2017-10-06 09:55:12.903 ERROR [] 3598 --- [ocalhost-startStop-1] o.s.b.c.embedded.tomcat.TomcatStarter    : Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'iamServiceConfiguration': Unsatisfied dependency expressed through method 'setContentNegotationStrategy' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter': Unsatisfied dependency expressed through constructor parameter 3; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration$$EnhancerBySpringCGLIB$$3067f332]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jacksonHttpMessageConverter' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter]: Factory method 'jacksonHttpMessageConverter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'config' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositories' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userAttributeDAO': Cannot create inner bean '(inner bean)#60b53b0c' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#60b53b0c': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
    2017-10-06 09:55:12.932  WARN [] 3598 --- [                main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
    2017-10-06 09:55:12.940 ERROR [] 3598 --- [                main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

说明

无法确定数据库类型 NONE 的嵌入式数据库驱动程序类

动作

如果您想要一个嵌入式数据库,请在类路径中放置一个受支持的数据库。如果您要从特定配置文件加载数据库设置,您可能需要激活它(当前没有配置文件处于活动状态)。

【问题讨论】:

你能显示构建文件吗? 你有 spring-retry 作为依赖吗? 你用的是什么版本? @spencergibb 感谢您的提示。我缺少 spring-retry 作为依赖项。放完之后,我得到了想要的行为。 添加为答案 【参考方案1】:

spring-retry 添加为依赖项。

【讨论】:

以上是关于停止 spring boot 应用程序启动,直到 spring cloud config server 启动的主要内容,如果未能解决你的问题,请参考以下文章

Gradle > 如何停止使用 gradle bootRun 启动的 Spring Boot 应用程序?

Spring boot centos部署启动停止脚本

停止许多特定的 Spring Boot 应用程序

spring boot 启动错误

Spring Boot 项目脚本(启动停止重启状态)

Spring Boot使用@Schedule启动、停止服务