RefreshEndpoint Bean 不适用于 Spring Boot 2.4.2 和 Spring Cloud 2020.0.1

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

我有一个应用程序正在迁移到最新的 Spring Boot(2.4.2) 和 Spring Cloud(2020.0.1) 版本。该应用程序使用 Cloud Config Server 来获取配置,刷新由调用 RefreshEndpoint.refresh().

的计划作业完成

这曾经工作得很好,但是对于上面的版本,我不能让它工作。

***************************
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.

这是具有以下配置的项目。

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());
    }
}

有人可以建议如何解决这个问题吗?

按如下方式更改您的 application.yml :

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

Refresh Scope

此外,您可能需要在 application.yml:

中启用 JMX
management:
  endpoints:
    web:
      exposure:
        include:
          - refresh
spring:
  jmx:
    enabled: true

查看此对话:https://github.com/spring-cloud/spring-cloud-commons/issues/710#issuecomment-598769493

我遇到了同样的问题,启用 JMX 有帮助。