startup.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/bin/sh
  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 DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
  74. export CUSTOM_SEARCH_LOCATIONS=${DEFAULT_SEARCH_LOCATIONS},file:${BASE_DIR}/conf/
  75. #===========================================================================================
  76. # JVM Configuration
  77. #===========================================================================================
  78. if [[ "${MODE}" == "standalone" ]]; then
  79. JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m"
  80. JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
  81. else
  82. if [[ "${EMBEDDED_STORAGE}" == "embedded" ]]; then
  83. JAVA_OPT="${JAVA_OPT} -DembeddedStorage=true"
  84. fi
  85. JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
  86. JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
  87. JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
  88. fi
  89. if [[ "${FUNCTION_MODE}" == "config" ]]; then
  90. JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config"
  91. elif [[ "${FUNCTION_MODE}" == "naming" ]]; then
  92. JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming"
  93. fi
  94. JAVA_OPT="${JAVA_OPT} -Dnacos.member.list=${MEMBER_LIST}"
  95. JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p')
  96. if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then
  97. JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/nacos_gc.log:time,tags:filecount=10,filesize=102400"
  98. else
  99. JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext"
  100. 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"
  101. fi
  102. JAVA_OPT="${JAVA_OPT} -Dloader.path=${BASE_DIR}/plugins/health,${BASE_DIR}/plugins/cmdb"
  103. JAVA_OPT="${JAVA_OPT} -Dnacos.home=${BASE_DIR}"
  104. JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/target/${SERVER}.jar"
  105. JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
  106. JAVA_OPT="${JAVA_OPT} --spring.config.location=${CUSTOM_SEARCH_LOCATIONS}"
  107. JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"
  108. JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"
  109. if [ ! -d "${BASE_DIR}/logs" ]; then
  110. mkdir ${BASE_DIR}/logs
  111. fi
  112. echo "$JAVA ${JAVA_OPT}"
  113. if [[ "${MODE}" == "standalone" ]]; then
  114. echo "nacos is starting with standalone"
  115. else
  116. echo "nacos is starting with cluster"
  117. fi
  118. # check the start.out log output file
  119. if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
  120. touch "${BASE_DIR}/logs/start.out"
  121. fi
  122. # start
  123. echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
  124. nohup $JAVA ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
  125. echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"