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