好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解Spring Boot 项目部署到heroku爬坑

​ 背景:最近小组进行一个环境比较恶劣的项目,由于没有真实的测试环境,决定上云,最终选择国外的 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

查看更多关于详解Spring Boot 项目部署到heroku爬坑的详细内容...

  阅读:42次