2

【震惊】Tomcat配置参数原来这么玩?99%的人不知道的秘密!

 11 months ago
source link: https://www.51cto.com/article/769371.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【震惊】Tomcat配置参数原来这么玩?99%的人不知道的秘密!

作者:Springboot实战案例锦集 2023-10-11 08:16:42
connectionTimeout参数是说当客户端有服务器连接以后,如果客户端不输入任何内容,那么超过了connectionTimeout设置的时间后连接会被断开。

application.yml配置

server:
  port: 8081
  tomcat:
    maxThreads: 10
    maxConnections: 10
    acceptCount: 1  
    connectionTimeout: 3000

在controller中休眠10s>connectionTimeout

@RestController
@RequestMapping("/test")
public class TestController {


  @GetMapping("/index")
  public Object index() {
    try {
      System.out.println(Thread.currentThread().getName()) ;
      TimeUnit.SECONDS.sleep(10) ;
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return "success" ;
  }


}

发现程序能正常地响应。

结论:connectionTimeout参数与具体的请求响应时间是没有关系的。

通过HttpURLConnection发送请求

public class HttpURLConnectionDemo {


  public static void main(String[] args) throws Exception {
    HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8081/test/index").openConnection() ;
    con.setDoInput(true) ;
    con.setDoOutput(true) ;
    long start = System.currentTimeMillis() ;
    InputStream is = con.getInputStream() ;
    Scanner scan = new Scanner(is) ;
    while(scan.hasNext()) {
      System.out.println("接收到内容:" + scan.next() + "\n耗时:" + (System.currentTimeMillis() - start)) ;
    }
    scan.close() ;
    con.disconnect() ;
    con = null ;
  }


}
图片

图片

结论:connectionTimeout参数与什么样的客户端做连接请求没关系。

通过Socket建立连接

public class TomcatConnectionTimeoutDemo {


  public static void main(String[] args) throws Exception {
    Socket socket = new Socket("127.0.0.1", 8081) ;
    long start = System.currentTimeMillis() ;
    InputStream is = socket.getInputStream() ;
    is.read() ;
    System.out.println(System.currentTimeMillis() - start ) ;
  }


}

运行结果:

图片

图片

差不多3s后程序结束了,也就是连接断开了。接着测试:

public class TomcatConnectionTimeoutDemo {


  public static void main(String[] args) throws Exception {
    Socket socket = new Socket("127.0.0.1", 8081) ;
    long start = System.currentTimeMillis() ;
    final OutputStream os = socket.getOutputStream() ;
    new Thread(() -> {
      Scanner scan = new Scanner(System.in) ;
      while(scan.hasNext()) {
        String content = scan.next() ;
        System.out.println("准备发送:" + content) ;
        try {
          os.write(content.getBytes()) ;
          os.flush() ;
        } catch (IOException e) {
          e.printStackTrace() ;
        }
      }
    }).start() ;
    InputStream is = socket.getInputStream() ;
    is.read() ;
    System.out.println(System.currentTimeMillis() - start ) ;
  }


}

结果1(什么也不做):

图片

图片

结果2(控制台不停的输入内容):

图片

图片

程序一开始运行,控制台不停地输入内容,发现程序一直正常,当停留3秒后在输入内容,发现程序又断开了。

结论:connectionTimeout参数是说当客户端有服务器连接以后,如果客户端不输入任何内容,那么超过了connectionTimeout设置的时间后连接会被断开。

完毕!!!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK