Blockhound 没有检测到直接的阻塞代码

问题描述

使用 spring boot webflux,我正在尝试使用 Blockhound 进行一个非常简单的阻塞调用,但它似乎没有检测到它。

        <dependency>
            <groupId>io.projectreactor.tools</groupId>
            <artifactId>blockhound</artifactId>
            <version>1.0.6.RELEASE</version>
        </dependency>

在主方法中:

public static void main(String[] args) {
    BlockHound.install();
    SpringApplication.run(MyApplication.class,args);
  }

我的阻塞端点:

    @GetMapping("/block")
    public Mono<String> block() {
        String a = Mono.just("block").block();
        return Mono.just(a);
    }

有什么想法吗?

编辑:

当我在端点中使用 UUID.randomUUID() 时,我收到与 randomUUID() 使用的阻塞 FileInputStream#readBytes 相关的错误。 所以我想我的安装很好

解决方法

这里没有什么问题,您只是遇到了一个极端情况。

data: () => ({ infoHeight: 0,resizeObserver: null }),mounted() { this.resizeObserver = new ResizeObserver(this.onResize) this.resizeObserver.observe(this.$refs.info) this.onResize() },beforeUnmount() { this.resizeObserver.unobserve(this.$refs.info) },methods: { onResize() { this.infoHeight = this.$refs.info.clientHeight + 'px' } } 是一种相当特殊的 Mono.just() 的方式不止一种(这就是为什么我对它在这么多简单的“入门”样式示例中的使用感到绝望,但我离题了)——因为你实际上只是将一个值包装在一个虚拟发布者中,它永远不需要为了返回它的值而阻塞,即使你调用了 block 方法。方法名称可能暗示您正在阻塞,但 you can trivially verify from the source code that it just returns a value。因此不会发生阻塞操作,Blockhound 也没什么可抱怨的。

如果您要在组合中添加另一个运算符,即使它没有实际效果:

Mono

...然后您会看到 Blockhound 开始抱怨,因为您不再直接使用 String a = Mono.just("block").cache().block(); 的特殊情况。

Blockhound 正在做它应该做的事情,问题是你(非常可以理解)期望阻止一些没有。