首页  >  文章  >  后端开发  >  在 AWS 上使用 Docker 构建 Go 应用程序:创建用于添加和检索项目的 RESTful 接口

在 AWS 上使用 Docker 构建 Go 应用程序:创建用于添加和检索项目的 RESTful 接口

Mary-Kate Olsen
Mary-Kate Olsen原创
2024-10-28 07:51:02186浏览

简介

虽然大多数 Go 应用程序都编译成单个二进制文件,但 Web 应用程序还附带模板、资产和配置文件;这些可能会不同步并导致错误的部署。
Docker 允许我们创建一个独立的镜像,其中包含应用程序运行所需的一切。在本文中,我们将学习如何使用安装在实例上的 Docker 来部署 Go Web 应用程序,以及 Docker 如何帮助您改进开发工作流程和部署过程。

我们需要的步骤如下:

- 启动一个实例(您的机器)来构建 Docker
申请

- 在实例中安装 Docker
- 在实例中安装 Go
- 为您的 Go 应用程序创建代码文件
- 应用程序测试

启动一个实例(您的机器)来构建 Docker
申请

您可以找到与文章中描述的实例启动和连接相同的步骤:

https://dev.to/zahraajawad/building-a-jupyter-notebook-environment-in-docker-for-data-analysis-on-aws-ec2-376i

注意: 确保选择安全组:

  • SSH 端口 22: 使用 SSH 访问并连接到实例
    远程管理系统的协议。

  • HTTP 端口 8080: 要在此端口 (8080) 上运行 Go 应用程序以从 Internet 或本地网络访问它,必须打开此端口。

- 在我们的实例中安装 Docker

我们将创建的具体工作流架构使用 Docker 来提供集成的工作流环境。
因此通过 SSH 连接到实例并获得 root 权限后,使用以下命令自动化安装 Docker:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" && sudo apt-get update && apt-缓存策略 docker-ce

Docker 体验:运行一个简单的测试命令 docker -v 来检查 Docker 是否正常工作并查看 Docker 版本:

安装 Go

您可以从Go官方网站下载安装Go https://go.dev/dl/
wget https://golang.org/dl/go1.20.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >>> ~/.bash_profile
源 ~/.bash_profile

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
哪里:
wget https://golang.org/dl/go1.20.linux-amd64.tar.gz 是下载Go二进制文件。


sudo tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz 是将 tarball 解压到 /usr/local。

echo 'export PATH=$PATH:/usr/local/go/bin' >>> ~/.bash_profile 更新 PATH 环境变量。
和 source ~/.bash_profile 以应用对配置文件所做的更改

执行命令并通过命令 ls 验证执行后,显示下载的文件:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
使用以下代码初始化 Go 应用程序:
go mod init my-go-app
Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

现在我们需要通过命令创建一个项目文件夹:
mkdir
然后通过命令更改当前目录:
cd
所以执行是:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

为您的 Go 应用程序创建代码文件

main.go 文件

我们创建一个名为 main.go 的新文件,其中包含以下函数和代码,我们将详细解释这些函数和代码,然后将所有代码放入 main.go 文件中:

  • 要导入必要的包,我们使用以下代码:
import (
    "encoding/json"
    "log"
    "net/http"
    "github.com/gorilla/mux"
    "os"
)
  • 对于数据结构项:
type Item struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}

