Kamis, 31 Januari 2019

Deploy java API as service in linux CentOS 6


For sample i use uereka microservices

  1. Create user for running API

    # useradd api
    # passwd api

  2. login as api create directory eureka for put jar file, copy jar file to /home/api/eureka

    # mkdir eureka

  3. Create script wrapper for system V init in /home/api/eureka


    #!/bin/sh
    ### BEGIN INIT INFO
    # chkconfig: 345 98 01
    # description: Startup Script for Eureka API
    # Provides:          Eureka
    ### END INIT INFO

    SCRIPT="nohup java -jar -DSpring.profiles.active=cloud1 /home/api/eureka/eureka-service.jar"
    RUNAS=api

    PIDFILE=/home/api/eureka/eureka-api.pid
    LOGFILE=/home/api/eureka/eureka-api.log

    start() {
      if [ -f $PIDFILE ] && [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE); then
        echo 'Eureka already running' >&2
        return 1
      fi
      cd /home/api/eureka
      echo 'Starting service… Eureka' >&2
      local CMD="$SCRIPT &> \"$LOGFILE\" & echo \$!"
      su -c "$CMD" $RUNAS > "$PIDFILE"
        
      # Try with this command line instead of above if not workable
      # su -s /bin/sh $RUNAS -c "$CMD" > "$PIDFILE"
      # or
      # nohup java -jar -DSpring.profiles.active=cloud1 /home/api/eureka/eureka.jar &> /home/api/eureka/eureka-api.log & echo $! > $PIDFILE

      PID=$(cat $PIDFILE)
        if pgrep -u $RUNAS -f eureka-service.jar > /dev/null
        then
          echo "API Eureka is now running, the PID is $PID"
        else
          echo ''
          echo "Error! Could not start Eureka !"
        fi
    }
      
    stop() {
      if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
        echo 'Service Eureka not running' >&2
        return 1
      fi
      echo 'Stopping service…' >&2
      kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
      echo 'Service Eureka stopped' >&2
    }
      
    uninstall() {
      echo -n "Are you really sure you want to uninstall this service? That cannot be undone. [yes|No] "
      local SURE
      read SURE
      if [ "$SURE" = "yes" ]; then
        stop
        rm -f "$PIDFILE"
        echo "Notice: log file was not removed: $LOGFILE" >&2
        update-rc.d -f eureka remove
        rm -fv "$0"
      fi
    }
      
    status() {
        printf "%-50s" "Checking Eureka ..."
        if [ -f $PIDFILE ] && [ -s $PIDFILE ]; then
           PID=$(cat $PIDFILE)
                if [ -z "$(ps axf | grep ${PID} | grep -v grep)" ]; then
                    printf "%s\n" "The process appears to be dead but pidfile still exists"
                else  
                    echo "Running, the PID is $PID"
                fi
        else
            printf "%s\n" "Service Eureka not running"
        fi
    }
      
      
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      status)
        status
        ;;
      uninstall)
        uninstall
        ;;
      restart)
        stop
        start
        ;;
      *)
        echo "Usage: $0 {start|stop|status|restart|uninstall}"
    esac
      

  4. Enable service in system V and start eureka

    # sudo ln -s /home/api/eureka/eureka /etc/init.d/eureka
    # sudo chkconfig --add eureka
    # sudo chkconfig eureka on
    # sudo service eureka start

  5. Firewall

    # iptables -I INPUT -p tcp --destination-port 8761 -j ACCEPT
    # service iptables save

Tidak ada komentar: