【发布时间】:2018-05-17 09:10:14
【问题描述】:
我正在使用WebClient 对象将 Http Post 请求发送到服务器。
它非常迅速地发送大量请求(QueueChannel 中有大约 4000 条消息)。问题是......似乎服务器响应不够快......所以我收到很多服务器错误 500 并且连接过早关闭。
有没有办法限制每秒请求的数量?或者限制它使用的线程数?
编辑:
消息端点在 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();
}
}
}
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
WebClient webClient;
@Override
public void push(Data data) {
String body = constructString(data);
Mono<ResponseEntity<Response>> res = webClient.post()
.uri(URL + getLogType(data))
.contentLength(body.length())
.contentType(MediaType.APPLICATION_JSON)
.syncBody(body)
.exchange()
.flatMap(response -> response.toEntity(Response.class));
res.subscribe(new Consumer<ResponseEntity<Response>>() { ... });
}
}
【问题讨论】:
-
为什么不用 Threed.sleep ?
-
你能发布一个你如何使用
WebClient的例子吗?我确信这对于 Project Reactor 是可行的,无需任何其他库。 -
@MuratOzkan 我编辑了帖子
标签: spring spring-webflux project-reactor