Search code examples
javaspringspring-bootspring-cloudspring-cloud-config

RefreshEndpoint Bean not available with Spring Boot 2.4.2 and Spring Cloud 2020.0.1


I have an application that we are migrating to the latest Spring Boot(2.4.2) and Spring Cloud(2020.0.1) versions. The application uses a Cloud Config Server to fetch the configurations and Refresh is done by a scheduled job which makes a call RefreshEndpoint.refresh().

This used to work perfectly fine, but with the version above, I cannot make it work.

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

Description:

Parameter 0 of constructor in io.divinedragon.spring.boot2.ConfigRefreshJob required a bean of type
'org.springframework.cloud.endpoint.RefreshEndpoint' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.cloud.endpoint.RefreshEndpoint' in your configuration.

Here is the project with below configuration.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>io.divinedragon.spring-boot</groupId>
    <artifactId>spring-boot-2.4-and-spring-cloud-2020.01</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.1</spring-cloud.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Testing Dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

spring:
  application:
    name: sprint-boot-2.4-and-spring-cloud-2020.0.1

management:
  endpoint:
    health:
      show-details: always
  server:
    port: 7979
  endpoints:
    web:
      base-path: /
      exposure:
        include:
          - health

server:
  port: 8080

Application.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class Application {

    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

ConfigRefreshJob.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.endpoint.RefreshEndpoint;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ConfigRefreshJob {

    private static final Logger LOG = LoggerFactory.getLogger(ConfigRefreshJob.class);

    private static final int TEN_MINUTE = 10 * 60 * 1000;

    private final RefreshEndpoint refreshEndpoint;

    @Autowired
    public ConfigRefreshJob(final RefreshEndpoint refreshEndpoint) {
        this.refreshEndpoint = refreshEndpoint;
    }

    @Scheduled(fixedDelay = TEN_MINUTE)
    public void refreshConfigs() {
        LOG.info("Refreshing Configurations - {}", refreshEndpoint.refresh());
    }
}

Can somebody suggest how to fix this?


Solution

  • Change your application.yml as follow :

    management:
      endpoints:
        web:
          exposure:
            include:
              - refresh
              - health
    

    Refresh Scope