spring-cloud / spring-cloud-config

External configuration (server and client) for Spring Cloud
Apache License 2.0
1.96k stars 1.29k forks source link

Discovery setting can't set in a different profile when use Discovery First Lookup. #2001

Open tanwan opened 2 years ago

tanwan commented 2 years ago

Describe the bug When I set discovery setting in specific profile(e.g. application-test.yml), it will raise an exception.

java.lang.NullPointerException: null
    at org.springframework.cloud.config.client.ConfigServerInstanceProvider.getConfigServerInstances(ConfigServerInstanceProvider.java:57) ~[spring-cloud-config-client-3.1.0-RC1.jar:3.1.0-RC1]
    at org.springframework.cloud.config.client.ConfigServerInstanceMonitor.refresh(ConfigServerInstanceMonitor.java:90) ~[spring-cloud-config-client-3.1.0-RC1.jar:3.1.0-RC1]

The bootstrapContext register Binder with activationContext after contributors process imports. So in ConfigServerConfigDataLocationResolver#resolveProfileSpecific method, the bootstrapContext has no activationContext, and the resolverContext has activationContext. Therefore, resolverContext resolved spring.cloud.config.discovery.enabled is true, However bootstrapContext resolved result is false. It cause the ConfigServerInstanceProvider.Function register in EurekaConfigServerBootstrapper is null.

Sample The configuration in application-test.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
spring:
  config:
    import: 'optional:configserver:'
  cloud:
    config:
      enabled: true
      discovery:
        enabled: true
        serviceId: configserver
ryanjbaxter commented 2 years ago

Does this happen in 2020.0.4 as well?

tanwan commented 2 years ago

Yes, I tried 2020.0.4 and 2021.0.0-RC1, they all happened.

pmahony893 commented 2 years ago

There is a similar issue in 2021.0.1, Boot 2.6.6; setting spring.cloud.config.discovery.enabled=true by default, and spring.cloud.config.discovery.enabled=false for a profile specific document in the same file produces an InactiveConfigDataAccessException if the profile isn't active.