Detail the Tomcat Https configuration and Jetty optimization of the initial SpringBoot tutorial

  • 2020-10-31 21:44:51
  • OfStack

1. Introduction

In SpringBoot's Web project, the default is the built-in Tomcat. You can also configure jetty to support the built-in jetty. What are the advantages of the built-in Tomcat?

1. Facilitate the deployment of micro-services.
2. Easy to start the project, no need to download Tomcat or Jetty

In the current company, the built-in Jetty has been deployed into the online project, so far there is no big problem. Even if there is a performance loss, by deploying multiple machines,

It is also easy to solve such problems, as the built-in container is easy to deploy and migrate.

1.1 Optimization strategy

For the current container optimizations, there is not much left to consider at this point

The number of threads timeout jvm optimization

For optimal point, the first number of threads is a key, the initial number of threads and maximum number of threads, the initial number of threads to ensure startup, if you have a large number of users to access, can be very stable to accept the request, and the maximum number of threads used to guarantee the stability of the system and timeout to guarantee the number of connections is not easy to be overwhelmed, if large quantity request, delay is higher, it is not easy to beat thread. This situation is quite common in production

Once the network is unstable, I would rather lose packets than crush the machine.

Generally speaking, jvm optimization 1 does not have many scenarios. It is just to increase the initial heap and the maximum limit heap, but it is also not infinite increase. It is adjusted according to the situation

2. Start fast

3.1 Tomcat SSL

tomcat's SSL configuration is very simple, first through JDK way generated.keystore, this way of certificate 1 generally is not very recognized, the best way to apply online, Ali cloud and Tencent cloud can apply for free, this way configured https, google browser will prompt https is not certified


keytool -genkey -alias tomcat -keyalg RSA

application-tomcat.yaml

This block has 1 optimized configuration for tomcat, with a maximum number of threads of 100, an initialization thread of 20, and a timeout of 5000ms


 server:
  tomcat:
  max-threads: 100
  min-spare-threads: 20
  connection-timeout: 5000
  ssl:
  key-store: classpath:.keystore
  key-store-type: JKS
  key-password: qq123456
  key-alias: tomcat
  port: 8443

Start the class

An httpConnector is added to the boot class section to support https access and http access


 @SpringBootApplication
 public class AppApplication {
  public static void main(String args[]) {
   SpringApplication.run(AppApplication.class, args);
  }

  @Bean
  public EmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() throws IOException {
   TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
   tomcat.addAdditionalTomcatConnectors(httpConnector());
   return tomcat;
  }

  public Connector httpConnector() throws IOException {
   Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
   Http11NioProtocol http11NioProtocol = (Http11NioProtocol) connector.getProtocolHandler();
   connector.setPort(8080);
   // Set the maximum number of threads 
   http11NioProtocol.setMaxThreads(100);
   // Set the initial number of threads   Minimum number of free threads 
   http11NioProtocol.setMinSpareThreads(20);
   // Set the timeout 
   http11NioProtocol.setConnectionTimeout(5000);
   return connector;
  }

 }

This completes the configuration of https, and if it starts successfully, you can see that tomcat is listening on two ports when it starts


2016-11-21 10:53:15.247 INFO 42382 --- [   main] com.start.AppApplication     : Starting AppApplication on elemebjdeMacBook-Pro.local with PID 42382 (/Users/wangkang/code/SpringBoot-Learn/springboot-9/target/classes started by wangkang in /Users/wangkang/code/SpringBoot-Learn)
2016-11-21 10:53:15.251 INFO 42382 --- [   main] com.start.AppApplication     : No active profile set, falling back to default profiles: default
2016-11-21 10:53:15.426 INFO 42382 --- [   main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@489115ef: startup date [Mon Nov 21 10:53:15 CST 2016]; root of context hierarchy
2016-11-21 10:53:19.164 INFO 42382 --- [   main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8443 (https) 8080 (http)
2016-11-21 10:53:19.193 INFO 42382 --- [   main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-11-21 10:53:19.194 INFO 42382 --- [   main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5

3.2 jvm optimization

This is not mainly about how to optimize, jvm optimization is 1 need scene, there is no too many specific parameters, 1 generally run on server side will specify the following parameters
The initial memory and maximum memory will be set to 1, the specific size according to the scene setting, our online environment 1 is usually 4G, because the machine is 16G, -ES78en is a parameter that must be used, as for the collector, the default is ok, unless there is a specific requirement


java -Xms4g -Xmx4g -Xmn768m -server -jar springboot-9-1.4.1.RELEASE.jar

4 jetty configuration

pom.xml

springboot adds 1 starter for jetty, just add 1 dependency to pom


  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jetty</artifactId>
  </dependency>

The following is the thread optimization for jetty, with 1 configuration. Of course, it is also available without configuration. In the online environment, it is best to configure 1 for optimization


@Profile("jetty")
  @Bean
  public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory(
    JettyServerCustomizer jettyServerCustomizer) {
   JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory();
   factory.addServerCustomizers(jettyServerCustomizer);
   return factory;
  }


  @Bean
  public JettyServerCustomizer jettyServerCustomizer() {
   return server -> {
    // Tweak the connection config used by Jetty to handle incoming HTTP
    // connections
    final QueuedThreadPool threadPool = server.getBean(QueuedThreadPool.class);
    threadPool.setMaxThreads(100);
    threadPool.setMinThreads(20);
   };
  }

4.1 jetty https configuration

application-jetty.yaml

There is not much difference between the https configuration and THE tomcat configuration. This one is the unified configuration, and SpringBoot is just an abstraction


server:
 connection-timeout: 5000
 ssl:
 key-store: classpath:.keystore
 key-store-type: JKS
 key-password: qq123456
 key-alias: tomcat
 port: 8444

3 summary

1 https is generally not configured with tomcat in production because in our production environment, tomcat is a template for unity 1 and can only change the number of threads. One way of doing it is through

nginx is configured with https, which is also easy to configure and easy to restart


Related articles: