preface

Nacos is often used in Spring Cloud projects, but using the default JVM configuration of Nacos can be a waste of resources if the test environment or microservices are relatively small. Especially low-configuration servers for research purposes.

The default JVM needs to be adjusted either for production environment optimization or for testing environments that do not waste resources.

Nacos memory configuration

Nacos is a Spring Boot-based project that uses memory parameters that are configured in startup commands. The startup command is in startup.sh (or.cmd) :


#===========================================================================================
# JVM Configuration
#===========================================================================================
if [[ "${MODE}" == "standalone" ]]; then
    JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
    JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
else
    if [[ "${EMBEDDED_STORAGE}" == "embedded" ]]; then
        JAVA_OPT="${JAVA_OPT} -DembeddedStorage=true"
    fi
    JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
    JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
    JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"

fi
Copy the code

If standalone mode is used, the default memory parameter is -xms512m -xmx512m -xmn256m.

However, if you boot in any other mode, the memory will be allocated 2 GIGABytes, which may not be used at all, and this will be a bit of a hole.

The meanings of corresponding parameters are as follows:

  • -Xms: sets the memory occupied by the program when it is started
  • -Xmx: sets the maximum memory size that can be occupied during the running of the program
  • -xMN: Cenozoic size

To avoid frequent GC, set the minimum and maximum memory to 2G. Therefore, we need to modify the memory footprint according to our business situation.

Docker-based memory configuration

If the service is started based on Docker, it can also be adjusted in the script:

Version: '3' services: nacos: image: nacos/nacos-server:latest # "nacos/ nacOS-server :latest 'container_name: Run the following command to restart volumes: nacos_server # data volume mount path set to map the native directory to container directory - "/ opt/nacous nacos_docker/logs: / home/nacos/logs" - "/opt/nacous/nacos_docker/init.d/custom.properties:/home/nacos/init.d/custom.properties" environment: Set the environment variable, equivalent to -e-prefer_host_mode =hostname in the docker run command SPRING_DATASOURCE_PLATFORM= mysql-mysql_service_host =127.0.0.1 -mysql_service_db_name =nacos_config # The required SQL script is located in 'nacos-mysql/nacos-mysql. SQL' -mysql_service_port =3306 - MYSQL_SERVICE_USER=root - MYSQL_SERVICE_PASSWORD=123456 # JVM tuning parameters -jvm_xms =64m # -xms default :2g -jvm_xmx =64m # -xmx default :2g - JVM_XMN=16m #-Xmn default :1g - JVM_MS=8m #-XX:MetaspaceSize default :128m - JVM_MMS=8m # -xx :MaxMetaspaceSize default :320m -nacos_DEBUG =n The default value is n-tomcat_accessLOG_enabled. The default value is false. Ports: # Mapping port - "8848-8848" - "95555:9555" # mem_limit: 300m # Maximum memory usedCopy the code

summary

The memory tuning of Nacos is essentially the memory tuning of Spring Boot project. The size of parameters can be adjusted appropriately according to their own business needs, and further adjustment can be made according to the specific situation. When the service volume is relatively small, using 2G of memory directly is indeed very wasteful.

About the blogger: Author of the technology book SpringBoot Inside Technology, loves to delve into technology and writes technical articles.

Public account: “program new vision”, the blogger’s public account, welcome to follow ~

Technical exchange: Please contact the weibo user at Zhuan2quan