使用Spring 5.0 WebClient进行异步调用将抛出连接,但成功调用很少

时间:2019-01-18 00:52:55

标签: spring rest spring-boot webclient spring-webflux

我正在尝试通过WebClient发出异步发布请求,因为这是使用Springframework 5.0时的首选方式。

这是我的服务器代码:

    A
    28.57223993
    28.30616607
    28.2447644
    28.29934366
    28.39485735
    28.80420177
    29.29541506
    29.42504079
    29.31588228
    29.51373208
    30.25737443
    28.94747231
    28.85195861
    28.72915529
    29.17943414
    29.12485489
    29.04298601
    28.96111712
    27.95822332
    28.5381279
    28.68822085
    28.12878349
    27.96504572
    29.32952709
    30.31877609
    30.1345711
    29.629713
    30.01859019
    30.71447569
    30.55756033
    29.09756526
    29.72522669
    29.96401093
    29.96401093
    28.98840675
    27.59663575
    28.07420423
    28.89971546
    28.70868807
    27.75355111
    28.28569885
    29.21354618
    31.89475207
    31.29438027
    31.36260434
    31.41718359

我拥有的客户端代码是:

package com.example.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@Slf4j
public class DemoApplication {

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

    @PostMapping(value = "/createCustomer", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = "text/plain")
    public Customer sendStream(@RequestBody String number) throws InterruptedException {
        Thread.sleep(40);
        return new Customer(number, "random" + number);
    }

    @Data
    @AllArgsConstructor
    class Customer {
        String id;
        String name;
    }


}

在成功运行此代码后,出现以下异常。

    package com.example.demo2;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

import java.util.stream.IntStream;

@SpringBootApplication
@Slf4j
public class Demo2Application implements CommandLineRunner {


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


    @Override
    public void run(String... args) throws Exception {
        WebClient webClient = WebClient.create("http://localhost:8080");
        IntStream.range(0, 100000).forEach(i -> {
            Flux.merge(
                    webClient
                            .post()
                            .uri("/createCustomer")
                            .body(BodyInserters.fromObject(String.valueOf(i)))
                            .retrieve().bodyToMono(String.class)
            )
                    .parallel()
                    .runOn(Schedulers.single())
                    .subscribe(log::info);


        });

    }
}

我在做什么错,我试图限制线程数,但这没有帮助,在使用单线程的客户端代码中可以看到。

任何帮助将不胜感激。

这是一个错误吗?

0 个答案:

没有答案