一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 基于Springboot吞吐量優化解決方案

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

2020-09-26 21:20灬點點 Java教程

這篇文章主要介紹了基于Springboot吞吐量優化解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

一、異步執行

實現方式二種:

1.使用異步注解@aysnc、啟動類:添加@EnableAsync注解

2.JDK 8本身有一個非常好用的Future類——CompletableFuture

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@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線程將會繼續執行。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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(守護)線程,所以,當主線程結束時,這些線程無論執行完畢都會退出系統。


核心代碼:

?
1
2
3
CompletableFuture.runAsync(() ->
 this.afterBetProcessor(betRequest,betDetailResult,appUser,id)
);


異步調用使用Callable來實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@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的最大連接數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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)

?
1
2
3
4
5
6
<exclusions>
  <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
  </exclusion>
</exclusions>

改為:

?
1
2
3
4
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>

五、使用 BufferedWriter 進行緩沖

六、Deferred方式實現異步調用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@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進行攔截

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@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()+ "服務調用完成,返回結果給客戶端");
 }
 
 @Override
 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
 throws Exception {
 if(null != ex){
 System.out.println("發生異常:"+ex.getMessage());
 }
 }
 
 @Override
 public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, 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吞吐量優化解決方案就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_32447301/article/details/88046026

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲精品AV无码永久无码 | 精品淑女少妇AV久久免费 | 91大神在线精品播放 | 精品人人做人人爽久久久 | jux629三浦理惠子在线播放 | 亚洲女同一区二区 | 国产三级精品91三级在专区 | 国产欧美精品一区二区三区–老狼 | 人人斗地主 | 亚洲欧美久久婷婷爱综合一区天堂 | 国产区香蕉精品系列在线观看不卡 | 天天做天天爽 | 成人另类视频 | 国产性tv国产精品 | 国产色拍 | 日本大片免aaa费观看视频 | 亚洲欧美日韩久久一区 | 男人j进女屁股视频在线观看 | 91精品啪在线观看国产91九色 | 高清日韩在线 | ai换脸造梦jennie | 国产精品亚欧美一区二区三区 | bt天堂午夜国产精品 | 久久视频这只精品99re6 | 成年视频在线播放 | 亚洲尿尿 | 亚洲激情一区 | xx顶级欧美熟妞xxhd | 国产精品久久久久影视不卡 | 色老太bbbbb| 亚洲haose在线观看 | 精品国产一区二区三区久 | 多人群p全肉小说 | 4438全国最大成人网视频 | wc凹凸撒尿间谍女厕hd | 好骚好紧 | 99热精品国产麻豆 | 国产成人精品.一二区 | 免费老外的毛片清高 | 99色在线播放 | 国产欧美一区二区成人影院 |