我在WebFlux中有一些应用程序,我想使用BlockHound,但我需要通过application.properties中的参数化或春季分析或其他一些方法来打开和关闭它。另外,当锁定操作被捕获时,我希望重写操作,这样就不会抛出错误,而会引发日志警告。首先,我在application.properties中使用了参数:
@SpringBootApplication
@Slf4j
public class GazPayApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context =
SpringApplication.run(GazPayApplication.class, args);
BlockHoundSwitch blockHoundSwitch = (BlockHoundSwitch)context.getBean("BlockHoundSwitchBean");
if (blockHoundSwitch.isBlockHoundEnabled()) {
BlockHound.install(builder ->
builder.blockingMethodCallback(it ->
log.warn("find block operation: {}", it.toString())));
}
}我的BlockHoundSwitch:
@Component("BlockHoundSwitchBean")
@Getter
public class BlockHoundSwitch {
@Value("${blockhound.enabled}")
private boolean blockHoundEnabled;
}它对我有效,但在我看来,这个解决方案相当困难,有点不可预测。接下来,我尝试通过分析来解决此任务:
@Profile("blockhound_enabled")
@Slf4j
@Component()
public class BlockHoundSwitch {
public BlockHoundSwitch() {
BlockHound.install(builder ->
builder.blockingMethodCallback(it ->
log.warn("find block operation: {}", it.toString())));
}
}而且效果也很好。嗯,我有几个问题:
发布于 2022-03-23 08:33:10
我来解决它。也许有人能帮上忙。我是通过分析和我的代码实现的:
@Profile("blockhound_on")
@Slf4j
@Component()
@Getter
public class BlockHoundSwitch {
public BlockHoundSwitch() {
BlockHound.install(builder ->
builder.blockingMethodCallback(it -> {
List<StackTraceElement> itemList = Arrays.stream(new Exception(it.toString()).getStackTrace())
.filter(i -> i.toString().contains("application.package"))
.collect(Collectors.toList());
log.warn("find block operation: \n{}", itemList);
}));
}
}where application.package -我的项目的主要包,我在堆栈中找到它。
https://stackoverflow.com/questions/71464826
复制相似问题