IT박스

프로덕션 환경에서 스프링 부트 실행 가능 jar를 어떻게 실행합니까?

itboxs 2020. 9. 4. 07:03
반응형

프로덕션 환경에서 스프링 부트 실행 가능 jar를 어떻게 실행합니까?


Spring Boot의 선호하는 배포 방법은 내부에 tomcat이 포함 된 실행 가능한 jar 파일을 사용하는 것입니다.

간단한 java -jar myapp.jar.

이제 EC2의 Linux 서버에 해당 jar를 배포하고 싶습니다. 뭔가 누락되었거나 애플리케이션을 데몬으로 올바르게 시작하기 위해 init 스크립트를 만들어야합니까?

단순히 전화 java -jar하면 응용 프로그램이 로그 아웃 할 때 죽습니다.

화면이나 nohup에서 시작할 수는 있지만 그다지 우아하지 않으며 서버를 다시 시작하면 로그인하고 프로세스를 수동으로 시작해야합니다.

그래서, 봄 부팅에서 이미 작업에 대한 것이 있습니까?


Spring Boot 1.3.0.M1 이후 Maven 및 Gradle을 사용하여 완전히 실행 가능한 jar를 빌드 할 수 있습니다.

Maven의 경우 다음을 포함하십시오 pom.xml.

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

Gradle의 경우 다음 스 니펫을에 추가하십시오 build.gradle.

springBoot {
    executable = true
}

완전히 실행 가능한 jar에는 파일 앞에 추가 스크립트가 포함되어 있으므로 Spring Boot jar를 스크립트에 심볼릭 링크 init.d하거나 사용할 수 systemd있습니다.

init.d 예:

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

이를 통해 다음과 같이 애플리케이션을 시작, 중지 및 다시 시작할 수 있습니다.

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

또는 systemd스크립트를 사용하십시오 .

[Unit]
Description=yourapp
After=syslog.target

[Service]
ExecStart=/var/yourapp/yourapp.jar
User=yourapp
WorkingDirectory=/var/yourapp
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

다음 링크에서 추가 정보 :


프로덕션에서 Spring Boot 애플리케이션을 실행하는 가장 쉽고 안정적인 방법은 Docker를 사용하는 것입니다. 여러 연결된 서비스를 사용해야하는 경우 Docker Compose, Docker Swarm 또는 Kubernetes를 사용하십시오.

다음은 시작하는 데 도움 Dockerfile이되는 공식 Spring Boot Docker 가이드 의 간단한 내용 입니다 .

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" ]

다음은 컨테이너를 데몬으로 실행하는 샘플 명령 줄입니다.

docker run \
  -d --restart=always \
  -e "SPRING_PROFILES_ACTIVE=prod" \
  -p 8080:8080 \
  prefix/imagename

내 Spring 부팅 응용 프로그램에는 두 개의 이니셜 라이저가 있습니다. 하나는 개발 용이고 다른 하나는 생산 용입니다. 개발을 위해 다음과 같은 주요 방법을 사용합니다.

@SpringBootApplication
public class MyAppInitializer {

    public static void main(String[] args) {
        SpringApplication.run(MyAppInitializer .class, args);
    }

}

프로덕션 환경을위한 My Initializer는 SpringBootServletInitializer를 확장 하며 다음과 같습니다.

@SpringBootApplication
public class MyAppInitializerServlet extends SpringBootServletInitializer{
    private static final Logger log = Logger
            .getLogger(SpringBootServletInitializer.class);
    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        log.trace("Initializing the application");
        return builder.sources(MyAppInitializerServlet .class);
    }

}

나는 gradle을 사용하고 내 build.gradle 파일은 ' WAR '플러그인을 적용 합니다. 개발 환경에서 실행할 때 부트 런 태스크를 사용 합니다. 프로덕션에 배포하고 싶을 때 어셈블 작업을 사용하여 WAR을 생성하고 배포합니다.

I can run like a normal spring application in production without discounting the advantages provided by the inbuilt tomcat while developing. Hope this helps.


In a production environment you want your app to be started again on a machine restart etc, creating a /etc/init.d/ script and linking to the appropriate runlevel to start and stop it is the correct approach. Spring Boot will not extend to covering this as it is a operating system specific setup and the are tonnes of other options, do you want it running in a chroot jail, does it need to stop / start before some other software etc.


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 .


If you are using gradle you can just add this to your build.gradle

springBoot {
    executable = true
}

You can then run your application by typing ./your-app.jar

Also, you can find a complete guide here to set up your app as a service

56.1.1 Installation as an init.d service (System V)

http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

cheers


On Windows OS without Service.

start.bat

@ECHO OFF
call run.bat start

stop.bat:

@ECHO OFF
call run.bat stop

run.bat

@ECHO OFF
IF "%1"=="start" (
    ECHO start myapp
    start "myapp" java -jar -Dspring.profiles.active=staging myapp.jar
) ELSE IF "%1"=="stop" (
    ECHO stop myapp
    TASKKILL /FI "WINDOWTITLE eq myapp"
) ELSE (
    ECHO please, use "run.bat start" or "run.bat stop"
)
pause

I start applications that I want to run persistently or at least semi-permanently via screen -dmS NAME /path/to/script. As far as I am informed this is the most elegant solution.


This is a simple, you can use spring boot maven plugin to finish your code deploy.

the plugin config like:

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${debug.port}
                    </jvmArguments>
                    <profiles>
                        <profile>test</profile>
                    </profiles>
                    <executable>true</executable>
                </configuration>
            </plugin>

And, the jvmArtuments is add for you jvm. profiles will choose a profile to start your app. executable can make your app driectly run.

and if you add mvnw to your project, or you have a maven enveriment. You can just call./mvnw spring-boot:run for mvnw or mvn spring-boot:run for maven.

참고URL : https://stackoverflow.com/questions/22886083/how-do-i-run-a-spring-boot-executable-jar-in-a-production-environment

반응형