apolloconfig / apollo

Apollo is a reliable configuration management system suitable for microservice configuration management scenarios.
https://www.apolloconfig.com
Apache License 2.0
28.94k stars 10.19k forks source link

Apollo对SpringBootTest模块的@DynamicPropertySource支持 #5081

Closed jinyang1101 closed 4 months ago

jinyang1101 commented 5 months ago

你的特性请求和某个问题有关吗?请描述 目前大部分服务都是使用springboot 开发的应用。很多时候使用的是SpringBootTest 模块进行测试。这个时候通常是希望使用不同的测试配置。 以下基于mysql 场景进行说明。

  1. 使用 testcontainers 进行环境隔离。
  2. 创建个一个container,替代apollo中配置的jdbcUrl
  3. 基于springboot test @DynamicPropertySource注入jdbcUrl达到替换的目的

但是由于apollo总是将自己的PropertySource 放在第一个,导致@DynamicPropertySource失效。

image

清晰简洁地描述一下你希望的解决方案 提供机制,在测试场景下可以调整apollo获取的properties顺序 例如增加apollo.test.enabled? 或者是提供apollo-test.jar 仅作用于scope=test

image
dosubot[bot] commented 5 months ago

🤖

你好,@jinyang1101!很高兴再次看到你的名字出现在这里。希望你一切都好!

感谢您的反馈。我理解您在使用SpringBootTest进行测试时,希望能够使用不同的测试配置,并且您希望Apollo能够支持@DynamicPropertySource。

目前,Apollo的PropertySource默认是放在第一个的,这可能会导致@DynamicPropertySource失效。但是,您可以通过设置apollo.override-system-propertiesfalse来改变这个默认行为。这可以通过以下几种方式实现:

  1. 通过Java系统属性apollo.override-system-properties
    • 可以在Java程序启动脚本中指定-Dapollo.override-system-properties=false
    • 也可以通过编程方式指定,例如System.setProperty("apollo.override-system-properties", "false");
  2. 通过Spring Boot配置文件
    • 可以在Spring Boot的application.propertiesbootstrap.properties中指定apollo.override-system-properties=false
  3. 通过app.properties配置文件
    • 可以在classpath:/META-INF/app.properties中指定apollo.override-system-properties=false

关于这个问题,您可以参考Apollo文档的"1.2.4.8 Enable Apollo Override System Properties"部分。

另外,Apollo确实提供了一种机制来使用不同的测试配置。在提供的上下文中,使用了@SpringBootTest注解来指定测试时的不同配置。这个注解的classes属性被设置为BizTestConfiguration.class,这意味着在测试期间,BizTestConfiguration类将被用作Spring应用上下文的配置。

这是相关的代码:

@SpringBootTest(
    classes = BizTestConfiguration.class,
    webEnvironment = WebEnvironment.RANDOM_PORT
)

这意味着您可以在BizTestConfiguration类中定义您的测试特定配置,这些配置将在运行测试时被使用。然而,上下文并没有提供关于apollo.test.enabledapollo-test.jar的信息。

关于这个问题,您可以参考Apollo源码

希望这些信息对您有所帮助。如果您有任何其他问题或需要进一步的澄清,请随时告诉我。

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

jinyang1101 commented 5 months ago

@dosu-bot 粗略地使用了下,确实可以使用。

dosubot[bot] commented 5 months ago

🤖

很高兴听到您对 Apollo 的积极反馈!如果您有任何关于配置管理或者 Apollo 的问题,都可以随时问我哦。


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

stale[bot] commented 4 months ago

This issue has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days unless it is tagged "help wanted" or other activity occurs. Thank you for your contributions.

stale[bot] commented 4 months ago

This issue has been automatically closed because it has not had activity in the last 7 days. If this issue is still valid, please ping a maintainer and ask them to label it as "help wanted". Thank you for your contributions.