startup.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better!"
  45. fi
  46. fi
  47. export SERVER="nacos-server"
  48. export MODE="cluster"
  49. export FUNCTION_MODE="all"
  50. export MEMBER_LIST=""
  51. export EMBEDDED_STORAGE=""
  52. while getopts ":m:f:s:c:p:" opt
  53. do
  54. case $opt in
  55. m)
  56. MODE=$OPTARG;;
  57. f)
  58. FUNCTION_MODE=$OPTARG;;
  59. s)
  60. SERVER=$OPTARG;;
  61. c)
  62. MEMBER_LIST=$OPTARG;;
  63. p)
  64. EMBEDDED_STORAGE=$OPTARG;;
  65. ?)
  66. echo "Unknown parameter"
  67. exit 1;;
  68. esac
  69. done
  70. export JAVA_HOME
  71. export JAVA="$JAVA_HOME/bin/java"
  72. export BASE_DIR=`cd $(dirname $0)/..; pwd`
  73. export CUSTOM_SEARCH_LOCATIONS=file:${BASE_DIR}/conf/
  74. #===========================================================================================
  75. # JVM Configuration
  76. #===========================================================================================
  77. if [[ "${MODE}" == "standalone" ]]; then
  78. JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
  79. JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
  80. else
  81. if [[ "${EMBEDDED_STORAGE}" == "embedded" ]]; then
  82. JAVA_OPT="${JAVA_OPT} -DembeddedStorage=true"
  83. fi
  84. JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
  85. JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
  86. JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
  87. fi
  88. if [[ "${FUNCTION_MODE}" == "config" ]]; then
  89. JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config"
  90. elif [[ "${FUNCTION_MODE}" == "naming" ]]; then
  91. JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming"
  92. fi
  93. JAVA_OPT="${JAVA_OPT} -Dnacos.member.list=${MEMBER_LIST}"
  94. JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
  95. if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then
  96. JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/nacos_gc.log:time,tags:filecount=10,filesize=102400"
  97. else
  98. JAVA_OPT_EXT_FIX="-Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext"
  99. 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"
  100. fi
  101. JAVA_OPT="${JAVA_OPT} -Dloader.path=${BASE_DIR}/plugins/health,${BASE_DIR}/plugins/cmdb,${BASE_DIR}/plugins/selector"
  102. JAVA_OPT="${JAVA_OPT} -Dnacos.home=${BASE_DIR}"
  103. JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/target/${SERVER}.jar"
  104. JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
  105. JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${CUSTOM_SEARCH_LOCATIONS}"
  106. JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"
  107. JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"
  108. if [ ! -d "${BASE_DIR}/logs" ]; then
  109. mkdir ${BASE_DIR}/logs
  110. fi
  111. echo "$JAVA $JAVA_OPT_EXT_FIX ${JAVA_OPT}"
  112. if [[ "${MODE}" == "standalone" ]]; then
  113. echo "nacos is starting with standalone"
  114. else
  115. echo "nacos is starting with cluster"
  116. fi
  117. # check the start.out log output file
  118. if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
  119. touch "${BASE_DIR}/logs/start.out"
  120. fi
  121. # start
  122. echo "$JAVA $JAVA_OPT_EXT_FIX ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
  123. if [[ "$JAVA_OPT_EXT_FIX" == "" ]]; then
  124. nohup "$JAVA" ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
  125. else
  126. nohup "$JAVA" "$JAVA_OPT_EXT_FIX" ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
  127. fi
  128. echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"