简体   繁体   中英

Unable to access Spring Boot application in Google App Engine after deployment

I am new to Google App Engine.

I have been trying to deploy an exercise to Google Cloud.

This is the project in case you want to see the configuration in detail:

Exercise Project to Deploy

This project is made with Java 8 and Spring Boot.

This is the pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <!-- dependency versions -->
    <com.google.guava.guava.version>21.0</com.google.guava.guava.version>
    <io.springfox.springfox-swagger2>2.3.1</io.springfox.springfox-swagger2>
    <io.springfox.springfox-swagger-ui>2.3.1</io.springfox.springfox-swagger-ui>
    <commons-lang.version>3.0</commons-lang.version>
    <junit.version>4.12</junit.version>
    <jsonpath.version>2.2.0</jsonpath.version>
</properties>

<dependencies>

    <!-- For default Spring Boot utilities -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <!-- For testing possibility -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- For all mvc and web functions -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>

    <!-- Default persistence functions -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
        <version>5.0.5.Final</version>
    </dependency>

    <!-- In-Memory DB -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- Java Utilities from google -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${com.google.guava.guava.version}</version>
    </dependency>

    <!-- Include swagger for API description -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${io.springfox.springfox-swagger2}</version>
    </dependency>

    <!-- Include swagger for API description UI -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${io.springfox.springfox-swagger-ui}</version>
    </dependency>

    <!-- Commons-lang3 -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>${commons-lang.version}</version>
    </dependency>

    <!-- Unit testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.jayway.jsonpath</groupId>
        <artifactId>json-path</artifactId>
        <version>${jsonpath.version}</version>
        <scope>test</scope>
    </dependency>

    <!-- Google cloud -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.5.7.RELEASE</version>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.3.2</version>
        </plugin>
    </plugins>
</build>

This is the app.yaml

runtime: java
env: flex

runtime_config:  # Optional
  jdk: openjdk8

handlers:
- url: /.*
  script: this field is required, but ignored

manual_scaling:
  instances: 1

This is the appengine-web.xml

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <version>1</version>
    <threadsafe>true</threadsafe>
    <runtime>java8</runtime>
</appengine-web-app>

I did not provide a web.xml

I was able to deploy this with the command:

mvn appengine:run

This is the console ouptut. I seems to have deployed successfully:

[INFO] GCLOUD: 2017-12-09 16:48:39.319:INFO::main: Logging initialized @501ms
[INFO] GCLOUD: 2017-12-09 16:48:39.867:INFO:oejs.Server:main: jetty-9.3.18.v20170406
[INFO] GCLOUD: 2017-12-09 16:48:45.673:INFO:oeja.AnnotationConfiguration:main: Scanning elapsed time=5286ms
[INFO] GCLOUD: Dec 09, 2017 7:48:45 PM com.google.appengine.tools.development.ApiProxyLocalImpl log
[INFO] GCLOUD: INFO: javax.servlet.ServletContext log: 1 Spring WebApplicationInitializers detected on classpath
[INFO] GCLOUD: 2017-12-09 16:48:46.541:INFO:oejsh.ContextHandler:main: Started c.g.a.t.d.j.DevAppEngineWebAppContext@7bd7d6d6{/,[file:///home/sebastiandagostino/mutant-api-exercise/target/mutant-exercise-1.0-SNAPS
HOT/, jar:file:///home/sebastiandagostino/mutant-api-exercise/target/mutant-exercise-1.0-SNAPSHOT/WEB-INF/lib/springfox-swagger-ui-2.3.1.jar!/META-INF/resources],AVAILABLE}{/home/sebastiandagostino/mutant-api-exer
cise/target/mutant-exercise-1.0-SNAPSHOT}
[INFO] GCLOUD: 2017-12-09 16:48:46.558:INFO:oejs.AbstractConnector:main: Started NetworkTrafficSelectChannelConnector@4722ef0c{HTTP/1.1,[http/1.1]}{localhost:8080}
[INFO] GCLOUD: 2017-12-09 16:48:46.561:INFO:oejs.Server:main: Started @7745ms
[INFO] GCLOUD: Dec 09, 2017 7:48:46 PM com.google.appengine.tools.development.AbstractModule startup
[INFO] GCLOUD: INFO: Module instance default is running at http://localhost:8080/
[INFO] GCLOUD: Dec 09, 2017 7:48:46 PM com.google.appengine.tools.development.AbstractModule startup
[INFO] GCLOUD: INFO: The admin console is running at http://localhost:8080/_ah/admin
[INFO] GCLOUD: Dec 09, 2017 4:48:46 PM com.google.appengine.tools.development.DevAppServerImpl doStart
[INFO] GCLOUD: INFO: Dev App Server is now running

When I try to reach the URL from the preview, I get Error 403 .

Then I run:

gcloud app deploy

In order to make that gcloud command to work, I copied the app.yaml to the target folder.

I get 404 Not Found . I even checked the logs:

Waiting for new log entries...
2017-12-09 20:09:52 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:13 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:15 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:16 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:16 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:16 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:16 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:16 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:17 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:17 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:17 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:17 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:19 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:23 default[20171209t170858]  "GET /swagger HTTP/1.1" 404
2017-12-09 20:10:38 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:10:44 default[20171209t170858]  "GET / HTTP/1.1" 404
2017-12-09 20:11:22 default[20171209t170858]  "GET / HTTP/1.1" 404

So, the app compiles and deploys... but somewhat there is some configuration problem. I have been looking and still have no clue.

Here states that I need to add the following class:

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MutantApplication.class);
    }

}

