[转帖]7 种提升 Spring Boot 吞吐量神技!

提升,spring,boot,吞吐量,神技 · 浏览次数 : 0

小编点评

1. **使用@Configuration**配置扫描类** - **@Bean**方法配置Servlet工厂** - **@Bean**方法配置连接自定义器** - **@Bean**方法配置时间任务** 2. **使用**@Async**注释**异步调用** - **@Async**方法配置任务执行器** - **@Async**方法配置回调方法 3. **使用**@Configuration**配置扫描类** - **@Bean**方法配置Undertow容器** 4. **使用**@Configuration**配置扫描类** - **@Bean**方法配置Undertow连接自定义器** 5. **使用**BufferedWriter**进行缓冲** - **@Async**方法配置异步调用** 6. **使用**AsyncHandlerInterceptor**进行拦截** - **@Component**扫描类** - **@Async**方法配置回调方法** 7. **使用**@Async**注释**异步调用** - **@Async**方法配置任务执行器** - **@Async**方法配置回调方法

正文

https://cloud.tencent.com/developer/article/2045348?areaSource=105001.6&traceId=7RuArY2Tm1MQWwQaMnx-Q

 

一、异步执行

实现方式二种:

1、 使用异步注解@aysnc启动类:添加@EnableAsync注解

2、 JDK8本身有一个非常好用的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, 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方法");
 }
 
}

与[转帖]7 种提升 Spring Boot 吞吐量神技!相似的内容:

[转帖]7 种提升 Spring Boot 吞吐量神技!

https://cloud.tencent.com/developer/article/2045348?areaSource=105001.6&traceId=7RuArY2Tm1MQWwQaMnx-Q 一、异步执行 实现方式二种: 1、 使用异步注解@aysnc、启动类:添加@EnableAsyn

【转帖】Linux 系统双网卡绑定 bond的7种模式

第一种模式:mod=0 ,即:(balance-rr) Round-robin policy(平衡抡循环策略)第二种模式:mod=1,即: (active-backup) Active-backup policy(主-备份策略)第三种模式:mod=2,即:(balance-xor) XOR poli

[转帖]重置 VCSA 6.7 root密码和SSO密码

问题描述 1、用root用户登录 VMware vCenter Server Appliance虚拟机失败,无法登录 2、vCenter Server Appliance 6.7 U1的root帐户错误尝试次数超过3次已锁定或帐户已过期 官方说明 在VCSA 6.7 U1中​​,SSO用户(即常用的

[转帖]重置 VCSA 6.7 root密码和SSO密码

问题描述 1、用root用户登录 VMware vCenter Server Appliance虚拟机失败,无法登录 2、vCenter Server Appliance 6.7 U1的root帐户错误尝试次数超过3次已锁定或帐户已过期 官方说明 在VCSA 6.7 U1中​​,SSO用户(即常用的

[转帖]7 个使用 bcc/BPF 的性能分析神器

https://t.cj.sina.com.cn/articles/view/1772191555/69a17f430190029mf 在 Linux 中出现的一种新技术能够为系统管理员和开发者提供大量用于性能分析和故障排除的新工具和仪表盘。它被称为增强的伯克利数据包过滤器(eBPF,或 BPF),

[转帖]Red Hat Enterprise Linux 8 和 9 中可用的 IO 调度程序

Red Hat 弃用了 Red Hat Enterprise Linux 7 中可用的 I/O 调度程序,并引入了四个新的 I/O 调度程序,如下所示, 运行以下命令检查 RHEL8 和 RHEL9 中可用的调度程序 # dmesg | grep -i scheduler [ 0.507104] i

[转帖]7 个使用 bcc/BPF 的性能分析神器

https://linux.cn/article-9139-1.html 使用伯克利包过滤器Berkeley Packet Filter(BPF)编译器集合Compiler Collection(BCC)工具深度探查你的 Linux 代码。 在 Linux 中出现的一种新技术能够为系统管理员和开发者

[转帖]CentOS 7系统优化脚本

https://zhuanlan.zhihu.com/p/530923408 作为一名运维,经常会部署各种用途的操作系统,但在这些工作中,我们会发现很多工作其实是重复性的劳动,操作的内容也是大同小异,基于这类情况,我们可以把相同的操作做成统一执行的脚本,不同的东西作为变量手动输入。节约下来的时间不就

[转帖].NET 7 正式发布

https://www.oschina.net/news/216967/dotnet-7-released 微软宣布正式推出 .NET 7 ,使用 .NET 7 可以轻松地将 .NET 7 项目容器化,在 GitHub 操作中设置 CI/CD 工作流,并实现云原生可观察性。 .NET 7 是标准期限

[转帖]Redhat 8 磁盘调度策略变化:NOOP改为NONE

说明 在 redhat 4/5/6/7版本中的NOOP调度策略,从8开始修改为NONE,官方解释: none Implements a first-in first-out (FIFO) scheduling algorithm. It merges requests at the generic