簡體   English   中英

在 spring boot 應用程序中以編程方式調用 spring cloud config actuator/env post 端點

[英]Programmatically call spring cloud config actuator/env post endpoint in spring boot application

我在我的spring boot應用程序中使用spring cloud config,我試圖通過actuator/env post端點更新屬性值。

這是我的代碼:

@Service
public class ActuatorRefreshService {

  private final String inventoryModeKey = "inventory.initial.mode";
  private WebClient webClient = WebClient.builder().build();

  @Autowired
  public ActuatorRefreshService() {
  }

  public void refreshStatus(String latestMode) {
    Map<String, String> bodyMap = new HashMap();
    bodyMap.put("name",inventoryModeKey);
    bodyMap.put("value",latestMode);
    webClient.post().uri("/actuator/env")
            .header(HttpHeaders.CONTENT_TYPE, String.valueOf(MediaType.APPLICATION_JSON))
            .body(Mono.just(bodyMap), Map.class).retrieve().onStatus(HttpStatus::isError, clientResponse -> {
        return Mono.error(new Exception("error"));
    }).bodyToMono(String.class);
    System.out.println("call actuator endpoint to update the value");

  }
}

當我調用調用此refreshStatus方法的休息端點時。 該 api 返回我 200 狀態。 之后我點擊localhost:8080/actuator/refresh 當我檢查更新的值時,它顯示了這個__refreshAll__

我不知道為什么會這樣?? 任何幫助,將不勝感激。

注意: *當我到達終點時 來自郵遞員的 localhost:8080/actuator/env 並刷新,然后它會更新屬性。*

我也嘗試使用localhost:8080/actuator/busenv總線端點,但仍然沒有運氣。 有人試過這種要求嗎?

我能夠在 spring 中使用RestTemplate in spring spring 啟動中以編程方式調用這個 api localhost:8080/actuator/busenv 它也在應用程序上下文中刷新配置。 我不確定為什么它不能與WebClient一起使用。

如果其他人正在尋找相同的要求,請發布答案。

請找到以下代碼

@Service
public class ActuatorRefreshService {

   private final String inventoryModeKey = "inventory.initial.mode";

   @Autowired
   public ActuatorRefreshService() {
   }

   public void refreshInventoryMode(String latestMode) {
      Map<String, String> bodyMap = new HashMap();
      bodyMap.put("name",inventoryModeKey);
      bodyMap.put("value",latestMode);
      final String url = "http://localhost:8080/actuator/busenv";

      RestTemplate restTemplate = new RestTemplate();
      HttpHeaders headers = new HttpHeaders();
    


      headers
      .setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));

      HttpEntity<Object> entity = new HttpEntity<>(bodyMap, headers);
      restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
      System.out.println("call actuator endpoint to update the value ");

}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM