@RefreshScope 和 /refresh 不工作

问题描述 投票:0回答:5

我尝试使用配置服务器实现 spring 外部配置。它在应用程序启动时第一次运行良好,但未反映对属性文件的任何更改。我尝试使用 /refresh 端点来动态刷新我的属性,但它似乎不起作用。对此的任何帮助都会非常有帮助。

我尝试发布到 localhost:8080/refresh 但收到 404 错误响应。

下面是我应用类的代码

   @SpringBootApplication
public class Config1Application {

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

@RestController
@RefreshScope
class MessageRestController {

    @Value("${message:Hello default}")
    private String message;

    @RequestMapping("/message")
    String getMessage() {
        return this.message;
    }
}

POM文件是

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</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>Finchley.M8</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

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

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

和 bootstrap.properties

spring.application.name=xxx
spring.cloud.config.uri=https://xxxxxx.com
management.security.enabled=false
endpoints.actuator.enabled=true
spring-cloud spring-cloud-config spring-config
5个回答
31
投票

Spring 2 及更高版本的端点现在是

/actuator/refresh

来自评论:

  • 您确实需要在
    management.endpoints.web.exposure.include=refresh
    中设置
    bootstrap.properties/bootstrap.yml

注意:如果您是

Spring-Cloud
的新手,并且不太确定所有关键字都可以进入
web.exposure
,您可以将其设置为
*
management.endpoints.web.exposure.include=*
)以显示所有内容,您可以访问稍后了解端点及其限制。


4
投票

对于YAML文件,属性的值需要用双引号括起来:

# Spring Boot Actuator
management:
  endpoints:
    web:
      exposure:
        include: "*"

注意:确保您为此属性使用正确的endpoints关键字(带有's'),只要它存在于另一个没有's'的属性:“management.endpoint.health ....”。


4
投票

添加属性后对我有用

management.endpoints.web.exposure.include=*

在 bootstrap.properties 中,将 url 更改为

/actuator/refresh
for spring version above 2.0.0 对于 spring 版本 1.0.5 url 是
/refresh
.


0
投票

注意:- 这是一个 POST 请求(不是 GET)

我在这里发布了解决方案

[https://stackoverflow.com/a/74465108/2171938][1]


-1
投票

如果您在接受 formurlencoded in SPRING 2.0> 时遇到问题,请使用:

curl -H "Content-Type: application/json" -d {} http://localhost:port/actuator/refresh

代替:

curl -d {} http://localhost:port/refresh

在春季 1 中被接受。*

© www.soinside.com 2019 - 2024. All rights reserved.