繁体   English   中英

Spring 引导配置客户端:刷新不工作

[英]Spring boot Config client : Refresh not working

无法使用“ http://localhost:9001/refresh ”刷新配置文件。 如果我重新启动客户端应用程序,更新的配置加载正常。 以下是我用来测试的简单 rest 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-将在启动时加载属性,并且不会通过刷新调用重新加载。 -要重新加载用@Value注释的属性,

  • 重新启动应用程序。
  • 也可以仅通过使用@Value的@RefreshScope(弹簧云配置注释)对类进行注释而无需重新启动。

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

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

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

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 config服务器进行解密。 然后,它在此类中设置值。

只需检查一下,您的 config-client 已设置属性

server.port: 8080
management.endpoints.web.exposure.include=*

然后运行 POST 刷新请求(不是 GET)

curl --location --request POST 'http://localhost:8080/actuator/refresh'

祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM