? 背景:最近小組進行一個環境比較惡劣的項目,由于沒有真實的測試環境,決定上云,最終選擇國外的heroku,折騰半天,其中有一些坑在這里記錄下來,方便網友及個人。
1.賬號注冊
? heroku官網:https://www.heroku.com
? heroku免費注冊賬號,heroku提供的功能已經可以滿足大部分個人需求,有特殊需求的用戶就需要進行付費了,比如heroku的數據庫的免費空間只有5m,且項目在30分鐘內無人訪問就會休眠,下面是heroku對于休眠的說明:
by default, your app is deployed on a free dyno. free dynos will sleep after a half hour of inactivity (if they don't receive any traffic). this causes a delay of a few seconds for the first request upon waking. subsequent requests will perform normally. free dynos also consume from a monthly, account-level quota of free dyno hours - as long as the quota is not exhausted, all free apps can continue to run.to avoid dyno sleeping, you can upgrade to a hobby or professional dyno type as described in the dyno types article. for example, if you migrate your app to a professional dyno, you can easily scale it by running a command telling heroku to execute a specific number of dynos, each running your web process type.
? heroku的注冊界面:
ps:
- heroku的網站需要翻墻才能訪問,并且設置翻墻軟件的模式為全局模式。
- heroku貌似不接受中國有限注冊(country可以選擇中國區域),個人使用gmail注冊
2.安裝cli
? 簡單注冊完賬號以后在官網登陸個人賬號,點擊getting started,選擇一樣自己需要的語言,然后選擇合適自己系統的版本,下載安裝cli,本人為mac系統。
3.heroku基本操作
? 官網給了比較詳細的操作說明,這里就不一一贅述,大家可以跟著官方教程一步一步操作,這里只說一下個人實踐過程中遇到的問題,附送一些官網教程的截圖。
詳細教程請參見heroku官網
4.遇到的問題
? 上傳項目到heroku時,一般系統會自動幫你打包并運行你的項目,這里我遇到兩個問題:
git的個人分支無法上傳
項目無法啟動
下面是解決方法:
1.git個人分支無法上傳
? 官網上上傳項目給了一條指令:
1
|
$ git push heroku master |
? 然后會得到這樣一個運行日志:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
initializing repository, done. counting objects: 110 , done. delta compression using up to 4 threads. compressing objects: 100 % ( 87 / 87 ), done. writing objects: 100 % ( 110 / 110 ), 212.71 kib | 0 bytes/s, done. total 110 (delta 30 ), reused 0 (delta 0 ) -----> java app detected -----> installing openjdk 1.8 ... done -----> installing maven 3.3 . 3 ... done -----> executing: mvn -b -dskiptests= true clean install [info] scanning for projects... ... [info] ------------------------------------------------------------------------ [info] build success [info] ------------------------------------------------------------------------ [info] total time: 11 .417s [info] finished at: thu sep 11 17 : 16 : 38 utc 2014 [info] final memory: 21m/649m [info] ------------------------------------------------------------------------ -----> discovering process types procfile declares types -> web |
但是實際項目中,我是在自己的分支上開發,然后我用git上傳自己的分支:
1
|
$ git push heroku xxx |
? 運行結果
total 0 (delta 0), reused 0 (delta 0)
remote: pushed to non-master branch, skipping build.
to https://git.heroku.com/certberus.git
f2c01f2..40aa59d xxx -> xxx
這樣顯然是不對的,最后發現上傳分支需要這樣輸入:
1
|
$ git push heroku xxx:master |
這樣你的分支修改的內容就會合并到mater上進行上傳,然后運行了。
2.項目無法啟動
? 通常maven項目在打包時,會被打成war包或者jar包,熟悉spring boot的童鞋應該了解spring boot的運行命令,其實heroku運行項目也非常簡單。
? 首先說一下正常的一個文件的spring boot部署到heroku,需要在根目錄添加一個procfile文件,告訴heroku你要打包哪個文件,文件內容如下:
1
|
web java -dserver.port=$port $java_opts -jar target/*.jar |
但是本人的項目為多個子項目打包,啟動類在子項目中,這樣如何來讓heroku啟動呢,自己不停的修改procfile中的文件路徑仍然不起作用,后來發現heroku中有一個很爽的命令,如下:
1
|
$ heroku run bash |
? 這樣就相當于遠程登錄一臺linux服務器啦,我們可以使用linux命令查看自己部署在heroku上的項目的目錄結構啦,找到需要運行的jar包,將其在云端的路徑修改到procfile文件中,再次上傳項目,就會發現項目跑起來了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/c68a77c3051e