RefreshEndpointBean不适用于SpringBoot2.4.2和SpringCloud2020.0.1

kyxcudwk  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(413)

我有一个应用程序,我们正在迁移到最新的springboot(2.4.2)和springcloud(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.

下面是配置如下的项目。

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>

应用程序.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

应用程序.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());
    }
}

有人能建议怎么解决这个问题吗?

qgelzfjb

qgelzfjb1#

将application.yml更改如下:

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

刷新作用域

相关问题