spring-boot - Spring Boot 配置客户端 : Refresh not working

标签 spring-boot configuration centralized

无法使用“http://localhost:9001/refresh”刷新配置文件'。 如果我重新启动客户端应用程序,更新的配置加载正常。 以下是我用来测试的简单休息 Controller 。 使用 curl 命令“curl -d {} localhost:9001/refresh/”运行刷新,会出现 404 错误。

@RestController
@RefreshScope
class ExampleController {

    @Value("${Message2}")
    private String message2 = "Hello World";

    @RequestMapping
    public String sayValue() {
        return message2;
    }
}

以下是我正在使用的 pom.xml

<groupId></groupId>
<artifactId>MyConfigurationClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>MyConfigurationServer</name>
<description>Demo project for Spring Boot</description>

<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-config-server</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>

最佳答案

需要在 pom.xml 中添加执行器依赖项才能使用。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

通过在 application.properties 或 bootstrap.properties 中添加以下条目来包含刷新端点。

management.endpoints.web.exposure.include=refresh

调用刷新端点以重新加载属性而不重新启动应用程序。 http://localhost:8080/actuator/refresh (使用http post方式获取不到)

@ConfigurationProperties - 将使用执行器刷新调用本身重新加载相应的属性。

@值 - 将在启动时加载属性,并且不会通过刷新调用重新加载。 - 要重新加载带有@Value 注释的属性,您需要,

  • 重启应用。
  • 也可以通过使用@RefreshScope(spring cloud config annotation)注释类来完成,而无需重新启动 @值。

关于spring-boot - Spring Boot 配置客户端 : Refresh not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49364866/

相关文章:

java - 如何使用 Spring Boot 创建列表类型 View 以从数据库中获取所有记录?

java - Jhipster UAA 外部客户端

html - 如何以绝对位置集中图像?

html - 如何对齐 3 个圆形图像?

css - 集中 div 和缩放以适合屏幕

java - Spring Data Rest存储库的安全方法

多个实例上的 Spring 和计划任务

node.js - 通过 TOR 使用 Node 的 HTTP 请求问题

java - 调试 Spring 配置

java - 以编程方式初始化 log4j2 的更好方法(二)