How do I run a spring boot executable jar in a Production environment?

后端 未结 9 2112
一向
一向 2020-12-02 04:19

Spring boot\'s preferred deployment method is via a executable jar file which contains tomcat inside.

It is started with a simple java -jar myapp.jar.

相关标签:
9条回答
  • 2020-12-02 05:02

    Please note that since Spring Boot 1.3.0.M1, you are able to build fully executable jars using Maven and Gradle.

    For Maven, just include the following in your pom.xml:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <executable>true</executable>
        </configuration>
    </plugin>
    

    For Gradle add the following snippet to your build.gradle:

    springBoot {
        executable = true
    }
    

    The fully executable jar contains an extra script at the front of the file, which allows you to just symlink your Spring Boot jar to init.d or use a systemd script.

    init.d example:

    $ln -s /var/yourapp/yourapp.jar /etc/init.d/yourapp
    

    This allows you to start, stop and restart your application like:

    $/etc/init.d/yourapp start|stop|restart
    

    Or use a systemd script:

    [Unit]
    Description=yourapp
    After=syslog.target
    
    [Service]
    ExecStart=/var/yourapp/yourapp.jar
    User=yourapp
    WorkingDirectory=/var/yourapp
    SuccessExitStatus=143
    
    [Install]
    WantedBy=multi-user.target
    

    More information at the following links:

    • Installation as an init.d service
    • Installation as a systemd service
    0 讨论(0)
  • 2020-12-02 05:06

    By far the most easiest and reliable way to run Spring Boot applications in production is with Docker. Use Docker Compose, Docker Swarm or Kubernetes if you need to use multiple connected services.

    Here's a simple Dockerfile from the official Spring Boot Docker guide to get you started:

    FROM frolvlad/alpine-oraclejdk8:slim
    VOLUME /tmp
    ADD YOUR-APP-NAME.jar app.jar
    RUN sh -c 'touch /app.jar'
    ENV JAVA_OPTS=""
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
    

    Here's a sample command line to run the container as a daemon:

    docker run \
      -d --restart=always \
      -e "SPRING_PROFILES_ACTIVE=prod" \
      -p 8080:8080 \
      prefix/imagename
    
    0 讨论(0)
  • 2020-12-02 05:09

    You can use the application called Supervisor. In supervisor config you can define multiple services and ways to execute the same.

    For Java and Spring boot applications the command would be java -jar springbootapp.jar.

    Options can be provided to keep the application running always.So if the EC2 restart then Supervisor will restart you application

    I found Supervisor easy to use compared to putting startup scripts in /etc/init.d/.The startup scripts would hang or go into waiting state in case of errors .

    0 讨论(0)
提交回复
热议问题