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

div*_*gon 5 java spring spring-boot spring-cloud spring-cloud-config

我有一个应用程序,我们正在迁移到最新的 Spring Boot(2.4.2) 和 Spring Cloud(2020.0.1) 版本。该应用程序使用云配置服务器来获取配置,并且刷新是通过调用的预定作业来完成的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.
Run Code Online (Sandbox Code Playgroud)

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

pom.xml

***************************
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.
Run Code Online (Sandbox Code Playgroud)

application.yml

<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>
Run Code Online (Sandbox Code Playgroud)

Application.java

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
Run Code Online (Sandbox Code Playgroud)

ConfigRefreshJob.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

小智 6

更改您的application.yml如下:

management:
  endpoints:
    web:
      exposure:
        include:
          - refresh
          - health
Run Code Online (Sandbox Code Playgroud)

刷新范围