Use render.com to deploy springboot projects for free

Recently, I want to use the free platform to deploy the springboot project. After screening, the following service providers are available:

  1. heroku
  2. fly.io
  3. render.com

Here are some comparisons:

platform support limit
heroku.com most support Need to bind a credit card, it seems that there is no free time
fly.io Node、Python、Laravel、Go、Ruby 、Docker Up to 3 shared cpu-1 * 256mb virtual machine, 3GB persistent volume storage, 160GB external traffic
render.com Node、Python、Go、Ruby、PHP、Docker The bandwidth is 100 GB, the build time is 500 minutes/month, and 5 applications after successful build are reserved for rollback

heroku is a relatively old brand, it was used in 2016, and a free database can also be used in it

fly.io looks good, but it must use the client to actively release the version, and render.com can import the projects on github, and realize automatic deployment after submission, which is the most convenient, so the final choice is render.com

Register and import projects

After opening render.com, log in with your GitHub account, and you can import the project after authorization.

There are quite a few supported languages:

I feel that there are really many platforms that Node.js can choose

Create a new springboot project

From the above figure, we can see that render itself does not support java projects, so Docker must be used.

First go to start.spring.io/ to generate the simplest spring boot project:

After downloading, it can be successfully run locally, and the interface can be accessed normally. The next step is to deploy it on the render

Make a docker image

First, let's sort out the steps that we want render.com to perform during deployment:

  1. Pull the source code from GitHub
  2. Use the specified gradle to build and generate .jarthe file
  3. Execute java -jar /app.jarrun project

我本身对 docker 不熟悉,经过了一些曲折,最终完成了上面的步骤,成功跑起来了

#
# Build stage
#
FROM gradle:jdk17-jammy AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build --no-daemon

LABEL org.name="hezf"
#
# Package stage
#
FROM eclipse-temurin:17-jdk-jammy
COPY --from=build /home/gradle/src/build/libs/docker-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

这个例子源码在 GitHub

环境变量

很多时候我们后端项目需要链接数据库,但是这些信息肯定是需要隐藏起来的,千万不要传到 GitHub 上

我在filess 新建了个 MySQL 实例,在本地使用 navicat 成功连上了,当然了这个数据库也是免费的

首先我们在 springboot 项目根目录,新建个 .env 文件,然后在.gitignore把这个文件名添加进去

### VS Code ###
.vscode/

.env

这样这个文件就不会上传到 GitHub 了,然后在.env填写上一步获取到的数据库相关信息

DB_HOST=gmp.zh.filess.io
DB_PORT=3307
DB_USER=authdb_replacedie
DB_PASSWORD=1f1fdfe1a0220bf5e822ef1a8d583b762d
DB=authdb_replacedie

配合项目配置文件 application.properties


spring.config.import=optional:file:.env[.properties] 

DB_HOST: ${DB_HOST}

这样就可以正常的读取环境变量了

  @Value("${DB_HOST}")
  private String dbHost;

以上的内容都是针对本地的环境,因为.env没有上传到 GitHub ,那么在 render.com 怎么读取?

在 render.com 打开项目,找到 Environment ,点击 Add Secret File 按钮,左侧写文件名 .env 右侧填写上内容保存好就可以了

image.png

这样在项目 build 的时候就可以读取到 .env 这个 secret 文件,这就实现了本地和部署环境的一致。而且虽然是开源项目,但是没有暴露关键信息。

Guess you like

Origin juejin.im/post/7261599630827683896