我试图在Google App Engine(标准环境)上部署Spring Boot应用程序。起初,我从这个不错的教程中克隆了示例应用程序 https://springframework.guru/spring-boot-web-application-part-4-spring-mvc/
例如,我调用了 http://localhost:8080/products 并显示了包含数据的模板。
所以一切都运行没有问题,我能够在本地调用所有控制器方法。然后我决定将其作为实验部署在 GAE 上。我根据这里的说明调整了pom.xml https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/appengine-standard-java8/springboot-appengine-standard
这意味着我排除了Tomcat的依赖,将包装从jar更改为war,创建了appengine-web.xml文件等。下一步,我在 GAE 控制台中创建了 GAE 项目,并将 APP ID 复制到 appengine-web.xml 中。然后我运行了mvn干净的包,并在目标文件夹中创建了战争。最后,我开始了 GAE 部署,它也很顺利,没有出现错误。
我的应用现在部署在此 URL 上 https://20180109t135551-dot-oe-gae-test.appspot.com/
如果您尝试一下,您将在浏览器中看到Hello World。但是,如果我尝试像这样调用/products 控制器方法 https://20180109t135551-dot-oe-gae-test.appspot.com/products 我会收到"找不到"错误。
你能给我建议我应该调用我的控制器方法的 URL 吗?我忘了实现像web.xml servlet映射这样的东西吗?还是一些特定的Spring Boot - Google App Engine问题?
我将不胜感激任何提示。
提前谢谢大家
执行此步骤将转换为以下代码:
-
在pom.xml中,将
<packaging>jar</packaging>
更改为<packaging>war</packaging>
-
在包
guru.springframework
中添加此类:
法典:
package guru.springframework;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
}
- 删除雄猫启动器:
在 POM 中找到此依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
并添加以下行:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
排除 Jetty 依赖项并包含 Servlet API 依赖项:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency>
添加 App Engine 标准插件:
<plugin> <groupId>com.google.cloud.tools</groupId> <artifactId>appengine-maven-plugin</artifactId> <version>1.3.1</version> </plugin>
- 在
src/webapp/WEB-INF
中添加一个名为appengine-web.xml
的文件,其中包含以下内容:
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <threadsafe>true</threadsafe> <runtime>java8</runtime> </system-properties> </appengine-web-app>
- 通过在pom中找到此依赖项来排除JUL到SLF4J桥 :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
- 在
并以此方式修改它:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
- 避免内存不足错误:
在src/main/resources
中添加一个 logging.properties 文件,其中包含:
.level = INFO
并在里面粘贴src/main/webapp/WEB-INF/appengine-web.xml
这个:
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties" />
</system-properties>
编辑:
对于步骤3
和7
,您还可以转到项目资源管理器(如果您使用的是 Eclipse)并导航到库 -> Maven 依赖项并单独选择每个库(在我的情况下jul-to-slf4j-1.7.25
和spring-boot-starter-tomcat-1.5.3.RELEASE
)。右键单击每个库并转到Maven -> 排除 Maven 工件...然后单击确定。这将对 POM 产生与编辑相同的效果。