其中item是一个包含标识符(ID)和名称(Name)的数据结构。这些字段使用标签转换为 JSON 格式(json:"id" 和 json:"name".

  • 项目变量
var items []Item

这是存储在服务器内存中的项目片段。

  • 通过 main 函数,除了引导检索或添加新元素以及显示简单 HTML 页面的各种请求之外,还通过读取端口(此处为端口 8080)来排列结构。
import (
    "encoding/json"
    "log"
    "net/http"
    "github.com/gorilla/mux"
    "os"
)
  • 函数 getItems 返回 JSON 格式的项目列表。标头中的内容类型设置为 application/json。
type Item struct {
    ID   int    `json:"id"`
    Name string `json:"name"`
}
  • createItem 函数将一个新项目添加到项目列表中。数据以 JSON 格式从请求正文中读取,根据现有项目的数量为项目分配 ID,添加的项目以 JSON 形式返回。
var items []Item
  • serveHome 函数显示一个简单的 HTML 页面,其中包含欢迎消息(欢迎使用 Go 应用程序)以及用于访问数据的链接。
func main() {
    port := os.Getenv("PORT")
    if port == "" {
        port = "8080"
    }

    router := mux.NewRouter()
    router.HandleFunc("/items", getItems).Methods("GET")
    router.HandleFunc("/items", createItem).Methods("POST")
    router.HandleFunc("/", serveHome).Methods("GET")

    log.Printf("Server is running on port %s...\n", port)
    log.Fatal(http.ListenAndServe(":"+port, router))
}

所以整个main.go文件是:

func getItems(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(items)
}

现在通过命令vim或nano创建main.go文件并将上面的代码放入文件中,这里我们将使用命令nano:

nano main.go

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
并通过代码:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

然后通过键盘ctrl x退出文件,然后按y(保存文件)然后单击输入

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Dockerfile:

是一个文本文档,其中包含用户可以在命令行上调用以组装图像的所有命令。
Dockerfile 可以通过读取 Dockerfile 中的指令来自动构建镜像。

创建 Dockerfile:

使用 Docker 构建容器映像需要包含构建指令的 Dockerfile。
我们创建一个 Dockerfile,并通过命令 nano Dockerfile 以与之前相同的方式添加以下代码:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Dockerfile 命令详细信息可以在 docker 文档主页上找到 https://docs.docker.com/guides/golang/build-images/

现在我们已经准备好了 Dockerfile,是时候为 Go 应用程序构建 Docker 镜像了。该镜像可以由官方 Docker 镜像制作而成,它们是:

docker build -t my-go-app .

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items
镜像构建成功,使用命令确认构建:
码头工人图像

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

然后,为了在构建镜像后运行容器,我们使用:

docker run -p 8080:8080 my-go-app

其中 8080 是 Web 服务器的端口,因此执行运行是:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

应用测试

- 通过curl命令测试Go应用程序

通过curl命令测试Go应用程序是否正常工作:

curl http://localhost:8080/items

curl http://:8080/items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

执行为null,这意味着应用程序正在运行,但我们还没有数据。

要添加项目,请使用以下命令:

curl -X POST -H "Content-Type: application/json" -d '{"name": "item"}' http://localhost:8080/items

curl -X POST -H "Content-Type: application/json" -d '{"name": "item"}' http://:8080/items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

所以执行添加:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

我们可以添加另一个项目:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

- 通过网页测试 Go 应用

通过网页测试Go应用是否正常运行,步骤如下:

  • 返回实例并通过复选框选择它。
  • 转到详细信息并复制公共 IPv4 地址。
  • 将端口为 8080 的公共 IPv4 地址粘贴到浏览器中,然后按 输入。

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

网页正在运行,当我们按下页面上的items时,我们会获得通过curl命令添加的项目。

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

或者可以按漂亮打印的复选框:

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

Building a Go Application with Docker on AWS: Creating a RESTful Interface for Adding and Retrieving Items

参考文献:

  • https://dev.to/zahraajawad/building-a-jupyter-notebook-environment-in-docker-for-data-analysis-on-aws-ec2-376i
  • https://semaphoreci.com/community/tutorials/how-to-deploy-a-go-web-application-with-docker
  • https://hub.docker.com/_/golang
  • https://docs.docker.com/guides/golang/build-images/
  • https://github.com/gorilla/mux

以上是在 AWS 上使用 Docker 构建 Go 应用程序:创建用于添加和检索项目的 RESTful 接口的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn