2

无法使用“ http://localhost:9001/refresh ”刷新配置文件。如果我重新启动客户端应用程序,更新的配置加载正常。以下是我用来测试的简单休息控制器。刷新是使用 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>

4

3 回答 3

6

应在 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 - 将在启动时加载属性,并且不会通过刷新调用重新加载。- 要重新加载使用 @Value 注释的属性,您需要,

  • 重新启动应用程序。
  • 也可以通过使用@Value 的@RefreshScope(spring cloud config annotation)注释类而无需重新启动。
于 2019-10-07T11:04:36.527 回答
0

我在另一个堆栈溢出问题中找到了答案。

就我而言,我必须设置属性

management.endpoints.web.exposure.include=*
# management.endpoints.web.exposure.include=xyz

启用“/actuator/refresh”url(注意执行器部分!),并添加一个类

package here.org.your.put;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 *
 */
@Component
@ConfigurationProperties(prefix = "encrypted")
public class PropertyConfiguration {

  private String property;

  public String getProperty() {
    return property;
  }

  public void setProperty(String property) {
    this.property = property;
  }
}

它有一个由“刷新”调用的方法 setProperty。它从 git 存储库中获取我的加密属性(我的客户端的 application.properties 中的 encrypted.password),并使用我也在运行的 spring 配置服务器进行解密。然后它在这个类中设置值。

于 2018-11-12T02:56:45.773 回答
-1

刷新端点包含在执行器中。请添加执行器依赖项并使用“ http://localhost:9001/actuator/refresh ”端点尝试此操作。

于 2018-07-31T09:18:14.243 回答