seata-server.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/bin/bash
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one or more
  4. # contributor license agreements. See the NOTICE file distributed with
  5. # this work for additional information regarding copyright ownership.
  6. # The ASF licenses this file to You under the Apache License, Version 2.0
  7. # (the "License"); you may not use this file except in compliance with
  8. # the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # resolve links - $0 may be a softlink
  19. PRG="$0"
  20. while [ -h "$PRG" ]; do
  21. ls=`ls -ld "$PRG"`
  22. link=`expr "$ls" : '.*-> \(.*\)$'`
  23. if expr "$link" : '/.*' > /dev/null; then
  24. PRG="$link"
  25. else
  26. PRG=`dirname "$PRG"`/"$link"
  27. fi
  28. done
  29. PRGDIR=`dirname "$PRG"`
  30. BASEDIR=`cd "$PRGDIR/.." >/dev/null; pwd`
  31. BASEDIR=${BASEDIR//"//"/"/"}
  32. . ${BASEDIR}/bin/seata-setup.sh
  33. JAVA_OPT="${JAVA_OPT} -Dspring.config.additional-location=${BASEDIR}/conf/ -Dspring.config.location=${BASEDIR}/conf/application.yml -Dlogging.config=${BASEDIR}/conf/logback-spring.xml -Dproduction.deploy.output=true"
  34. JAVA_OPT="${JAVA_OPT} -jar ${BASEDIR}/target/seata-server.jar"
  35. CMD_LINE_ARGS=$@
  36. NEW_ARGS=$(echo "${CMD_LINE_ARGS}" | sed -e 's/^start//g' -e 's/^restart//g' -e 's/^ //g')
  37. show_usage() {
  38. echo " Usage: sh seata-server.sh(for linux and mac) or cmd seata-server.bat(for"
  39. echo " windows) [options]"
  40. echo " Options:"
  41. echo " --host, -h"
  42. echo " The ip to register to registry center."
  43. echo " --port, -p"
  44. echo " The port to listen."
  45. echo " Default: 0"
  46. echo " --storeMode, -m"
  47. echo " log store mode : file, db, redis"
  48. echo " --serverNode, -n"
  49. echo " server node id, such as 1, 2, 3.it will be generated according to the"
  50. echo " snowflake by default"
  51. echo " --seataEnv, -e"
  52. echo " The name used for multi-configuration isolation."
  53. echo " --sessionStoreMode, -ssm"
  54. echo " session log store mode : file, db, redis"
  55. echo " --lockStoreMode, -lsm"
  56. echo " lock log store mode : file, db, redis"
  57. echo " --help"
  58. }
  59. # start
  60. function validate_host() {
  61. local host=$1
  62. local re_ip="^([0-9]{1,3}\.){3}[0-9]{1,3}$"
  63. if [[ ! $host =~ $re_ip ]]; then
  64. echo "Invalid host: $host"
  65. show_usage
  66. exit 1
  67. fi
  68. }
  69. function validate_port() {
  70. local port="$1"
  71. if ! [[ "$port" =~ ^[0-9]+$ ]]; then
  72. echo "Error: Invalid port: $port"
  73. show_usage
  74. exit 1
  75. fi
  76. return 0
  77. }
  78. function validate_mode() {
  79. local mode="$1"
  80. if [[ "$mode" != 'file' && "$mode" != 'db' && "$mode" != 'redis' ]]; then
  81. echo "Error: Invalid storeMode: $mode"
  82. show_usage
  83. exit 1
  84. fi
  85. return 0
  86. }
  87. function validate_serverNode() {
  88. local serverNode="$1"
  89. if ! [[ "$serverNode" =~ ^[0-9]+$ ]]; then
  90. echo "Error: Invalid serverNode: $serverNode"
  91. show_usage
  92. exit 1
  93. fi
  94. return 0
  95. }
  96. while [[ $# -gt 0 ]]; do
  97. key="$1"
  98. case "$key" in
  99. start|stop|restart)
  100. ;;
  101. -h|--host)
  102. if [[ -n "$2" ]]; then
  103. validate_host "$2"
  104. shift
  105. else
  106. echo "Error: Host value is required"
  107. show_usage
  108. exit 1
  109. fi
  110. ;;
  111. -p|--port)
  112. if [[ -n "$2" ]]; then
  113. validate_port "$2"
  114. shift
  115. else
  116. echo "Error: Port value is required"
  117. show_usage
  118. exit 1
  119. fi
  120. ;;
  121. -m|--storeMode)
  122. if [[ -n "$2" ]]; then
  123. validate_mode "$2"
  124. shift
  125. else
  126. echo "Error: storeMode value is required"
  127. show_usage
  128. exit 1
  129. fi
  130. ;;
  131. -ssm|--sessionStoreMode)
  132. if [[ -n "$2" ]]; then
  133. validate_mode "$2"
  134. shift
  135. else
  136. echo "Error: sessionStoreMode value is required"
  137. show_usage
  138. exit 1
  139. fi
  140. ;;
  141. -lsm|--lockStoreMode)
  142. if [[ -n "$2" ]]; then
  143. validate_mode "$2"
  144. shift
  145. else
  146. echo "Error: lockStoreMode value is required"
  147. show_usage
  148. exit 1
  149. fi
  150. ;;
  151. -e|--seataEnv)
  152. if [[ -n "$2" ]]; then
  153. shift
  154. else
  155. echo "Error: seataEnv value is required"
  156. show_usage
  157. exit 1
  158. fi
  159. ;;
  160. -n|--serverNode)
  161. if [[ -n "$2" ]]; then
  162. validate_serverNode "$2"
  163. shift
  164. else
  165. echo "Error: serverNode value is required"
  166. show_usage
  167. exit 1
  168. fi
  169. ;;
  170. --help)
  171. show_usage
  172. exit 1
  173. ;;
  174. *)
  175. echo "Error: Unknown argument: $key"
  176. show_usage
  177. exit 1
  178. ;;
  179. esac
  180. shift
  181. done
  182. function start_server() {
  183. echo "$JAVACMD ${JAVA_OPT} ${NEW_ARGS} &"
  184. nohup $JAVACMD ${JAVA_OPT} ${NEW_ARGS} &
  185. echo "seata-server is starting, you can check the ${LOG_HOME}/ *.log"
  186. }
  187. function stop_server() {
  188. PID=`ps aux | grep -i 'seata-server' | grep java | grep -v grep | awk '{print $2}'`
  189. if [ -z "$PID" ]; then
  190. echo "No seata-server running."
  191. exit 1;
  192. fi
  193. echo "The seata-server(${PID}) is running..."
  194. kill ${PID}
  195. sleep 4
  196. echo "Send shutdown request to seata-server(${PID}) OK"
  197. }
  198. function replace_old_arg() {
  199. local old_arg="$1"
  200. local new_arg="$2"
  201. for i in "${!OLD_ARGS_ARRAY[@]}"
  202. do
  203. if [[ "${OLD_ARGS_ARRAY[$i]}" == "$old_arg" ]]; then
  204. OLD_ARGS_ARRAY[$i]="$new_arg"
  205. found=1
  206. for j in $(seq $((i+1)) "${#OLD_ARGS_ARRAY[@]}")
  207. do
  208. if [[ "${OLD_ARGS_ARRAY[$j]}" == "$old_arg" ]]; then
  209. unset OLD_ARGS_ARRAY[$j]
  210. else
  211. break
  212. fi
  213. done
  214. if [[ "$i+1" -lt "${#OLD_ARGS_ARRAY[@]}" && "${OLD_ARGS_ARRAY[$i+1]}" != -* ]]; then
  215. OLD_ARGS_ARRAY[$i+1]="${new_arg_value# }"
  216. fi
  217. break
  218. fi
  219. done
  220. }
  221. function restart_server() {
  222. PID=`ps aux | grep -i 'seata-server' | grep java | grep -v grep | awk '{print $2}'`
  223. #filtered
  224. OLD_ARGS=`ps -p $PID -o args= | grep -v "^$0" | sed -E 's/.*seata-server.jar(.*)/\1/g'`
  225. #Unfiltered
  226. #OLD_ARGS=`ps -p $PID -o args= | grep -v "^$0"`
  227. #echo "previous parameters ${OLD_ARGS}"
  228. IFS=' ' read -r -a OLD_ARGS_ARRAY <<< "${OLD_ARGS}"
  229. IFS=' ' read -r -a NEW_ARGS_ARRAY <<< "${NEW_ARGS}"
  230. for new_arg in "${NEW_ARGS_ARRAY[@]}"
  231. do
  232. found=0
  233. if [[ "$new_arg" == "-p" || "$new_arg" == "--port" ]]; then
  234. replace_old_arg "-p" "$new_arg"
  235. elif [[ "$new_arg" == "-h" || "$new_arg" == "--host" ]]; then
  236. replace_old_arg "-h" "$new_arg"
  237. elif [[ "$new_arg" == "-m" || "$new_arg" == "--storeMode" ]]; then
  238. replace_old_arg "-m" "$new_arg"
  239. elif [[ "$new_arg" == "-n" || "$new_arg" == "--serverNode" ]]; then
  240. replace_old_arg "-n" "$new_arg"
  241. elif [[ "$new_arg" == "-e" || "$new_arg" == "--seataEnv" ]]; then
  242. replace_old_arg "-e" "$new_arg"
  243. elif [[ "$new_arg" == "-ssm" || "$new_arg" == "--sessionStoreMode" ]]; then
  244. replace_old_arg "-ssm" "$new_arg"
  245. elif [[ "$new_arg" == "-lsm" || "$new_arg" == "--lockStoreMode" ]]; then
  246. replace_old_arg "-lsm" "$new_arg"
  247. fi
  248. if [[ "$found" == 0 ]]; then
  249. OLD_ARGS_ARRAY+=("$new_arg")
  250. fi
  251. done
  252. NEW_ARGS=$(printf "%s " "${OLD_ARGS_ARRAY[@]}")
  253. stop_server
  254. echo "The seata-server restarting..."
  255. start_server
  256. }
  257. if [ -z "${CMD_LINE_ARGS}" ]; then
  258. start_server
  259. else
  260. case "${CMD_LINE_ARGS}" in
  261. start*)
  262. start_server
  263. ;;
  264. stop*)
  265. stop_server
  266. ;;
  267. restart*)
  268. restart_server
  269. ;;
  270. *)
  271. start_server
  272. ;;
  273. esac
  274. fi