如何开始使用Spring Boot/Heroku

在Windows 10的64位系统上。

需要安装的软件

    1. 以管理員權限啟動命令提示字元。

 

    1. 從Win+X菜單中選擇。

 

    1. 安裝Chocolately。

 

    1. 套件管理系統。

 

    1. 執行https://chocolatey.org/install#installing-chocolatey 中「使用cmd.exe安裝」的命令。

 

    1. 更新環境變數,即使不重啟命令提示字元,也可以將環境變數更新為最新內容。

 

    1. 安裝Git、JDK、Maven、Spring Boot CLI、Heroku CLI。

 

    1. cinst -y git jdk8 maven spring-boot-cli heroku-cli && refreshenv

 

    關閉管理員權限的命令提示字元。

应用程序的创建和操作确认

    1. 命令提示符启动

 

    1. 创建和移动工作目录,例如 mkdir \herokustudy && cd \herokustudy

spring init –dependencies=web myfirstapp && cd myfirstapp

可以使用spring init –list命令查看可指定的依赖列表

git init && git add . && git commit -m “spring init直接后”

在后续部署到Heroku时,git是必需的

编辑src\main\java\com\example\myfirstapp\DemoApplication.java(如下所示)

保存为UTF-8编码(由于原文件仅包含ASCII字符,因此在添加日语时容易不小心保存为Windows-31J编码)

mvnw spring-boot:run

Maven的封装命令已准备在当前目录中
会进行必要的软件下载(仅在初次运行时)、编译和启动Tomcat

在浏览器中访问http://localhost:8080/
git add . && git commit -m “hello追加”

package com.example.myfirstapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*; // 追加
import org.springframework.stereotype.*;  // 追加

@Controller // 追加
@SpringBootApplication
public class DemoApplication {

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

    // 追加
    @RequestMapping("/")
    @ResponseBody
    String hello() {
        return "こんにちは";
    }
}

获取Heroku账户、部署和确认操作

    1. https://signup.heroku.com/ (在这里注册新账号)

不要关闭浏览器(将在heroku login之后使用)

heroku login(在之前的浏览器中进行登录)

可以通过参数指定应用程序名称(https://○○○.herokuapp.com/中的○○○部分),如果没有指定则由系统自动分配。可以通过heroku apps:list –json命令查看自己应用程序的详细列表。

git push heroku master(将应用程序部署至Heroku,包括下载必要软件(仅初次)、编译和启动Tomcat)

heroku open(浏览器将打开并访问应用程序的URL)

可以使用heroku logs –tail命令查看日志

附赠:设置Git的origin。

    1. 在我平时使用的Git服务器(GitBucket)上创建一个空的新存储库。

新存储库的URL将显示在结果屏幕上。

git remote add origin <新存储库的URL>
git push -u origin master

赠品:在Chocolately上安装SourceTree。

    1. 在管理者命令提示符下,使用Chocolately安装SourceTree → cinst -y sourcetree

将”C:\Program Files (x86)\Atlassian\Sourcetree\SourceTree.exe”固定到任务栏上

接下来

将Spring Boot/Heroku应用程序导入到VSCode中
将Spring Boot/Heroku应用程序导入到Eclipse中
将现有的Spring Boot应用程序(已经在Git上管理)部署到Heroku上

以上

bannerAds