SpringBoot启动成功后调用接口、方法

简介: SpringBoot启动成功后调用接口、方法

问题


  线上问题遇到一个接口第一次访问特别慢的问题,后来说是因为该接口加了某注解,所以第一次请求比较慢,初步解决办法就是启动后先请求一次就好了。


代码


模拟测试接口


  @RequestMapping("/hello")
  public String hello() {
    return LocalDateTime.now().toString();
  }


核心接口CommandLineRunner


package com.example.autorequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * @author chaird
 * @create 2020-07-31 22:43
 */
@Component
@Slf4j
public class StartAutoRequestService implements CommandLineRunner {
  @Override
  public void run(String... args) throws Exception {
    String httpUrl = "http://localhost:8082/hello";
    // 链接
    HttpURLConnection connection = null;
    InputStream is = null;
    BufferedReader br = null;
    StringBuffer result = new StringBuffer();
    try {
      // 创建连接
      URL url = new URL(httpUrl);
      connection = (HttpURLConnection) url.openConnection();
      // 设置请求方式
      connection.setRequestMethod("GET");
      // 设置连接超时时间
      connection.setReadTimeout(15000);
      // 开始连接
      connection.connect();
      // 获取响应数据
      if (connection.getResponseCode() == 200) {
        // 获取返回的数据
        is = connection.getInputStream();
        if (null != is) {
          br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
          String temp = null;
          while (null != (temp = br.readLine())) {
            result.append(temp);
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (null != br) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (null != is) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      // 关闭远程连接
      connection.disconnect();
    }
    System.out.println(result.toString());
  }
}

原理


首先在自己实现CommandLineRunner接口的类的run方法里打个断点,看一下调用栈,这样好知道在哪打断点


5.png


在上图箭头处的方法的第一行打断点

public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
      ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
      configureIgnoreBeanInfo(environment);
      Banner printedBanner = printBanner(environment);
      context = createApplicationContext();
      exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
          new Class[] { ConfigurableApplicationContext.class }, context);
      prepareContext(context, environment, listeners, applicationArguments, printedBanner);
      refreshContext(context);
            //断点走到这,此时控制台已经打印出Tomcat started on port(s): 8082 (http) with context path ''
            //断点走到这,此时控制台已经打印出Tomcat started on port(s): 8082 (http) with context path ''
           //断点走到这,此时控制台已经打印出Tomcat started on port(s): 8082 (http) with context path ''
      afterRefresh(context, applicationArguments);
      stopWatch.stop();
      if (this.logStartupInfo) {
        new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
      }
      listeners.started(context);
            //开始调用实现CommandLineRunner接口的方法
            //开始调用实现CommandLineRunner接口的方法
            //开始调用实现CommandLineRunner接口的方法
      callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
    }
    try {
      listeners.running(context);
    }
    catch (Throwable ex) {
    }
    return context;
  }

进入上图的callRunners方法


  private void callRunners(ApplicationContext context, ApplicationArguments args) {
    List<Object> runners = new ArrayList<>();
    runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
    //拿到实现CommandLineRunner接口的实现类
    runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
    AnnotationAwareOrderComparator.sort(runners);
    for (Object runner : new LinkedHashSet<>(runners)) {
      if (runner instanceof ApplicationRunner) {
        callRunner((ApplicationRunner) runner, args);
      }
      if (runner instanceof CommandLineRunner) {
           //调用方法,此时在跟进去就进入自己写的方法里了
        callRunner((CommandLineRunner) runner, args);
      }
    }
  }
目录
相关文章
|
15天前
|
人工智能 JSON 前端开发
Spring Boot解决跨域问题方法汇总
Spring Boot解决跨域问题方法汇总
|
21天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
|
1月前
|
NoSQL Java Redis
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
SpringBoot集成Redis解决表单重复提交接口幂等(亲测可用)
373 0
|
1月前
|
Java API Spring
SpringBoot项目调用HTTP接口5种方式你了解多少?
SpringBoot项目调用HTTP接口5种方式你了解多少?
96 2
|
2月前
|
前端开发 安全 Java
Spring Boot 三招组合拳,手把手教你打出优雅的后端接口
Spring Boot 三招组合拳,手把手教你打出优雅的后端接口
43 0
|
1月前
|
JSON 前端开发 Java
前端请求SpringBoot接口出现Required request body is missing
前端请求SpringBoot接口出现Required request body is missing
38 2
|
2月前
|
Java 数据库
SpringBoot手动取消接口执行方案
实际开发中经常会遇到比较耗时的接口操作,但页面强制刷新或主动取消接口调用后后台还是会继续运行,特别是有大量数据库操作时会增加服务器压力,所以进行研究测试后总结了一套主动取消接口调用的解决方案
17 0
|
1月前
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
43 2
|
4天前
|
Java Spring
spring boot访问接口报500
spring boot访问接口报500
11 2
|
8天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
20 2