startup.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/bin/bash
  2. # Copyright 1999-2018 Alibaba Group Holding Ltd.
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. cygwin=false
  14. darwin=false
  15. os400=false
  16. case "`uname`" in
  17. CYGWIN*) cygwin=true;;
  18. Darwin*) darwin=true;;
  19. OS400*) os400=true;;
  20. esac
  21. error_exit ()
  22. {
  23. echo "ERROR: $1 !!"
  24. exit 1
  25. }
  26. [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java
  27. [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java
  28. [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/opt/taobao/java
  29. [ ! -e "$JAVA_HOME/bin/java" ] && unset JAVA_HOME
  30. if [ -z "$JAVA_HOME" ]; then
  31. if $darwin; then
  32. if [ -x '/usr/libexec/java_home' ] ; then
  33. export JAVA_HOME=`/usr/libexec/java_home`
  34. elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then
  35. export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"
  36. fi
  37. else
  38. JAVA_PATH=`dirname $(readlink -f $(which javac))`
  39. if [ "x$JAVA_PATH" != "x" ]; then
  40. export JAVA_HOME=`dirname $JAVA_PATH 2>/dev/null`
  41. fi
  42. fi
  43. if [ -z "$JAVA_HOME" ]; then
  44. JAVA_PATH=$(which java)
  45. if [ -z "$JAVA_PATH" ]; then
  46. error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better!"
  47. fi
  48. JAVA_HOME=$(dirname "$JAVA_PATH")/..
  49. export JAVA_HOME=$(cd "$JAVA_HOME" && pwd)
  50. fi
  51. fi
  52. export SERVER="nacos-server"
  53. export MODE="cluster"
  54. export FUNCTION_MODE="all"
  55. export MEMBER_LIST=""
  56. export EMBEDDED_STORAGE=""
  57. while getopts ":m:f:s:c:p:" opt
  58. do
  59. case $opt in
  60. m)
  61. MODE=$OPTARG;;
  62. f)
  63. FUNCTION_MODE=$OPTARG;;
  64. s)
  65. SERVER=$OPTARG;;
  66. c)
  67. MEMBER_LIST=$OPTARG;;
  68. p)
  69. EMBEDDED_STORAGE=$OPTARG;;
  70. ?)
  71. echo "Unknown parameter"
  72. exit 1;;
  73. esac
  74. done
  75. export JAVA_HOME
  76. export JAVA="$JAVA_HOME/bin/java"
  77. export BASE_DIR=`cd $(dirname $0)/..; pwd`
  78. export CUSTOM_SEARCH_LOCATIONS=file:${BASE_DIR}/conf/
  79. #===========================================================================================
  80. # JVM Configuration
  81. #===========================================================================================
  82. if [[ "${MODE}" == "standalone" ]]; then
  83. JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
  84. JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
  85. else
  86. if [[ "${EMBEDDED_STORAGE}" == "embedded" ]]; then
  87. JAVA_OPT="${JAVA_OPT} -DembeddedStorage=true"
  88. fi
  89. JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
  90. JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
  91. JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
  92. fi
  93. if [[ "${FUNCTION_MODE}" == "config" ]]; then
  94. JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config"
  95. elif [[ "${FUNCTION_MODE}" == "naming" ]]; then
  96. JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming"
  97. fi
  98. JAVA_OPT="${JAVA_OPT} -Dnacos.member.list=${MEMBER_LIST}"
  99. JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
  100. if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then
  101. JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/nacos_gc.log:time,tags:filecount=10,filesize=100m"
  102. else
  103. JAVA_OPT="${JAVA_OPT} -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8 "
  104. JAVA_OPT_EXT_FIX="-Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext"
  105. JAVA_OPT="${JAVA_OPT} -Xloggc:${BASE_DIR}/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
  106. fi
  107. JAVA_OPT="${JAVA_OPT} -Dloader.path=${BASE_DIR}/plugins,${BASE_DIR}/plugins/health,${BASE_DIR}/plugins/cmdb,${BASE_DIR}/plugins/selector"
  108. JAVA_OPT="${JAVA_OPT} -Dnacos.home=${BASE_DIR}"
  109. JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/target/${SERVER}.jar"
  110. JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
  111. JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${CUSTOM_SEARCH_LOCATIONS}"
  112. JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"
  113. JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"
  114. if [ ! -d "${BASE_DIR}/logs" ]; then
  115. mkdir ${BASE_DIR}/logs
  116. fi
  117. echo "$JAVA $JAVA_OPT_EXT_FIX ${JAVA_OPT}"
  118. if [[ "${MODE}" == "standalone" ]]; then
  119. echo "nacos is starting with standalone"
  120. else
  121. echo "nacos is starting with cluster"
  122. fi
  123. # check the start.out log output file
  124. if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
  125. touch "${BASE_DIR}/logs/start.out"
  126. fi
  127. # start
  128. echo "$JAVA $JAVA_OPT_EXT_FIX ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
  129. if [[ "$JAVA_OPT_EXT_FIX" == "" ]]; then
  130. nohup "$JAVA" ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
  131. else
  132. nohup "$JAVA" "$JAVA_OPT_EXT_FIX" ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
  133. fi
  134. echo "nacos is starting. you can check the ${BASE_DIR}/logs/start.out"