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 micro-service deployment. 2. It is convenient to start the project without downloading Tomcat or Jetty

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

  1. The number of threads
  2. timeout
  3. The JVM to optimize

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, The latency is high and it is not easy to fill the thread. This situation is relatively common in production when the network is unstable, 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 is not about how to optimize, JVM optimization is a need to scene, there are not too many specific parameters, generally running on the server side will specify the following parameters: initial memory and maximum memory will be set basically the same size, depending on the scene, -server is a must parameter, For collectors, use the default ones unless there is a specific need.

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 size of the Java stack

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 -iCopy 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

 

 

Note: Tencent cloud we media sharing plan now

Copy the code