How to deploy Spring bootloader on Amazon Elastic Beanstalk?

I am very new to AWS and Spring Framework.

I built a simple web program after the getting started guide on the Spring website. In addition, I have already created the Elastic Beanstalk application and the environment in which Tomcat 8 and Java 8 work. I'm trying to do this,

  • create this simple program using Gradle and create an executable jar (or war).
  • Download this executable jar (or war) from the download and deploy button on the Elastic Beanstalk toolbar.

Now I use IntelliJ, and my program works well if I double-clicked the released executable jar. However, after I loaded this jar into my elastic beanstalk, this program simply causes an HTTP 404 error.

I don’t understand if I use the Gradle 'build' or 'bootRun' task, the created jar executable works fine. However, if I create a war file using the Gradle 'war' task and put it in the local Tomcat, the server will throw an HTTP 404 error, the same as AWS.

This is my build.gradle file.

    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
        classpath("org.apache.tomcat:tomcat-catalina:8.0.28")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'

jar {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

war {
    baseName = 'gs-authenticating-ldap'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}

In addition, my program has only two Java classes: src/main/java/hello/HomeController.javaandsrc/main/java/hello/Application.java

src/main/java/hello/HomeController.java:

package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String index() {
        return "Welcome to the home page!";
    }
}

src/main/java/hello/Application.java:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}

As a summary:

  • build 'bootRun' → browsing "localhost: 8080": works fine.
  • build 'build' → 'xxx.jar( )' → , jar → "localhost: 8080" :
  • build 'build' → 'xxx.jar( )' → AWS → "xx.elasticbeanstalk.com": HTTP 404
  • build 'war' → 'xxx.war' → Tomcat IntelliJ → "localhost: 8080" : HTTP 404
  • build 'war' → 'xxx.war' → AWS → "xx.elasticbeanstalk.com": HTTP 404

, , - 1) IntelliJ, 2) Gradle, 3) 4) AWS . , .

- , , . !

+4
2

, , Application SpringBootServletInitializer:

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

}

Spring Frameworks Servlet (Tomcat ).

Gradle, ​​ . :

dependencies {
    // …
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    // …
}

(build war) .

, Spring : https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

+6

Source: https://habr.com/ru/post/1615948/


All Articles