首先创建一个空的 Spring Boot 项目,修改启动类SpringBootHerokuApplication如下
package io.arukas;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class SpringBootHerokuApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootHerokuApplication.class, args);
}
@GetMapping("")
public String welcome() {
return "hello heroku";
}
}
接下来就可以通过git将代码推送到 Heroku 部署了,前提需要安装 Heroku 客户端,可参考官方文档安装https://devcenter.heroku.com/articles/heroku-cli
登录
$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/browser/c037d3e6-1359-446c-b26a-8589d07de256
heroku: Waiting for login... |
Heroku 客户会打开默认的浏览器,提示登录确认,确认后如下
$ heroku login
heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/browser/c037d3e6-1359-446c-b26a-8589d07de256
Logging in... done
Logged in as xxx@gmail.com
初始化仓库提交到本地
$ git init
Initialized empty Git repository in E:/2019/0000/spring-boot-heroku/.git/
$ git add
........
$ git commit -am "hello heroku"
[master (root-commit) 1885978] hello heroku
10 files changed, 353 insertions(+)
create mode 100644 .gitignore
create mode 100644 build.gradle
create mode 100644 gradle/wrapper/gradle-wrapper.jar
create mode 100644 gradle/wrapper/gradle-wrapper.properties
create mode 100644 gradlew
create mode 100644 gradlew.bat
create mode 100644 settings.gradle
create mode 100644 src/main/java/io/arukas/SpringBootHerokuApplication.java
create mode 100644 src/main/resources/application.properties
create mode 100644 src/test/java/io/arukas/SpringBootHerokuApplicationTests.java
创建应用
$ heroku create spring-boot-heroku
Creating ⬢ spring-boot-heroku... !
! Name spring-boot-heroku is already taken
## 如上提示名称已存在,换个名称再试
$ heroku create spring-boot-heroku-gradle
Creating ⬢ spring-boot-heroku-gradle... done
https://spring-boot-heroku-gradle.herokuapp.com/ | https://git.heroku.com/spring-boot-heroku-gradle.git
推送到远程仓库
创建应用时,heroku 已经为我们添加了远程地址 https://git.heroku.com/spring-boot-heroku-gradle.git ,下面开始推送代码构建项目
$ git push heroku master
Enumerating objects: 24, done.
Counting objects: 100% (24/24), done.
Delta compression using up to 8 threads
Compressing objects: 100% (16/16), done.
Writing objects: 100% (24/24), 54.37 KiB | 6.04 MiB/s, done.
Total 24 (delta 0), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Gradle app detected
remote: -----> Spring Boot detected
remote: -----> Installing JDK 1.8... done
remote: -----> Building Gradle app...
remote: -----> executing ./gradlew build -x test
remote: Downloading https://services.gradle.org/distributions/gradle-6.0.1-bin.zip
remote: ..........................................................................................
remote: > Task :compileJava
remote: > Task :processResources
remote: > Task :classes
remote: > Task :bootJar
remote: > Task :jar SKIPPED
remote: > Task :assemble
remote: > Task :check
remote: > Task :build
remote:
remote: BUILD SUCCESSFUL in 35s
remote: 3 actionable tasks: 3 executed
remote: -----> Discovering process types
remote: Procfile declares types -> (none)
remote: Default types for buildpack -> web
remote:
remote: -----> Compressing...
remote: Done: 65.7M
remote: -----> Launching...
remote: Released v3
remote: https://spring-boot-heroku-gradle.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/spring-boot-heroku-gradle.git
* [new branch] master -> master
出现上面的信息,我们的应用就部署成功了,下面可以打开浏览器试试了,访问地址:https://spring-boot-heroku-gradle.herokuapp.com
项目源码: https://github.com/heroku-sample/spring-boot-heroku-gradle
来源:CSDN
作者:FDtom
链接:https://blog.csdn.net/FDtom/article/details/103738623