Knowledge changes fate, stroke code makes me happy, 2020 continue to walk in the open source community click “like” to see, form a habit to give me a Star, click to understand the componentialized interface services based on SpringBoot landing solution

Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar: Jar

The Nohup command

Linux system or OS X provides a command to solve the background running of application programs, that is, nohup. With this command, we can directly place the tasks to be executed in the background running. When we want to stop the running, we need to end the PID, as follows:

➜ germ-first-application git:(2.x) those who qualify nohup can go onto java-jar target/ service-application-0.1-snapshot.jar & [1] 394 appending output to nohup.outCopy the code

After executing the above commands, we can see the console output that the PID of this running program is 2349. We can use the kill command to kill this PID, so as to achieve the effect of ending the process.

Matters needing attention: When we run the application directly through java-jar xxx.jar, we did not find the output of the run log. Where did the logs go?

Run log

When you see “appending Output to Nohup. Out” printed on the console, you can guess that the log contents have been printed to a file named nohup. Out, which is in the same directory as the nohup command. Is not the jar file directory), we can run the tail -1000f nohup.out command to view the run log content, as shown below:

➜ developing - first - application git: (2 x) ✗ tail - 1000 - f nohup. Out. ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (() \ ___ | '_ |' _ | | '_ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | | | (_ | |))))' there comes | |. __ _ - | | | _ _ - | | | _ \ __, | / / / / = = = = = = = = = | _ | = = = = = = = = = = = = = = | ___ / = / _ / _ / _ / : : Spring the Boot: : (v2.2.4. RELEASE)... The 14:31:42 2020-02-21. 2349-614 the INFO [main] O.S.B.W.E mbedded. Tomcat. TomcatWebServer: tomcat started on the port (s) : 8080 (HTTP) with the context path '14:31:42 2020-02-21. 2349-617 the INFO [main] O.M.C.D.F.A.D evelopingFirstApplication: Started DevelopingFirstApplication in 1.437 seconds (JVM running for 1.75)Copy the code

Logs generated by commands executed through nohup are output to the default nohup.out file.

Specifying log files

There may be multiple Jar files that need to be run on the same server in the same directory. In order to distinguish the log output of each application, we need to specify the file name of the log output, as shown below:

➜ germ-first-application git:(2.x) qualify nohup java-jar target/ service-application0.1-snapshot.jar &> those who qualify can go onto university. Service - application - 0.0.1. Log & [1], 2579Copy the code

In the same directory as the nohup command, we can see that a log file named service-application-0.0.1.log is created.

Suggestion: The log file name format is ServiceID + Service Version. Different versions of the same ServiceID Service may be deployed.

The JVM Server mode

There is a concept of schema within the JVM. Client mode is used in development environments, but server mode is used in production servers. How do we choose?

Recommend use client mode development environment, because it started fast, can improve the efficiency of part development, save every project start time, and the production environment is recommended server mode, use within the heavyweight compiler code for C2, so although the speed of the improved application startup, but compile more thoroughly, The service performs better than the client during runtime.

Setting up the server mode is also relatively simple, we just need to execute the java-server command, as shown below:

➜ grant-first-application git:(2.x) qualify nohup java-server-jar target/ service-application0.1-snapshot.jar &> those who qualify can go onto university. Service - application - 0.0.1. Log & [1], 2707Copy the code

Initial memory (-xMS)

The JVM runs in client mode, and the default Xms size is 1 MB. The default Xms size in Server mode is 128 MB. The allocation can be modified as required, as shown below:

➜ germ-first-application git:(2.x) qualify nohup java-server-xms256m-jar Target /service-application-0.0.1- snapshot.jar &> service-application-0.0.1.log & [1] 2846Copy the code

Run the -xms256m command to change the initial memory allocation to 256 MB.

Maximum memory (-xmx)

The JVM runs in client mode and the default Xmx size is 64M, while the default Xmx size in Server mode is 1024M. The allocation can be modified as required, as shown below:

➜ germ-first-application git:(2.x) qualify nohup java-server-xms256m-xmx2048m-jar Target /service-application-0.0.1- snapshot.jar &> service-application-0.0.1.log & [1] 2340Copy the code

Run the -xmx2048m command to change the maximum allocated memory to 2048 MB.

JVM tuning scripts

JVM tuning is the most important, server configuration is limited, available resources we are to cherish, make the greatest contribution!!

For the convenience of deploying the service each time, I have encapsulated the command to start the service and named it boot-jar.sh, as follows:

#! # http://blog.yuqiyu.com nohup java-server-xx :MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms256m -Xmx1024m -Xmn256m -Xss256k -XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC -jar "$1" > "$1.log" 2>&1 & tail -1000f "$1.log"Copy the code

Run the touch boot-jar.sh command to create a startup script, copy the preceding content to the script, and run the chmod u+x boot-jar.sh command to change the permission to an executable file.

The boot-jar.sh script is as follows:

➜ grant-first-application git:(2.x) qualify./boot-jar.sh target/service-application-0.0.1- snapshot.jarCopy the code

Since the tail command is added to the script, run logs are automatically generated after the application is started.

Suggestion: Save the boot-jar.sh application startup script in the same directory as jar.


Author’s Personal blog

Use the open source framework ApiBoot to help you become an Api service architect