基于Springboot吞吐量?jī)?yōu)化解決方案
一、異步執(zhí)行
實(shí)現(xiàn)方式二種:
1.使用異步注解@aysnc、啟動(dòng)類:添加@EnableAsync注解
2.JDK 8本身有一個(gè)非常好用的Future類——CompletableFuture
@AllArgsConstructor
public class AskThread implements Runnable{
private CompletableFuture<Integer> re = null;
public void run() {
int myRe = 0;
try {
myRe = re.get() * re.get();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(myRe);
}
public static void main(String[] args) throws InterruptedException {
final CompletableFuture<Integer> future = new CompletableFuture<>();
new Thread(new AskThread(future)).start();
//模擬長(zhǎng)時(shí)間的計(jì)算過(guò)程
Thread.sleep(1000);
//告知完成結(jié)果
future.complete(60);
}
}
在該示例中,啟動(dòng)一個(gè)線程,此時(shí)AskThread對(duì)象還沒(méi)有拿到它需要的數(shù)據(jù),執(zhí)行到 myRe = re.get() * re.get()會(huì)阻塞。我們用休眠1秒來(lái)模擬一個(gè)長(zhǎng)時(shí)間的計(jì)算過(guò)程,并將計(jì)算結(jié)果告訴future執(zhí)行結(jié)果,AskThread線程將會(huì)繼續(xù)執(zhí)行。
public class Calc {
public static Integer calc(Integer para) {
try {
//模擬一個(gè)長(zhǎng)時(shí)間的執(zhí)行
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return para * para;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
final CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> calc(50))
.thenApply((i) -> Integer.toString(i))
.thenApply((str) -> "\"" + str + "\"")
.thenAccept(System.out::println);
future.get();
}
}
CompletableFuture.supplyAsync方法構(gòu)造一個(gè)CompletableFuture實(shí)例,在supplyAsync()方法中,它會(huì)在一個(gè)新線程中,執(zhí)行傳入的參數(shù)。在這里它會(huì)執(zhí)行calc()方法,這個(gè)方法可能是比較慢的,但這并不影響CompletableFuture實(shí)例的構(gòu)造速度,supplyAsync()會(huì)立即返回。
而返回的CompletableFuture實(shí)例就可以作為這次調(diào)用的契約,在將來(lái)任何場(chǎng)合,用于獲得最終的計(jì)算結(jié)果。supplyAsync用于提供返回值的情況,CompletableFuture還有一個(gè)不需要返回值的異步調(diào)用方法runAsync(Runnable runnable),一般我們?cè)趦?yōu)化Controller時(shí),使用這個(gè)方法比較多。
這兩個(gè)方法如果在不指定線程池的情況下,都是在ForkJoinPool.common線程池中執(zhí)行,而這個(gè)線程池中的所有線程都是Daemon(守護(hù))線程,所以,當(dāng)主線程結(jié)束時(shí),這些線程無(wú)論執(zhí)行完畢都會(huì)退出系統(tǒng)。
核心代碼:
CompletableFuture.runAsync(() -> this.afterBetProcessor(betRequest,betDetailResult,appUser,id) );
異步調(diào)用使用Callable來(lái)實(shí)現(xiàn)
@RestController
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
private HelloService hello;
@GetMapping("/helloworld")
public String helloWorldController() {
return hello.sayHello();
}
/**
* 異步調(diào)用restful
* 當(dāng)controller返回值是Callable的時(shí)候,springmvc就會(huì)啟動(dòng)一個(gè)線程將Callable交給TaskExecutor去處理
* 然后DispatcherServlet還有所有的spring攔截器都退出主線程,然后把response保持打開的狀態(tài)
* 當(dāng)Callable執(zhí)行結(jié)束之后,springmvc就會(huì)重新啟動(dòng)分配一個(gè)request請(qǐng)求,然后DispatcherServlet就重新
* 調(diào)用和處理Callable異步執(zhí)行的返回結(jié)果, 然后返回視圖
*
* @return
*/
@GetMapping("/hello")
public Callable<String> helloController() {
logger.info(Thread.currentThread().getName() + " 進(jìn)入helloController方法");
Callable<String> callable = new Callable<String>() {
@Override
public String call() throws Exception {
logger.info(Thread.currentThread().getName() + " 進(jìn)入call方法");
String say = hello.sayHello();
logger.info(Thread.currentThread().getName() + " 從helloService方法返回");
return say;
}
};
logger.info(Thread.currentThread().getName() + " 從helloController方法返回");
return callable;
}
}
異步調(diào)用的方式 WebAsyncTask
@RestController
public class HelloController {
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@Autowired
private HelloService hello;
/**
* 帶超時(shí)時(shí)間的異步請(qǐng)求 通過(guò)WebAsyncTask自定義客戶端超時(shí)間
*
* @return
*/
@GetMapping("/world")
public WebAsyncTask<String> worldController() {
logger.info(Thread.currentThread().getName() + " 進(jìn)入helloController方法");
// 3s鐘沒(méi)返回,則認(rèn)為超時(shí)
WebAsyncTask<String> webAsyncTask = new WebAsyncTask<>(3000, new Callable<String>() {
@Override
public String call() throws Exception {
logger.info(Thread.currentThread().getName() + " 進(jìn)入call方法");
String say = hello.sayHello();
logger.info(Thread.currentThread().getName() + " 從helloService方法返回");
return say;
}
});
logger.info(Thread.currentThread().getName() + " 從helloController方法返回");
webAsyncTask.onCompletion(new Runnable() {
@Override
public void run() {
logger.info(Thread.currentThread().getName() + " 執(zhí)行完畢");
}
});
webAsyncTask.onTimeout(new Callable<String>() {
@Override
public String call() throws Exception {
logger.info(Thread.currentThread().getName() + " onTimeout");
// 超時(shí)的時(shí)候,直接拋異常,讓外層統(tǒng)一處理超時(shí)異常
throw new TimeoutException("調(diào)用超時(shí)");
}
});
return webAsyncTask;
}
/**
* 異步調(diào)用,異常處理,詳細(xì)的處理流程見MyExceptionHandler類
*
* @return
*/
@GetMapping("/exception")
public WebAsyncTask<String> exceptionController() {
logger.info(Thread.currentThread().getName() + " 進(jìn)入helloController方法");
Callable<String> callable = new Callable<String>() {
@Override
public String call() throws Exception {
logger.info(Thread.currentThread().getName() + " 進(jìn)入call方法");
throw new TimeoutException("調(diào)用超時(shí)!");
}
};
logger.info(Thread.currentThread().getName() + " 從helloController方法返回");
return new WebAsyncTask<>(20000, callable);
}
}
二、增加內(nèi)嵌Tomcat的最大連接數(shù)
@Configuration
public class TomcatConfig {
@Bean
public ConfigurableServletWebServerFactory webServerFactory() {
TomcatServletWebServerFactory tomcatFactory = new TomcatServletWebServerFactory();
tomcatFactory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());
tomcatFactory.setPort(8005);
tomcatFactory.setContextPath("/api-g");
return tomcatFactory;
}
class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {
public void customize(Connector connector) {
Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
//設(shè)置最大連接數(shù)
protocol.setMaxConnections(20000);
//設(shè)置最大線程數(shù)
protocol.setMaxThreads(2000);
protocol.setConnectionTimeout(30000);
}
}
}
三、使用@ComponentScan()定位掃包比@SpringBootApplication掃包更快
四、默認(rèn)tomcat容器改為Undertow(Jboss下的服務(wù)器,Tomcat吞吐量5000,Undertow吞吐量8000)
<exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions>
改為:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
五、使用 BufferedWriter 進(jìn)行緩沖
六、Deferred方式實(shí)現(xiàn)異步調(diào)用
@RestController
public class AsyncDeferredController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final LongTimeTask taskService;
@Autowired
public AsyncDeferredController(LongTimeTask taskService) {
this.taskService = taskService;
}
@GetMapping("/deferred")
public DeferredResult<String> executeSlowTask() {
logger.info(Thread.currentThread().getName() + "進(jìn)入executeSlowTask方法");
DeferredResult<String> deferredResult = new DeferredResult<>();
// 調(diào)用長(zhǎng)時(shí)間執(zhí)行任務(wù)
taskService.execute(deferredResult);
// 當(dāng)長(zhǎng)時(shí)間任務(wù)中使用deferred.setResult("world");這個(gè)方法時(shí),會(huì)從長(zhǎng)時(shí)間任務(wù)中返回,繼續(xù)controller里面的流程
logger.info(Thread.currentThread().getName() + "從executeSlowTask方法返回");
// 超時(shí)的回調(diào)方法
deferredResult.onTimeout(new Runnable(){
@Override
public void run() {
logger.info(Thread.currentThread().getName() + " onTimeout");
// 返回超時(shí)信息
deferredResult.setErrorResult("time out!");
}
});
// 處理完成的回調(diào)方法,無(wú)論是超時(shí)還是處理成功,都會(huì)進(jìn)入這個(gè)回調(diào)方法
deferredResult.onCompletion(new Runnable(){
@Override
public void run() {
logger.info(Thread.currentThread().getName() + " onCompletion");
}
});
return deferredResult;
}
}
七、異步調(diào)用可以使用AsyncHandlerInterceptor進(jìn)行攔截
@Component
public class MyAsyncHandlerInterceptor implements AsyncHandlerInterceptor {
private static final Logger logger = LoggerFactory.getLogger(MyAsyncHandlerInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// HandlerMethod handlerMethod = (HandlerMethod) handler;
logger.info(Thread.currentThread().getName()+ "服務(wù)調(diào)用完成,返回結(jié)果給客戶端");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
if(null != ex){
System.out.println("發(fā)生異常:"+ex.getMessage());
}
}
@Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// 攔截之后,重新寫回?cái)?shù)據(jù),將原來(lái)的hello world換成如下字符串
String resp = "my name is chhliu!";
response.setContentLength(resp.length());
response.getOutputStream().write(resp.getBytes());
logger.info(Thread.currentThread().getName() + " 進(jìn)入afterConcurrentHandlingStarted方法");
}
}
以上這篇基于Springboot吞吐量?jī)?yōu)化解決方案就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
完美解決springboot項(xiàng)目出現(xiàn)”java: 錯(cuò)誤: 無(wú)效的源發(fā)行版:17“問(wèn)題(圖文詳解)
這篇文章主要介紹了完美解決springboot項(xiàng)目出現(xiàn)”java: 錯(cuò)誤: 無(wú)效的源發(fā)行版:17“問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
在SpringBoot中使用JWT的實(shí)現(xiàn)方法
這篇文章主要介紹了在SpringBoot中使用JWT的實(shí)現(xiàn)方法,詳細(xì)的介紹了什么是JWT和JWT實(shí)戰(zhàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
SpringMVC多個(gè)模塊404報(bào)錯(cuò)問(wèn)題及解決
這篇文章主要介紹了SpringMVC多個(gè)模塊404報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
win10安裝JDK14.0.2的詳細(xì)安裝過(guò)程
這篇文章主要介紹了win10安裝JDK14.0.2的詳細(xì)安裝過(guò)程的相關(guān)資料,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
IDEA中l(wèi)og4j 無(wú)法輸出到本地 properties配置無(wú)效問(wèn)題
這篇文章主要介紹了IDEA中l(wèi)og4j 無(wú)法輸出到本地 properties配置無(wú)效問(wèn)題,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Java使用POI實(shí)現(xiàn)excel文件的導(dǎo)入和導(dǎo)出
這篇文章主要為大家詳細(xì)介紹了Java如何使用POI實(shí)現(xiàn)excel文件的導(dǎo)入和導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
IDEA2022搭建Spring?Cloud多模塊項(xiàng)目的詳細(xì)過(guò)程
這篇文章主要介紹了IDEA2022搭建Spring?Cloud多模塊項(xiàng)目,網(wǎng)上有很多教程父模塊都是通過(guò)maven的方式創(chuàng)建的,然后子模塊是通過(guò)Spring?Initalizr方式創(chuàng)建,這種方式父模塊無(wú)法管理子模塊的依賴仲裁,需要每個(gè)子模塊自行管理,就失去了父模塊的用處了2022-10-10

