'Programmatically call spring cloud config actuator/env post endpoint in spring boot application
I am using spring cloud config in my spring boot application, I am trying to update the property value through actuator/env post endpoint.
Here is my code:
@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");
}
}
When I am calling my rest endpoint that calls this refreshStatus
method. The api returns me 200 status. After that I hit localhost:8080/actuator/refresh
. When I check the updated value , it shows this __refreshAll__
.
I have no idea why is this happening?? Any help would be appreciated.
Note :* When I hit the endpoint
localhost:8080/actuator/env from postman and refresh then it updates the property.*
I tried with localhost:8080/actuator/busenv
bus endpoint as well but still no luck.
Anyone tried this kind of requirement?
Solution 1:[1]
I was able to call this api localhost:8080/actuator/busenv
programmatically in spring boot using RestTemplate in spring
. It is refreshing the configuration as well in application context. I am not sure why it didn't work with WebClient
though.
posting the answer if anyone else is looking for the same requirement.
Please find the below code
@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 ");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | APK |