1. 程式人生 > >Heroku中的java伺服器部署-官網教程翻譯

Heroku中的java伺服器部署-官網教程翻譯

Heroku 可以提供免費的web應用伺服器分佈伺服器(並不只限於此),如下記錄瞭如何將自己的應用程式提交到heroku的過程。原英文連結 https://devcenter.heroku.com/articles/git

Heroku是一個利用 Git 來管理App 程式分佈的應用。你不需要懂太多 Git 的知識,但是需要了解一個基本的 Git 概念。

預備安裝: Git 和 heroku 命令列工具

用 Git 管理你的專案

在執行和部署應用程式之前,你需要初始化本地的 Git 倉庫,並且將程式碼提交到本地倉庫中。 下面的例子將在目錄 myapp 下部署專案

$ cd myapp
$ git init
Initialized empty Git repository in .git/
$ git add .
$ git commit -m "My first commit"
Created initial commit 5df2d09: My first commit
 44 files changed, 8393 insertions(
+), 0 deletions(-) create mode 100644 README create mode 100644 Procfile create mode 100644 app/controllers/source_file ...

創一個 Heroku 遠端專案

上面將專案程式碼提交到了本地專案,但是沒有和任何的遠端倉庫進行關聯。如下操作進行遠端倉庫的關聯。關聯的時候分為如下幾種情況。

  1. 對於一個新的 Heroku 應用

在專案的根目錄(myapp)下,直接使用 heroku create 命令會生成 Heroku 應用,並且Heroku 會自動將剛剛初始化的本地 git 倉庫與遠端的 Heroku 倉庫關聯起來。此時,Heroku 會自動生成一個專案名thawing-inlet-61413

。如果想指定名稱,可以在 heroku create APPNAME。前提是 APPNAME 不能和任何其他人的專案重名。

$ heroku create
Creating app... done, ⬢ thawing-inlet-61413
https://thawing-inlet-61413.herokuapp.com/ | https://git.heroku.com/thawing-inlet-61413.git

記住上面的兩個地址,一個是你的專案訪問地址,一個是你的遠端倉庫地址

可以使用 git remote -v 檢視遠端倉庫中是否已經有一個名為 heroku 的倉庫已經為你的APP 部署而被建立。

$ git remote -v
heroku  https://git.heroku.com/thawing-inlet-61413.git (fetch)
heroku  https://git.heroku.com/thawing-inlet-61413.git (push)
  1. 對於一個已經存在的 Heroku 應用

如果是你已經存在了 Heroku 專案,你只是想把原生代碼倉庫和原先的遠端倉庫關聯起來。使用如下命令(前提是知道 Heroku APP 的名字,比如 “thawing-inlet-61413”)

$ heroku git:remote -a thawing-inlet-61413
set git remote heroku to https://git.heroku.com/thawing-inlet-61413.git

重新命名遠端倉庫名稱

通常情況下, Heroku CLI 會命名所有的 Heroku app 的遠端倉庫分支名為 heroku。 可以使用 git remote rename 命令來修改遠端倉庫名

$ git remote rename heroku heroku-staging

如果你有多個 Heroku 應用使用同一份程式碼庫,比如開發環境和生成壞境。你重新命名 Heroku 倉庫名,將會顯得更加便捷。

後續的命令都是在遠端倉庫分支名為 heroku 情況下進行。

部署程式碼

為了部署你的 APP 到 Heroku,最常用的方法就是使用 git push 命令將程式碼有本地倉庫的 master 分支,推送到遠端的 heroku 倉庫。

$ git push heroku master
Initializing repository, done.
updating 'refs/heads/master'
...

無論何時,你想提交最新的編寫的程式碼到 Heroku,都可以使用該命令。

值得注意的是,這個命令只會影響到 master 分支。

如果想體驗下如何在 Heroku 中部署專案,可以使用官方提供的例子進行 Java 專案部署,參考例子程式碼 https://devcenter.heroku.com/articles/getting-started-with-java#deploy-the-app

國內網路不好,需要自己準備梯子,可以去 GitHub 上搜索 xx-net。 只能說這麼多了