0

更新:这是springboot的一个错误。https://github.com/spring-projects/spring-boot/issues/22091

我是springframe的初学者。使用 webclient 时会出现一些问题。我可以在订阅中得到想要的返回信息,但是在所有请求都返回后,文件句柄数 lsof -n |grep <javaPid> | wc -l(句柄数为2097;没有订阅只有46个,但确定目标服务器没有收到请求)大大增加,并且不能一直释放。我的代码有问题吗?感谢您的时间。

package com.test.webclienttest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@EnableAsync
public class WebclienttestApplication {

    public static void main(String[] args) {
        TestTasks tt = new TestTasks();
        tt.updateAdsInfoWhenBoot();
        SpringApplication.run(WebclienttestApplication.class, args);
    }

}
package com.test.webclienttest;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestTasks {

    public void updateAdsInfoWhenBoot() {
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put("Accept-Encoding", "gzip,deflate,br");
        headers.put("Accept", "*/*");
        String url = "https://test.com";
        String path = "/api/test";
        String body = "{\"content\":{\"data\":{}}}";
        for(int i = 0; i < 2000; i++){
            WebClient.create()
                    .post()
                    .uri(url+path)
                    .headers(httpHeaders -> {
                        for(Map.Entry<String,String> entry: headers.entrySet()){
                            httpHeaders.set(entry.getKey(), entry.getValue());
                        }
                    })
                    .body(BodyInserters.fromValue(body))
                    .retrieve()
                    .bodyToMono(String.class)
                    .subscribe(response -> {
                        log.info("b:{}", response);
                    });
        } 
        log.info("TestTasks.updateAdsInfoWhenBoot!");
    }
}

源 fd.sh 结果:

ec2-user 4606 71.3 3.8 8102884 640708 pts/1 Sl+ 11:17 0:34 java -jar ./target/webclienttest-0.0.1-SNAPSHOT.jar
PID:4606
total:2561
java:2560
GC:0
VM:0
Reference:0
Finalizer:0
Signal:0
C2:0
C1:0
Sweeper:0
Service:0
Common-Cl:0
Catalina-:0
container:0
http-nio-:0
schedulin:0
reactor-h:0
parallel-:0

fd.sh

pid=`ps aux|grep java|grep webclienttest|grep -v pushd|awk '{print $2}'`

echo `ps aux|grep java|grep webclienttest|grep -v pushd`
echo PID:$pid
echo total:`lsof -n|grep $pid|wc -l`

TYPES=("java GC VM Reference Finalizer Signal C2 C1 Sweeper Service Common-Cl Catalina- container http-nio- schedulin reactor-h parallel-")
for TYPE in ${TYPES[@]}
do
        echo $TYPE:`lsof -n|grep $pid|grep $TYPE|wc -l`
done
lsof -n|grep <PID>
(most handle is like that)
java       4606               ec2-user 2105u     sock                0,7         0t0 455065798 can't identify protocol
java       4606               ec2-user 2106u     sock                0,7         0t0 455065799 can't identify protocol
java       4606               ec2-user 2107u     sock                0,7         0t0 455065800 can't identify protocol
java       4606               ec2-user 2108u     sock                0,7         0t0 455065801 can't identify protocol
java       4606               ec2-user 2109u     sock                0,7         0t0 455065802 can't identify protocol
java       4606               ec2-user 2110u     sock                0,7         0t0 455065803 can't identify protocol
java       4606               ec2-user 2111u     sock                0,7         0t0 455065804 can't identify protocol
java       4606               ec2-user 2112u     sock                0,7         0t0 455065805 can't identify protocol
java       4606               ec2-user 2113u     sock                0,7         0t0 455065806 can't identify protocol
java       4606               ec2-user 2114u     sock                0,7         0t0 455065807 can't identify protocol
java       4606               ec2-user 2115u     sock                0,7         0t0 455065808 can't identify protocol
java       4606               ec2-user 2116u     sock                0,7         0t0 455065809 can't identify protocol
java       4606               ec2-user 2117u     sock                0,7         0t0 455065810 can't identify protocol
java       4606               ec2-user 2118u     sock                0,7         0t0 455065811 can't identify protocol

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.test</groupId>
    <artifactId>webclienttest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>webclienttest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <lombok.version>1.18.12</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4

0 回答 0