awspring / spring-cloud-aws

The New Home for Spring Cloud AWS
http://awspring.io
Apache License 2.0
851 stars 290 forks source link

Refreshscope annotated beans are not updated #1112

Open tinah-1973 opened 5 months ago

tinah-1973 commented 5 months ago

Type: Bug

Component: spring-cloud-aws-parameter-store Version 3.1.0

Describe the bug I am currently trying to migrate the Spring Cloud AWS dependencies from 2.4.4 to 3.1.0. The Spring Boot Version we are using is 3.2.2

We use the AWS Parameter Store to obtain the application parameters. Here is the configuration for that:

spring.config.import=aws-parameterstore:/EAC/ESB/APP/

spring.cloud.aws.parameterstore.region=eu-central-1

spring.cloud.aws.parameterstore.reload.strategy=refresh

spring.cloud.aws.parameterstore.reload.period=15s

So far everything fits, except the refresh of properties in @Refreshscope annotated beans isn't working.

In the debug output I see that the task runs every 15 seconds.

08:35:58.399 [spring-cloud-aws-parameterstore-ThreadPoolTaskScheduler-1] 
DEBUG io.awspring.cloud.parameterstore.ParameterStorePropertySource - Populating property retrieved from AWS Parameter Store: testCount

I set a breakpoint in ParameterStorePropertySource Line 45 to see if the parameter "testCount" contains the changed value from the parameter store. This is the case. But unfortunately the bean, which is using 'testCount' isn't updated.

@Configuration
@RefreshScope
public class TestConfiguration {

    @Value("${testCount}")
    private Integer testCount;

    public Integer getTestCount() {
        return this.testCount;
    }
}

@Component
@EnableScheduling
public class TestBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestBean.class);
    @Autowired
    private TestConfiguration config;

    @Scheduled(fixedDelay = 5000)
    public void message() {
        System.out.println("The message is: " + config.getTestCount());
    }
}

Any ideas why this could be?

Do I still need the spring actuator endpoint "refresh"?

management.endpoints.web.exposure.include=refresh

I tried using it, but it didn't do anything.

maciejwalkowiak commented 5 months ago

@tinah-1973 I made a sample app with Parameter Store and refresh working: https://github.com/maciej-scratches/spring-cloud-aws-gh-1003

Perhaps there's something else wrong. I am happy to take a look but please create such a repo like the one i did so that i can easily run it and see what's not working

tinah-1973 commented 2 months ago

@maciejwalkowiak Thanks for your sample! It worked with my parameter store. Then I noticed, that I still had included following dependency in our projects pom file:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

I removed it, and then everything worked fine!