我搜索了教程,以便使用Gradle部署Spring引导应用程序。我找不到任何资源来解释这样做的过程。
有人能指导我这个过程吗?
当我的项目在我的机器上本地运行时,它的工作就像一种魅力。但是我想在Google应用引擎的灵活Java环境上进行部署。
谢谢,提前。
我的build.gradle看起来像这样
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
jwtVersion = '3.4.0'
appEngineVersion = '1.9.56'
appEngineGradleToolsVersion = '1.3.4'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile("org.springframework.boot:spring-boot-starter-security")
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'
implementation "com.auth0:java-jwt:${jwtVersion}"
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}发布于 2019-11-13 06:28:31
标准(Java 8)
本示例演示如何在Google上部署Spring应用程序。
有关更详细的说明,请参见Google标准-文档。有关使用mysql的操作,请参见将Cloud用于MySQL
设置
gcloud initgcloud app createMaven
在本地运行
mvn appengine:run
使用vist:http://localhost:8080/
部署
mvn appengine:deploy
使用vist:https://YOUR-PROJECT-ID.appspot.com
测试
mvn verify
在添加/修改源代码(src/main/java/...)时,向(src/main/test/...)添加单元测试非常有用。以下资源非常有用:
有关更多信息,请参阅Java应用引擎文档。
将Spring应用程序转换为App标准的步骤
使用战争包装
必须使用WAR打包将其部署到标准中。
如果从start.spring.io生成Spring项目,请确保切换到初始化程序站点的完整版本视图,并选择WAR打包。
如果您有一个现有的JAR打包项目,可以通过以下方式将其转换为WAR项目: 1.在pom.xml中,将<packaging>jar</packaging>更改为<packaging>war</packaging> 1。创建一个新的SpringBootServletInitializer实现:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(YourApplication.class);
}
}拆下Tomcat起动器
Google标准将您的WAR部署到Jetty服务器中。Spring Boot的启动程序默认包括Tomcat。这将引起冲突。排除Tomcat依赖关系:
<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依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>添加App标准插件
在pom.xml中,添加App标准插件:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>1.3.1</version>
</plugin>此插件用于运行本地开发服务器以及将应用程序部署到Google中。
添加App配置
添加一个src/main/webapp/WEB-INF/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>此配置是运行在Google中的应用程序所必需的。
不包括7月到SLF4J桥
Spring的默认日志记录桥与Jetty的日志记录系统冲突。为了能够捕获Spring启动日志,需要排除org.slf4j:jul-to-slf4j依赖项。最简单的方法是将依赖范围设置为provided,这样它就不会包含在WAR文件中:
<!-- Exclude any jul-to-slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<scope>provided</scope>
</dependency>内存不足错误
使用SpringBoot1.5.6,您可能会在启动时遇到内存不足的错误。请按照下列指示处理此问题:
.level = INFO<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties"/>
</system-properties>https://stackoverflow.com/questions/52042443
复制相似问题