1. 程式人生 > 程式設計 >基於Springboot吞吐量優化解決方案

基於Springboot吞吐量優化解決方案

一、非同步執行

實現方式二種:

1.使用非同步註解@aysnc、啟動類:新增@EnableAsync註解

2.JDK 8本身有一個非常好用的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();
 //模擬長時間的計算過程
 Thread.sleep(1000);
 //告知完成結果
 future.complete(60);
 }
}

在該示例中,啟動一個執行緒,此時AskThread物件還沒有拿到它需要的資料,執行到 myRe = re.get() * re.get()會阻塞。我們用休眠1秒來模擬一個長時間的計算過程,並將計算結果告訴future執行結果,AskThread執行緒將會繼續執行。

public class Calc {
 public static Integer calc(Integer para) {
 try {
  //模擬一個長時間的執行
  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方法構造一個CompletableFuture例項,在supplyAsync()方法中,它會在一個新執行緒中,執行傳入的引數。在這裡它會執行calc()方法,這個方法可能是比較慢的,但這並不影響CompletableFuture例項的構造速度,supplyAsync()會立即返回。

而返回的CompletableFuture例項就可以作為這次呼叫的契約,在將來任何場合,用於獲得最終的計算結果。supplyAsync用於提供返回值的情況,CompletableFuture還有一個不需要返回值的非同步呼叫方法runAsync(Runnable runnable),一般我們在優化Controller時,使用這個方法比較多。

這兩個方法如果在不指定執行緒池的情況下,都是在ForkJoinPool.common執行緒池中執行,而這個執行緒池中的所有執行緒都是Daemon(守護)執行緒,所以,當主執行緒結束時,這些執行緒無論執行完畢都會退出系統。

核心程式碼:

CompletableFuture.runAsync(() ->
 this.afterBetProcessor(betRequest,betDetailResult,appUser,id)
);

非同步呼叫使用Callable來實現

@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(); 
 } 
 
 /** 
 * 非同步呼叫restful 
 * 當controller返回值是Callable的時候,springmvc就會啟動一個執行緒將Callable交給TaskExecutor去處理 
 * 然後DispatcherServlet還有所有的spring攔截器都退出主執行緒,然後把response保持開啟的狀態 
 * 當Callable執行結束之後,springmvc就會重新啟動分配一個request請求,然後DispatcherServlet就重新 
 * 呼叫和處理Callable非同步執行的返回結果, 然後返回檢視 
 * 
 * @return 
 */ 
 @GetMapping("/hello") 
 public Callable<String> helloController() { 
 logger.info(Thread.currentThread().getName() + " 進入helloController方法"); 
 Callable<String> callable = new Callable<String>() { 
 
  @Override 
  public String call() throws Exception { 
  logger.info(Thread.currentThread().getName() + " 進入call方法"); 
  String say = hello.sayHello(); 
  logger.info(Thread.currentThread().getName() + " 從helloService方法返回"); 
  return say; 
  } 
 }; 
 logger.info(Thread.currentThread().getName() + " 從helloController方法返回"); 
 return callable; 
 } 
} 

非同步呼叫的方式 WebAsyncTask

@RestController 
public class HelloController { 
 
 private static final Logger logger = LoggerFactory.getLogger(HelloController.class); 
 
 @Autowired 
 private HelloService hello; 
 
 /** 
 * 帶超時時間的非同步請求 通過WebAsyncTask自定義客戶端超時間 
 * 
 * @return 
 */ 
 @GetMapping("/world") 
 public WebAsyncTask<String> worldController() { 
 logger.info(Thread.currentThread().getName() + " 進入helloController方法"); 
 
 // 3s鐘沒返回,則認為超時 
 WebAsyncTask<String> webAsyncTask = new WebAsyncTask<>(3000,new Callable<String>() { 
 
  @Override 
  public String call() throws Exception { 
  logger.info(Thread.currentThread().getName() + " 進入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() + " 執行完畢"); 
  } 
 }); 
 
 webAsyncTask.onTimeout(new Callable<String>() { 
 
  @Override 
  public String call() throws Exception { 
  logger.info(Thread.currentThread().getName() + " onTimeout"); 
  // 超時的時候,直接拋異常,讓外層統一處理超時異常 
  throw new TimeoutException("呼叫超時"); 
  } 
 }); 
 return webAsyncTask; 
 } 
 
 /** 
 * 非同步呼叫,異常處理,詳細的處理流程見MyExceptionHandler類 
 * 
 * @return 
 */ 
 @GetMapping("/exception") 
 public WebAsyncTask<String> exceptionController() { 
 logger.info(Thread.currentThread().getName() + " 進入helloController方法"); 
 Callable<String> callable = new Callable<String>() { 
 
  @Override 
  public String call() throws Exception { 
  logger.info(Thread.currentThread().getName() + " 進入call方法"); 
  throw new TimeoutException("呼叫超時!"); 
  } 
 }; 
 logger.info(Thread.currentThread().getName() + " 從helloController方法返回"); 
 return new WebAsyncTask<>(20000,callable); 
 } 
} 

二、增加內嵌Tomcat的最大連線數

@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();
  //設定最大連線數  
  protocol.setMaxConnections(20000);
  //設定最大執行緒數  
  protocol.setMaxThreads(2000);
  protocol.setConnectionTimeout(30000);
 }
 }
}

三、使用@ComponentScan()定位掃包比@SpringBootApplication掃包更快

四、預設tomcat容器改為Undertow(Jboss下的伺服器,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 進行緩衝

六、Deferred方式實現非同步呼叫

@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() + "進入executeSlowTask方法");
 DeferredResult<String> deferredResult = new DeferredResult<>();
 // 呼叫長時間執行任務
 taskService.execute(deferredResult);
 // 當長時間任務中使用deferred.setResult("world");這個方法時,會從長時間任務中返回,繼續controller裡面的流程
 logger.info(Thread.currentThread().getName() + "從executeSlowTask方法返回");
 // 超時的回撥方法
 deferredResult.onTimeout(new Runnable(){
 
 @Override
 public void run() {
 logger.info(Thread.currentThread().getName() + " onTimeout");
 // 返回超時資訊
 deferredResult.setErrorResult("time out!");
 }
 });
 
 // 處理完成的回撥方法,無論是超時還是處理成功,都會進入這個回撥方法
 deferredResult.onCompletion(new Runnable(){
 
 @Override
 public void run() {
 logger.info(Thread.currentThread().getName() + " onCompletion");
 }
 });
 
 return deferredResult;
 }
}

七、非同步呼叫可以使用AsyncHandlerInterceptor進行攔截

@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,Object handler,ModelAndView modelAndView) throws Exception {
// HandlerMethod handlerMethod = (HandlerMethod) handler;
 logger.info(Thread.currentThread().getName()+ "服務呼叫完成,返回結果給客戶端");
 }
 
 @Override
 public void afterCompletion(HttpServletRequest request,Exception ex)
 throws Exception {
 if(null != ex){
 System.out.println("發生異常:"+ex.getMessage());
 }
 }
 
 @Override
 public void afterConcurrentHandlingStarted(HttpServletRequest request,Object handler)
 throws Exception {
 
 // 攔截之後,重新寫回資料,將原來的hello world換成如下字串
 String resp = "my name is chhliu!";
 response.setContentLength(resp.length());
 response.getOutputStream().write(resp.getBytes());
 
 logger.info(Thread.currentThread().getName() + " 進入afterConcurrentHandlingStarted方法");
 } 
}

以上這篇基於Springboot吞吐量優化解決方案就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。