startup.sh 4.7 KB

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