I did that, but it fails compiling then:

[INFO] GCLOUD: 20:37:33.990 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment - Replacing PropertySource 'servletContextInitParams' with 'servletContextInitParams'
[INFO] GCLOUD: 20:37:34.056 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []
[INFO] GCLOUD: 20:37:34.057 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
[INFO] GCLOUD: 20:37:34.057 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/C:/Users/Sebastian/workspace/mutant-exercise/target/mutant-exercise-1.0-SNAPSHOT/WEB-INF/classes/]
[INFO] GCLOUD: java.lang.IllegalArgumentException: Unable to find the main class to restart
[INFO] GCLOUD:  at org.springframework.util.Assert.notNull(Assert.java:134)
[INFO] GCLOUD:  at org.springframework.boot.devtools.restart.Restarter.doStart(Restarter.java:285)
[INFO] GCLOUD:  at org.springframework.boot.devtools.restart.Restarter.start(Restarter.java:273)
[INFO] GCLOUD:  at org.springframework.boot.devtools.restart.Restarter$1.call(Restarter.java:174)
[INFO] GCLOUD:  at org.springframework.boot.devtools.restart.Restarter$1.call(Restarter.java:170)
[INFO] GCLOUD:  at org.springframework.boot.devtools.restart.Restarter$LeakSafeThread.run(Restarter.java:627)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 21.114 s
[INFO] Finished at: 2017-12-09T17:37:34-03:00
[INFO] Final Memory: 39M/263M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.google.cloud.tools:appengine-maven-plugin:1.3.2:run (default-cli) on project mutant-exercise: Execution default-cli of goal com.google.cloud.tools:appengine-maven-plugin:1.3.2:run failed: Non zero exit: 1 -> [Help 1]

It is weird. It says that it can't find the main class.

Deploy it in App Engine standard following these steps:

Clone the git repo https://github.com/sebastiandagostino/mutant-api-exercise.git

Download and install STS for your OS: https://spring.io/tools/sts/all

Once it is installed and running. Click on 'help -> Eclipse Marketplace' and search for the Google Cloud plugin. Install it.

Install the Google Cloud SDK (I used linux instructions in my case) https://cloud.google.com/sdk/downloads#interactive

Install the app-engine-java component , see here and then here

After installing the components and authenticating In Google Cloud Platform. In Eclipse, click 'window -> preferences' and click on Google Cloud Tools (these are the preferences for the Google Cloud plugin). In SDK location select the folder where you installed the SDK. See image below.

在此处输入图片说明

Now you're ready to import the project with sts:

Select 'new..project: Google App Engine Standard Project'.

在此处输入图片说明

Click next. Uncheck 'Use default location' so you can select the folder that contains the git project you downloaded.

After importing. Right click on the project name and go to 'configure -> Convert to maven project'

After maven solves all the imports, right click on the project name again and select 'Deploy to app engine standard'. A window will open where you'll have to validate your account and choose a Google Cloud project.

After deployment finishes you shoud be able to go to https://your-project-id.appspot.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM