【发布时间】:2020-12-07 01:32:22
【问题描述】:
我正在尝试在我的项目中使用 Webclient,但是当我加载测试时,我注意到 docker 内存使用量在实例终止之前永远不会下降。
@Component
public class Controller {
//This is an endpoint to another simple api
//I use my local Ip instead of localhost in the container
private static final String ENDPOINT = "http://localhost:9090/";
private WebClient client;
public Controller(WebClient.Builder client) {
super();
this.client = client.build();
}
@Bean
public RouterFunction<ServerResponse> router() {
return RouterFunctions.route(GET("helloworld"), this::handle);
}
Mono<ServerResponse> handle(ServerRequest request) {
Mono<String> helloMono =
client.get().uri(ENDPOINT + "/hello").retrieve().bodyToMono(String.class);
Mono<String> worldMono =
client.get().uri(ENDPOINT + "/world").retrieve().bodyToMono(String.class);
return Mono.zip(helloMono, worldMono, (h, w) -> h + w)
.flatMap(s -> ServerResponse.ok().bodyValue(s));
}
}
这也是我的 dockerFile。
FROM openjdk:8
ENV SERVICE_NAME reactive-hello-world
ADD target/reactive-hello-world-*.jar $APP_HOME/reactive-hello-world.jar
RUN mkdir /opt/reactor-netty/
EXPOSE 9010
CMD java \
-Dcom.sun.management.jmxremote=true \
-Dcom.sun.management.jmxremote.local.only=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname=localhost \
-Dcom.sun.management.jmxremote.port=9010 \
-Dcom.sun.management.jmxremote.rmi.port=9010 \
-Xmx190M \
-jar reactive-hello-world.jar
EXPOSE 8080
我是否在某个地方错过了一步?
编辑:这里有一些图片
负载测试后
如您所见,GC 正常进行,但内存并未减少。如果我让测试继续,它会在几分钟内杀死实例。
我使用RestTemplate 尝试过类似的代码,但我没有遇到任何问题,即使我长时间运行 Jmeter,内存通常也不会超过 400MB。你能帮助了解发生了什么吗?
编辑:我也尝试过已弃用的AsyncRestTemplate,但我也没有发现问题。
编辑:我为此示例创建了存储库。请检查您是否可以重现该问题。
The Webclient Hello World(JMX 在这个 repo 里面)
【问题讨论】:
-
所以你正在调用端点
/hello,然后在无限循环中调用端点/hello?然后它崩溃了......还是我错过了什么? -
我在端口 9090 上创建了第二个 API,它有 2 个简单的字符串端点。
-
“内存使用量永远不会下降”——它是否曾经达到 OutOfMemoryError 的程度?如果不是,那么垃圾收集可能尚未启动。尝试运行测试,直到达到 OOM 或使用的内存下降。此外,如果您正在研究性能:您不应该为每个请求创建一个新的 WebClient,您应该自动装配一个 WebClient 实例或将其创建为类中的静态字段。
-
它没有下降,我检查了 VisualVM 发生的 GC,但内存仍然没有下降。堆内存下降,但 docker 内存几乎没有受到影响。
-
是什么让你觉得有问题,是由
WebClient引起的?
标签: spring-boot docker reactive-programming spring-webflux project-reactor