天蓝色函数使用docker compose表示本地运行天蓝色函数

Every time I join a new project, I try not to rely too much on external environments when building and running the software that I’m working on. Most of the time, a DEV or CI environment is overrated and unstable. My approach is to run all the components locally and remove the external dependencies.

每次我加入一个新项目时,在构建和运行正在使用的软件时,我都尽量不要过多地依赖外部环境。 大多数情况下,DEV或CI环境被高估且不稳定。 我的方法是在本地运行所有组件,并删除外部依赖项。

I already wrote about running a local SQL Express Docker instance, you will find this article to have the same merit:

我已经写过关于运行本地SQL Express Docker实例的文章,您会发现本文具有相同的优点:

If it were up to me, I’d write everything in Azure Functions, not everything fits the model, though. Maarten Balliauw explains this in more detail here: https://blog.maartenballiauw.be/post/2019/10/02/dont-use-azure-functions-as-a-web-application.html

如果要由我决定,我会在Azure Functions中编写所有内容,但是并不是所有内容都适合该模型。 Maarten Balliauw在这里更详细地解释了这一点: https : //blog.maartenballiauw.be/post/2019/10/02/dont-use-azure-functions-as-a-web-application.html

However, when I do develop an Azure Function, I like to run it locally first, without the interference of anything hosted in Azure or elsewhere. The fastest way of having such an experience, in my opinion, is using Docker and Docker Compose.

但是,当我开发Azure功能时,我喜欢先在本地运行它, 而不会受到Azure或其他地方托管的任何内容的干扰 。 我认为,获得这种体验的最快方法是使用DockerDocker Compose

Image for post

The term ‘ Docker’ is glorified by many and horrified by some, I consider Docker to be just another tool in my toolbelt, and a great one at that.

术语“ Docker”被许多人所荣耀,而被某些人所恐惧,我认为Docker只是我的工具带中的另一种工具,在这方面是一个很棒的工具。

I think the reason I like Docker so much, is the versatility of the tool:

我认为我非常喜欢Docker的原因是该工具的多功能性:

Do you have an ASP.NET Core Web Application? Docker.

您是否有ASP.NET Core Web应用程序 ? 码头工人

How about an SQL Database? Docker.

SQL数据库怎么样? 码头工人

Angular Frontend Application? Docker.

角前端应用程序 ? 码头工人

An executable that you’d likely run via a Windows Service? Docker.

您可能通过Windows服务运行的可执行文件 ? 码头工人

Database Migrations? Docker.

数据库迁移 ? 码头工人

Azure Storage Emulator? Docker.

Azure存储模拟器 ? 码头工人

Your entire CI/CD Jenkins Pipeline? Docker.

您的整个CI / CD Jenkins管道 ? 码头工人

Docker和Docker-Compose的本地Azure功能 (Local Azure functions with Docker and Docker-Compose)

Everything I’ll mention here is compressed inside of the accompanied git repo below:

我在这里提到的所有内容都压缩在下面随附的git repo中:

I’ve created a template C# Azure Functions project for you to scaffold that holds the following features:

我已经创建了一个模板C#Azure Functions项目,供您使用具有以下功能的支架:

  • An HTTP Triggered function

    HTTP触发功能
  • A Blob Triggered function with Blob output binding connected to a local storage account

    Blob触发功能,其中Blob输出绑定已连接到本地存储帐户
  • A Queue Triggered function connected to a local queue

    连接到本地队列的队列触发功能
  • An easy “start and stop” way of hosting this function locally

    在本地托管此功能的简便“启动和停止”方式

What you will need to do first in order to get started:

首先,您需要做的是:

docker-compose up

First, you will see the Azurite image being pulled from Docker.

首先,您将看到从Docker中提取了Azurite映像。

Image for post

Next, you will see the Local.Functions project being built and containerized.

接下来,您将看到正在构建和容器化的Local.Functions项目。

Image for post

Finally, when both containers are ready, they are spun up inside of one network using their respective names:

最后,当两个容器都准备就绪时,它们将使用各自的名称在一个网络内旋转:

Image for post

Now, you can open up the Storage Explorer and browse the local.storage.emulator’s Blob storage and Queues:

现在,您可以打开存储资源管理器并浏览local.storage.emulator的Blob存储和队列:

Image for post

准备环境 (Preparing the environment)

Create two containers named; input-container and output-container.

创建两个名为的容器; 输入容器输出容器

Create a queue named; queue.

创建一个名为的队列; 排队

使用队列 (Working with the Queue)

Add a new message to the queue via the Azure Storage Explorer

通过Azure存储资源管理器向队列添加新消息

Image for post

In a few moments, you’ll see the message getting picked up by the local.functions container.

稍后,您会看到local.functions容器收到了该消息。

Image for post

使用Blob容器 (Working with the Blob Containers)

Add any file to the input-container by simply dragging and dropping through the Aure Storage Explorer. The function will pick this up and copy the file to the output-container.

只需通过Aure Storage Explorer拖放即可将任何文件添加到输入容器 。 该函数将对此进行拾取并将文件复制到输出容器。

Image for post

测试Http端点 (Testing the Http endpoint)

Open the http folder from the cloned repo in VS Code.

从VS Code中的克隆存储库中打开http文件夹。

Open the HttpTriggerGet.http file and click the ‘Send Request’ context item above the GET request line.

打开HttpTriggerGet.http文件,然后单击GET请求行上方的“ Send Request”上下文项。

Image for post

The Http function should send a response as a result:

Http函数应发送响应结果:

Image for post

调试 (Debugging)

Once you’ve run the docker-compose command, you should have a local container named local.storage.emulator.

运行docker-compose命令后,应该有一个名为local.storage.emulator的本地容器。

Image for post

To debug the Local.Functions application, you simply need to press F5 from inside VS Code. The launch task will start the local.storage.emulator container to ensure the local storage emulation is running. Then you can put a breakpoint in the QueueTriggeredFunction to inspect the message being sent:

要调试Local.Functions应用程序,只需从VS Code内部按F5。 启动任务将启动local.storage.emulator容器,以确保本地存储仿真正在运行。 然后,您可以在QueueTriggeredFunction中放置一个断点,以检查正在发送的消息:

Image for post

重要要点 (Key take-aways)

This wraps up everything I wanted to share at this point, I wanted to create a starting point for running a containerized local Azure Function application that is connected to a local Storage Emulator, hope you find it useful.

这总结了我现在想要共享的所有内容,我想创建一个起点来运行一个容器化的本地Azure Function应用程序,该应用程序连接到本地Storage Emulator,希望您发现它有用。

翻译自: https://medium.com/@M3rken/azure-functions-express-running-azure-functions-locally-using-docker-compose-bf6be03250fc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值