ServerResponse方式web
@Configuration public class RouterFunctionConfiguration { @Bean // @Autowired public RouterFunction<ServerResponse> personFindAll(UserRepository userRepository){ return RouterFunctions.route(RequestPredicates.GET("/person/find/all"), request ->{ Collection<User> users = userRepository.findAll(); Flux<User> userFlux = Flux.fromIterable(users); return ServerResponse.ok().body(userFlux,User.class); }); } }
測試spring
@Test
public void threadTestOrderBy() throws InterruptedException {
final Long id=141284830240768L;
int threadCount=100000;
final CountDownLatch begin = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(threadCount);
final int[] result={0,0};
final Object lock = new Object();
ExecutorService executorService = Executors.newFixedThreadPool(100);
Long time1=System.currentTimeMillis();
for (int i = 0; i < threadCount; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
begin.await();
HttpParamsVo httpParamsVo = new HttpParamsVo(new HttpSecretVo(appKey,appSecret),params);
String resultS = HttpClientSecretSendUtil.get(url, httpParamsVo);
synchronized(lock){
result[0]++;
}
} catch (Exception ex) {
synchronized(lock) {
result[1]++;
}
ex.printStackTrace();
}finally{
end.countDown();
}
}
});
executorService.submit(thread);
}
System.out.println(threadCount+"個線程開始");
begin.countDown();
end.await();
Long time2=System.currentTimeMillis();
System.out.println("耗時: "+(time2-time1)/1000+"秒");
System.out.println(threadCount+"個線程更新結束");
System.out.println("成功"+result[0]+",失敗"+result[1]);
}
耗時app
servlet方式ide
@GetMapping("/person/find/all") @ResponseBody public Collection<User> findAll(){ return this.userRepository.findAll(); }
測試同上測試
結果this
總結100W個線程的狀況下url
spring 5提供的 webFlux 相比 servlet模型 慢了17秒spa