Как обновить ответ Spring Actuator во время выполнения?JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Как обновить ответ Spring Actuator во время выполнения?

Сообщение Anonymous »

Можно ли получить обновленный ответ от конечной точки Spring Actuator (например, https://localhost:8080/app/actuator/env) во время работы приложения?
Для Например, в хранилище параметров AWS параметр «app.actuator.excludedEndpoints» пуст, поэтому конечная точка app/actuator/env должна вернуть правильный ответ (и это делает).
Но когда я тем временем меняю значение «app.actuator.excludedEndpoints» на env и использую «/refresh-actuator-config», я на самом деле в отладчике вы увидите, что он обновляет компонент «webEndpointProperties», который обновляется, а «excludedEndpoints» имеет новое значение, но в ответе на «/app/actuator/env» я все еще получаю предыдущий ответ (я должна получить ошибку!).

Код: Выделить всё

@Configuration
@RefreshScope
public class ManagementConfig {

@Value("${app.actuator.excludedEndpoints}")
private String excludedEndpoints;

@Autowired
private ConfigurableApplicationContext applicationContext;

@Bean
@Primary
@RefreshScope
public WebEndpointProperties webEndpointProperties() {
return createWebEndpointProperties();
}

private WebEndpointProperties createWebEndpointProperties() {
WebEndpointProperties properties = new WebEndpointProperties();
properties.setBasePath("/app/actuator");

Set excludedEndpointsSet = Set.of(excludedEndpoints.split(","));

properties.getExposure().setExclude(excludedEndpointsSet);
return properties;
}

@Bean
public WebFluxEndpointHandlerMapping webFluxEndpointHandlerMapping(
WebEndpointDiscoverer endpointDiscoverer) {

EndpointMapping endpointMapping = new EndpointMapping("/app/actuator");
Collection endpoints = endpointDiscoverer.getEndpoints();
EndpointMediaTypes mediaTypes = EndpointMediaTypes.DEFAULT;
EndpointLinksResolver linksResolver = new EndpointLinksResolver(endpoints);

return new WebFluxEndpointHandlerMapping(
endpointMapping,
endpoints,
mediaTypes,
null,
linksResolver,
true
);
}

public void refreshWebEndpointProperties() {
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext.getBeanFactory();

// Force environment refresh
ConfigurableEnvironment environment = applicationContext.getEnvironment();
environment.getPropertySources().iterator();

// Refresh web properties
beanFactory.destroySingleton("webEndpointProperties");
beanFactory.registerSingleton("webEndpointProperties", createWebEndpointProperties());

// Refresh handler mapping
beanFactory.destroySingleton("webFluxEndpointHandlerMapping");
applicationContext.getBean("webFluxEndpointHandlerMapping");
}
}
Обновить контроллер:

Код: Выделить всё

@RestController
public class ConfigRefreshController {

@Qualifier("configDataContextRefresher")
@Autowired
private ContextRefresher contextRefresher;

@Autowired
private ManagementConfig managementConfig;

@Autowired
private ConfigurableEnvironment environment;

@PostMapping("/refresh-actuator-config")
public String refreshConfig() {
contextRefresher.refresh();
managementConfig.refreshWebEndpointProperties();
return "Configuration refreshed";
}
}
Кроме того, не знаю, актуально ли это, но я использую реактивный Netty.

Подробнее здесь: https://stackoverflow.com/questions/792 ... t-runetime
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»