introduce

In SpringBoot’s Web project, Tomcat is built in by default, but you can also configure it to support jetty. What are the benefits of built-in Tomcat?

  1. Facilitate microservice deployment.
  2. Easy to start projects, no need to download Tomcat or Jetty

For current container optimization, there is not much to consider at this point

  • The number of threads
  • timeout
  • The JVM to optimize

In view of the above optimization points, the number of threads is a key point first, the number of initial threads and the maximum number of threads, the initial number of threads to ensure that when starting, if there are a large number of users access, can be very stable to accept requests.

The maximum number of threads is used to ensure the stability of the system, and the timeout time is used to ensure that the number of connections is not easily overwhelmed, if a large number of requests come, the delay is relatively high, it is not easy to fill the thread. This situation is relatively common in production, once the network is unstable, prefer to lose packets rather than crush the machine.

JVM optimizations generally do not have many scenarios, except to increase the initial heap, and the maximum heap limit, but not infinitely, depending on the situation to start fast

In the Spring Boot configuration file application.yml, add the following configuration

server:
  tomcat:
    min-spare-threads: 20
    max-threads: 100
  connection-timeout: 5000
Copy the code

This optimizes tomcat with a maximum number of threads of 100, initialization threads of 20, and a timeout of 5000ms

The Jvm to optimize

This section is not about how to optimize, JVM optimization is a need to scene, there is no too many specific parameters, generally running on the server will specify the following parameters

The initial memory and maximum memory will be set to the same size, depending on the scenario. -server is a mandatory parameter. For collectors, use the default, unless there is a specific requirement.

1. Use the -server mode

Set the JVM to server mode. The 64-bit JDK starts this mode by default

Java server - jar springboot - 1.0. The jarCopy the code

2. Specify heap parameters

This sets the heap parameters based on the memory size of the server.

  • -xms: sets the initial size of the Java stack
  • -Xmx: Sets the maximum Java heap size
Java -server -xms512m -xmx768m -jar Springboot -1.0.jarCopy the code

Set the initial heap memory to 512MB and maximum to 768MB.

3. The remote Debug

Change the startup parameters on the server to:

java -Djavax.net.debug= ssl -Xdebug -Xnoagent -Djava.compiler= NONE -Xrunjdwp:transport= dt_socket,server=y,suspend= N, address = 8888 - jar springboot - 1.0. The jarCopy the code

The remote Debug mode of the server is enabled and the port number is 8888.

In IDEA, click the Edit Configuration button.

When a popup window appears, click the + button to find the Remote option.

Fill in the Remote project name in [1], IP address and port number in [2], select the project Module for Remote debugging in [3], and click OK after the configuration is complete

If the connection times out, it is likely that the server firewall is faulty. For example, CentOs7, turn off the firewall

Systemctl stop firewalld. Service # stop firewall systemctl disable firewalldCopy the code

Click the debug button to print the message on the IDEA console:

The remote debugging succeeds.

JVM tool remote connection

Jconsole connects to Jvisualvm remotely

Usually our Web services are deployed on the server, so using JConsole on Windows is convenient, but a bit more cumbersome than Linux, requiring some setup.

1. Check hostname

hostname -i
Copy the code

The hostname of the server is 127.0.0.1

2. Modify the hostname

Change 127.0.0.1 localhost. Localdomain localhost in the first line of the /etc/hosts file to: Localdomain localhost 192.168.44.128 is the IP address of the actual server

3. Restart Linux, enter hostname -i on the server, and check whether the actual IP address is the one you set

4. Start the service as follows:

Java jar - Djava. Rmi. Server hostname = 192.168.44.128 - Dcom. Sun. Management jmxremote -Dcom.sun.management.jmxremote.port=911 - Dcom.sun.management.jmxremote.ssl=false - Dcom. Sun. Management. Jmxremote. Authenticate = false jantent - 1.0 - the SNAPSHOT. The jarCopy the code

The IP address is 192.168.44.128 and the port number is 911.

5. Open Jconsole and enter the IP address and port number for remote connection

Click connect, and after a little waiting, the connection can be completed, as shown in the picture below:

Similarly, the remote connection for JvisualVm is the same, as are the startup parameters.

Then enter IP: PORT in the local JvisualVm to make a remote connection: as shown in the picture below:

Compared with Jvisualvm, the function is more powerful and the interface is more beautiful.

In addition, the author has completed two columns of the article Mybatis advanced, Spring Boot advanced, has organized the column into a book, there is a need for the public number [code monkey technology column] reply keywords Mybatis advanced, Spring Boot advanced free access.