我正在将 Spring WebFlux 和 WebClient 用于我的 Web 应用程序。
我的应用程序可能会调用同样由我们托管的“N”个其他微服务。
现在的问题是我想限制我的 WebClient 对现有微服务调用有限数量的同时调用。
另外,我不想在个人调用级别执行此操作,而是在应用程序级别执行此操作。
我已经经历过“如何限制活动 Spring WebClient 调用的数量? ”和“如何使用 WebClient 限制请求/秒? ”,但无济于事。
我正在使用一个WebClient对象将Http Post请求发送到服务器。它正在非常迅速地发送大量请求(一个中大约有4000条消息QueueChannel)。问题是...似乎服务器无法足够快地响应...所以我收到很多服务器错误500,并且过早关闭了连接。
有没有一种方法可以限制每秒的请求数?还是限制它正在使用的线程数?
编辑:
Message端点在QueueChannel中处理消息:
@MessageEndpoint
public class CustomServiceActivator {
private static final Logger logger = LogManager.getLogger();
@Autowired
IHttpService httpService;
@ServiceActivator(
inputChannel = "outputFilterChannel",
outputChannel = "outputHttpServiceChannel",
poller = @Poller( fixedDelay = "1000" )
)
public void processMessage(Data data) {
httpService.push(data);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
WebClient服务类:
@Service
public class HttpService implements IHttpService {
private static final String URL = "http://www.blabla.com/log";
private static final Logger logger = LogManager.getLogger();
@Autowired …Run Code Online (Sandbox Code Playgroud) 我有一个简单的Java程序,它使用Spring WebClient发送多个请求.每个都返回一个单声道,我使用response.subscribe()来检查结果.
但是,我的主要执行线程在处理所有请求之前完成,除非我添加一个长的Thread.sleep().
使用CompletableFutures,您可以使用:CompletableFuture.allOf(期货).join();
有没有办法等待所有Mono完成?