Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SpringCloud项目中注解方式使用客户端导致Spring启动失败 #990

Open
slankka opened this issue Mar 8, 2018 · 11 comments
Open
Labels
feature request Categorizes issue as related to a new feature.

Comments

@slankka
Copy link

slankka commented Mar 8, 2018

大神们好,我在SpringCloud(Spring core 4.2.6)中 使用Apollo Client,选用
使用Spring Annotation支持
这个方式启动失败,报错如下,还有一些Value注解的配置注入失败的信息没有贴出来。

[WARN] [17:26:00.336][DirectJDKLog][180]:The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Object.wait(Native Method)
 java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
 com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
[ERROR] [17:26:00.342][SpringApplication][827]:Application startup failed

如果我用

@Component //加上注解
public class TestApolloAnnotationBean {
  @ApolloConfig
  private Config config; //inject config for namespace application
  @ApolloConfig("application")
  private Config anotherConfig; //inject config for namespace application
  @ApolloConfig("FX.apollo")
  private Config yetAnotherConfig; //inject config for namespace FX.apollo
 
  @Value("${batch:100}")
  private int batch;

删除

@Configuration
@EnableApolloConfig
public class AppConfig {
  @Bean
  public TestApolloAnnotationBean testApolloAnnotationBean() {
    return new TestApolloAnnotationBean();
  }
}

并且application配置文件指定
apollo.bootstrap.enabled: true则可以启动成功。

问题:

  1. 上文WIKI给出的注解配置是不是不支持SpringCloud?
  2. apollo.bootstrap.enabled 是否必须是true,不然启动完后,private Config config 这个值为null?
@nobodyiam
Copy link
Member

@EnableApolloConfig的方式是支持绝大部分场景的,除非你的AppConfig没有被扫描到?

apollo.bootstrap.enabled的场景是针对Spring Boot的启动阶段的一些配置,比如@ConditionalOnProperty

@slankka
Copy link
Author

slankka commented Mar 9, 2018

我把报错的代码弄出来了,是最精简的环境,希望有人能够帮助我看一下是什么原因导致
https://github.com/slankka/Apollo-SpringCloudExample

@slankka
Copy link
Author

slankka commented Mar 10, 2018

已经解决。原因是项目在XML中配置了自定义的property-placeholder,会创建的PropertySourcesPlaceholderConfigurer,但Apollo Client在此之前抢先创建了这个Bean,但没能读取到这里的配置,导致给Bean填充Value的时候,失败了。详细内容请访问上面的链接。

@slankka slankka changed the title SpringCloud项目中使用注解方式启动客户端失败 SpringCloud项目中注解方式使用客户端导致Spring启动失败 Mar 11, 2018
@nobodyiam
Copy link
Member

@slankka 非常感谢详细的分析。

由于Apollo的Spring使用方式依赖于PropertySourcesPlaceholderConfigurer,所以我们需要确保至少存在一个PropertySourcesPlaceholderConfigurer,这也是我们默认添加一个的原因。

我们后续看下是否可以把默认添加的这个PropertySourcesPlaceholderConfigurer的优先级调到最低,从而避免和用户的配置产生冲突。

@nobodyiam nobodyiam self-assigned this Mar 11, 2018
@nobodyiam nobodyiam added the bug Categorizes issue or PR as related to a bug. label Mar 11, 2018
@slankka
Copy link
Author

slankka commented Mar 12, 2018

Apollo 创建的PropertySourcesPlaceholderConfigurer中 ignoreUnresolvablePlaceholders 默认是true,导致只要没有配置值的,项目也不能启动成功。

@nobodyiam
Copy link
Member

Apollo 创建的PropertySourcesPlaceholderConfigurer没有设置ignoreUnresolvablePlaceholders这个属性,看了下Spring代码里面默认值是false呀,org.springframework.beans.factory.config.PlaceholderConfigurerSupport#ignoreUnresolvablePlaceholders

@slankka
Copy link
Author

slankka commented Mar 12, 2018

不好意思,这个不是Apollo的问题。。另外优先级也不是Apollo的问题,只是需要说明一下,优先级默认刚好是int 最大值,都是最低的,需要用户自己提升优先级。

@nobodyiam
Copy link
Member

恩,刚看了一下,确实order已经是最低了 - Ordered.LOWEST_PRECEDENCE

@nobodyiam nobodyiam added enhancement and removed bug Categorizes issue or PR as related to a bug. labels Mar 12, 2018
@nobodyiam nobodyiam removed their assignment Mar 12, 2018
@nobodyiam nobodyiam added feature request Categorizes issue as related to a new feature. and removed enhancement labels Nov 30, 2019
@liu-hong
Copy link

我现在用的1.2.1版本,我也遇到了这个问题,apollo在后面迭代中硬编码方式把PropertySourcesPlaceholderConfigurer优先级设为了0而不是注解中的最低,导致自定义的PropertySourcesPlaceholderConfigurer在后面加载,所以填充value报错。目前解决方法是把自定义的order设为-1

@xiehanghang
Copy link

现在解决了吗,我好想也遇到相同的问题

@slankka
Copy link
Author

slankka commented Jul 30, 2020

@xiehanghang 项目中,最多配置一个 PropertySourcesPlaceholderConfigurer。因此要么把自定义配置的文件按照Spring Boot的规范放置, 参见SpringBoot官方文档,文件名和路径都要按照规范; 要么自己加载Yaml配置文件, 另外,如果你一定要自己亲手加载配置文件,这里给出我当时的临时解决方案仅供参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Categorizes issue as related to a new feature.
Projects
None yet
Development

No branches or pull requests

4 participants