首页 > 图灵资讯 > 技术篇>正文

在 Koyeb 上部署 Spring Boot 应用程序

2024-09-04 19:42:09

在 koyeb 上部署 spring boot 应用程序

介绍

当我们开始建立作为开发人员的东西时,我们的主要目标之一就是与他人分享我们创造的东西。归功于前端开发人员,这通常很简单 vercel 和 netlify 其他优秀的托管服务,无缝支持前端应用。然而,展示我们的工作对后端开发人员来说可能更具挑战性。我们构建 api,尽管使用数据库 json 也许它很强大,但它在视觉上不如使用它好 css 或 lottie 构建的动画如此引人注目。

这就是为什么找到一种高效可靠的方法来部署后端应用程序至关重要。 spring boot 它是一种流行的基础 java 框架简化了构建生产就绪应用程序的过程,但部署它们仍然是一个挑战。这就是 koyeb 用武之地。koyeb 它提供了一个强大且易于使用的平台,允许开发人员以最少的设置快速部署他们的平台 spring boot 应用程序。在本指南中,我们将引导您完成 koyeb 上部署带有 postgresql 数据库的 spring boot 从开始到结束,整个应用过程。

1. 前提条件

在我们深入部署之前,请确保您具备以下条件:

  • 一个基本的 spring boot 应用程序。 如果没有,可以用 spring initializr 快速生成一个具有以下依赖性的项目:
    • spring web
    • spring data jpa
    • postgresql 驱动程序
  • github(或 gitlab/bitbucket)存储库,托管您的 spring boot 项目。
  • neon 帐户。 在 neon 注册
  • 一个 koyeb 帐户。假如你没有,请在这里 koyeb 网站注册。
  • 已安装 maven 或 gradle,这取决于你的 spring boot 项目配置模式。
2. 设置数据库
  • 在 koyeb 上,实例化是免费的 postgresql 它将提供数据库 url 但每月仅限 50 小时。

-在 neon 上,实例化一个 免费 postgresql 数据库还将提供数据库 url。

3.使用springng boot连接数据库

您将在资源目录中创建一个名称 env.properties 在本例中,存储所有环境变量的文件是 db_url、db_username 和 db_password。

切勿提交把这个文件提交给你 github 存储库。

env.properties 文件:

db_username=<get this from the neon or koyeb dashbord>
db_password=<get this from the neon or koyeb dashbord>
db_url=<get this from the neon or koyeb dashbord></get></get></get>

您的 application.properties 文件:

application.properties文件:

server.port=${port:8080}

spring.datasource.url=${db_url}
spring.datasource.username=${db_username}
spring.datasource.password=${db_password}

spring.jpa.hibernate.ddl-auto=update

spring.config.import=classpath:env.properties

解释
  • server.port - 这是您的应用程序将运行的端口。我们设置了环境变量 port,如果 port 为空,则回退为 8080。
  • spring.datasource.url - 这是来自 neon 或 koyeb 仪表板的环境变量。
  • spring.datasource.username - 这是来自 neon 或 koyeb 仪表板的环境变量。
  • spring.datasource.password - 这是来自 neon 或 koyeb 仪表板的环境变量。
  • spring.config.import - 这样就可以导入你存储敏感数据的文件。
4.创建system.properties文件

在项目的根目录下创建一个 system.properties 文件。

指定使用本文件 java 运行时的版本,以便 koyeb java 构建包使用正确版本执行项目。

记住:koyeb 接受主要版本值 1.8、11、13、15、17、19 和 20。

如果Java版本没有指定,版本1.8将被使用。

我在用java 21.如果您使用其他版本,请相应更改。

系统.属性

java.runtime.version=21

5. 创建控制器

该控制器将在这里 / 路线上显示 hello world 消息。

package com.example.demo.Modules.User.controller;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class UserController {

    @GetMapping("/")
    public String helloWorld() {
        return "Hello World";
    }

}

6.将代码推送到github

在 github 创建公共存储库并推送您的代码。获取此存储库 url。

7. 在 koyeb 上部署
  • 输入您的 koyeb 帐户。
  • 转到服务 > web 服务 > 使用 github 创建 web 服务。
  • 连接到 github 或粘贴公共存储库 url。
  • 等待项目建设。
  • 如果成功,它将产生一个公众 url,每个人都可以访问你 spring boot 应用程序。
结论

就是这样!你已经创造了一个 spring boot 应用程序将其连接到云数据库,并部署所有内容 koyeb 上面。这可以是你想在作品集中展示的项目的开始,让客户看到你能做什么。

您可以使用图像上传器文章来添加此应用程序。

谢谢你的阅读!

?参考
  • koyeb - spring boot 部署
  • koyeb - java 参考
?跟我说话
  • 领英
  • github
  • 投资组合

以上就是在 Koyeb 上部署 Spring Boot 详情请关注图灵教育的其他相关文章!

上一篇 如何设计可定制的 Java 函数?
下一篇 返回列表

文章素材均来源于网络,如有侵权,请联系管理员删除。