当前位置: 首页 > 编程日记 > 正文

桌面应用程序 azure_如何开始使用Microsoft Azure-功能应用程序,HTTP触发器和事件队列...

桌面应用程序 azure

"Serverless" architecture is all the rage in tech land at the moment, including heavy usage at my new workplace.

“无服务器”架构目前在科技界风靡一时,包括在我的新工作场所中大量使用。

Microsoft jumped into this space with Azure a while back. Their portal, where all the services are grouped and "organized", offers so many services it feels nearly impossible to be an expert.

微软不久前就通过Azure进入了这个领域。 他们的门户网站将所有服务分组和“组织”在一起,提供了如此多的服务,成为专家几乎感觉不到。

I'll cover function apps, triggers, bindings, and event queues, enough to get a web developer started on something great in the cloud.

我将介绍功能应用程序,触发器,绑定和事件队列 ,足以使Web开发人员着手开发云中的出色功能。

Tulip fields near Amsterdam

Two months ago I had never worked with any of these technologies, but I am loving how clean, clearly separated, and async these tools can be. Azure services have some flaws (portal speed, UI problems, and as you scale, difficulty in understanding what is dragging down the system) but on the whole the technology is easy to use and very powerful.

两个月前,我从未使用过任何这些技术,但是我喜欢这些工具可以做到多么干净,清晰地分离和异步。 Azure服务存在一些缺陷(门户速度,UI问题,以及随着扩展而导致的问题,难以理解导致系统崩溃的原因),但总体而言,该技术易于使用且非常强大。

功能应用 (Function Apps)

The first thing you'll do to get started is create a Function App. In Azure's world, this is a grouping of related functionality. You will create discrete functions inside the Function App. Each Function App has access to a storage container.

首先,您要做的是创建一个功能应用程序。 在Azure的世界中,这是一组相关功能。 您将在Function App中创建离散函数 。 每个功能应用程序都可以访问存储容器。

Creating a function app in Azure's web portal

Function app settings

From the portal (after account setup and payment), you'll click "Create a Resource" at the very top, and select Function App. Give your app a name, choose a language to write your code in (I chose Node for Javascript), and keep the rest of the settings as they are.

从门户网站(设置和付款帐户后),您将单击最顶部的“创建资源”,然后选择Function App。 为您的应用命名,选择一种语言来编写代码(我为Java选择了Node),其余设置保持原样。

It takes a minute to deploy a new Function App - you can click on the notification in the upper right to watch progress and get an easy link when it's ready.

部署新的Function App需要一分钟-您可以单击右上角的通知以查看进度,并在准备就绪时获得简单链接。

增加功能 (Adding functionality)

Once the Function App is ready, click the plus next to Functions. Select "In Portal editor" and "Webhook / API". We'll be doing the most basic setup possible. CI/CD deployments and such are outside the scope of this tutorial, but of course you can version control your work using git tools and deploy through a pipeline.

准备好功能应用程序后,单击“功能”旁边的加号。 选择“在门户网站编辑器中”和“ Webhook / API”。 我们将进行最基本的设置。 CI / CD部署等不在本教程的讨论范围之内,但是您当然可以使用git工具进行版本控制并通过管道进行部署。

editing-azure-function-in-portal

Azure function app logs after http trigger

绑定 (Bindings)

After you create your function, you'll get an index.js and a function.json. You can access these to the right of your in-portal editor under "View Files".

创建函数后,您将获得一个index.js和一个function.json 。 您可以在“查看文件”下的门户编辑器右侧访问这些文件。

Let's look at function.json first - it's the configuration file for your function. Here's the file as the portal will spit it out:

首先让我们看一下function.json -这是您的函数的配置文件。 这是门户网站将吐出的文件:

{"bindings": [{"authLevel": "function","type": "httpTrigger","direction": "in","name": "req","methods": ["get","post"]},{"type": "http","direction": "out","name": "res"}],"disabled": false
}

Bindings are Azure's handy way to connect to various services and resources without needing to do a lot of setup.

绑定是Azure连接到各种服务和资源的便捷方法,无需进行大量设置。

httpTrigger (httpTrigger)

  • Change authLevel to "anonymous" and we won't need a key to call our function.

    authLevel更改为"anonymous" ,我们将不需要键来调用我们的函数。

  • The type is httpTrigger - that means we're going to generate

    typehttpTrigger这意味着我们将生成

    an endpoint we can use to call this function (more below).

    我们可以用来调用此功能的端点(更多信息请参见下文)。

  • The direction can be either in or out - indicating input or output to/from our function.

    direction可以是inout -指示从我们的函数输入或输出到/。

  • The trigger will call the function with parameters supplied. We can call the binding whatever we want, but convention says we name the input req for request and the output res for response.

    触发器将使用提供的参数来调用该函数。 我们可以随意调用绑定,但是习惯上说我们将输入req命名为请求,将输出res命名为响应。

  • Methods are how we can call this function. I'll delete post, as we won't need it.

    Methods是我们如何调用此函数的方法。 我将删除post ,因为我们不需要它。

The next binding is also type http, direction out, because this is how we'll send a response. If you want to disable the function, you can do it here in the function.json or in the UI of the portal.

下一个绑定也是type http ,direction out ,因为这是我们发送响应的方式。 如果要禁用该功能,可以在此处在function.json或门户的UI中进行。

在功能代码中使用绑定 (Using bindings in the function code)

We can use the example code provided by Azure to show how bindings work. I've simplified Azure's code but you should have something very similar in your index.js if you're following along:

我们可以使用Azure提供的示例代码来显示绑定的工作方式。 我简化了Azure的代码,但是如果您遵循以下步骤,则应该在index.js有一些非常相似的内容:

module.exports = async function (context, req) {context.log('JavaScript HTTP trigger function processed a request.');if (req.query.name) {context.res = {// status: 200, /* Defaults to 200 */body: "Hello " + req.query.name};}else {context.res = {status: 400,body: "Please pass a name on the query string or in the request body"};}
};

Sending a request through Postman to our Azure function app

Here you can see we have a simple web hook. We can watch it work quickly by clicking on "Get function URL" and sending a request with cURL or Postman. So what's going on?

在这里您可以看到我们有一个简单的Web挂钩。 我们可以通过单击“获取功能URL”并使用cURL或Postman发送请求来快速查看其工作情况。 发生什么了?

Azure function app logs receiving an http trigger

Through Azure magic, our function has been called with context and the req that we sent. The context allows us access to the bindings we saw in function.json and the req becomes input for our function.

通过Azure魔术,已经使用context和发送的req调用了我们的函数。 context允许我们访问在function.json看到的绑定,并且req成为函数的输入。

More Azure magic handles the response. We simply set the status to our requirements, and the body appropriately. Azure handles the rest. (Note that if you are sending JSON or other non-string response objects, you'll have to set the headers.)

更多Azure魔术可以处理响应。 我们只需根据自己的要求设置status ,并适当地设置body 。 Azure负责其余的工作。 (请注意,如果要发送JSON或其他非字符串响应对象,则必须设置标头。)

We can see all this working in the logs. You'll find them at the very bottom of the page under the index.js code. Note that console.log won't help you here - you'll need to use context.log.

我们可以在日志中看到所有这些工作。 您可以在页面底部index.js代码下找到它们。 请注意, console.log在这里无济于事-您需要使用context.log

And that's it! We have a web hook running in the "cloud".

就是这样! 我们有一个在“云”中运行的网络挂钩。

环境变量 (Env variables)

We probably won't get far without having some secrets we use to connect to databases, external APIs, and other services. You can keep those variables under "Configuration" when you click the name of your Function App (as opposed to Functions). Azure will automatically put a bunch in there to deal with your Function App storage and Application Insights (monitoring).

如果没有用于连接数据库,外部API和其他服务的一些秘密,我们可能不会走得太远。 当您单击Function App的名称(与Function相对)时,可以将这些变量保留在“配置”下。 Azure将自动在其中放置一些功能来处理您的Function App存储和Application Insights(监视)。

其他绑定 (Other bindings)

There are dozens of bindings that can be used. Azure can connect to several different types of databases, and you can create a trigger for a new document being created. Likewise, this method could receive a payload that would create a record with an out binding.

可以使用数十种绑定。 Azure可以连接到几种不同类型的数据库,并且可以为要创建的新文档创建触发器。 同样,此方法可以接收将创建带有out绑定的记录的有效负载。

Another interesting binding is an event, which I'll cover next.

另一个有趣的绑定是一个event ,我将在下面讨论。

事件队列 (Event queues)

A request and response is great, but what if we want to create an asynchronous event from an HTTP binding?

请求和响应很棒,但是如果我们想通过HTTP绑定创建异步事件怎么办?

Say we are creating a user. After that user is created, we want to send an email to welcome them to our service.

假设我们正在创建一个用户。 创建该用户之后,我们希望发送电子邮件以欢迎他们使用我们的服务。

But if something is wrong with our send-email code, we would never want that to get in the way of creating a user or responding to the customer. We can create async messaging through an event queue.

但是,如果我们的发送电子邮件代码出了点问题,我们绝不会希望这种方式妨碍创建用户或响应客户。 我们可以通过事件队列创建异步消息传递。

I won't get too deep into how queues work, but the idea is quite simple: one method puts messages into a line, and another peels them off and does something with them (last in, first out). When the queue is empty, anything that listens to it is quiet.

我不会对队列的工作原理有很深的了解,但是这个想法很简单:一种方法将消息放入一行,另一种将消息剥离并对其进行处理(后进先出)。 当队列为空时,任何可以监听的内容都是安静的。

Azure has functionality for different types of queues, including the more complex Service Bus.

Azure具有适用于不同类型队列的功能,包括更复杂的服务总线。

创建队列 (Creating the queue)

The first thing we'll do is create a queue that our functions can push and read from. If you click "Home" in the breadcrumbs, you should see your Function App name in recent resources.

我们要做的第一件事是创建一个队列,我们​​的函数可以推送和读取该队列。 如果单击面包屑中的“主页”,则应该在最近的资源中看到您的Function App名称。

The link here is actually to the "Resource group" - a collection of all the items you created with the Function App. Click in, then select your storage (it looks like a database table).

此处的链接实际上是“资源组”的链接-您使用Function App创建的所有项目的集合。 单击,然后选择您的存储(它看起来像数据库表)。

Now find the Queues tab, click "+Queue", then create a queue for your function app. Note that you can have as many queues as you like. You can also create items in your queue manually for testing your functions independently.

现在,找到“队列”选项卡,单击“ +队列”,然后为您的函数应用创建队列。 请注意,您可以根据需要拥有任意数量的队列。 您还可以手动在队列中创建项目以独立测试功能。

Azure function app resource group

队列绑定 (Queue bindings)

Add a new function (click the + icon) and select "Azure Queue Storage trigger". This will prompt you to install an Event Hub extension, then give your function a name - you can ignore the other settings for now. Here's the function.json code for our new bindings:

添加一个新功能(单击+图标),然后选择“ Azure Queue Storage触发器”。 这将提示您安装Event Hub扩展,然后为您的函数命名-您现在可以忽略其他设置。 这是我们新绑定的function.json代码:

{"bindings": [{"name": "myQueueItem","type": "queueTrigger","direction": "in","queueName": "js-queue-items","connection": "AzureWebJobsStorage"}],"disabled": false
}

We'll use the storage we created for this function app to keep a queue of events. The index.js takes in the item from the queue and executes with it. We don't need to do much with our function, so we can just keep the demo code from Azure:

我们将使用为此功能应用程序创建的存储来保留事件队列。 index.js从队列中取出项目并执行。 我们不需要对我们的功能做太多事情,因此我们可以保留Azure的演示代码:

module.exports = async function (context, myQueueItem) {context.log('JavaScript queue trigger function processed work item', myQueueItem);
};

Azure function app logs after queue trigger

将项目添加到队列 (Adding an item to the queue)

Your queue trigger function is running, but it won't do anything until we add an item to our queue. We can do this by adjusting our first function to put an item in the queue when the web hook is called.

您的队列触发功能正在运行,但是在我们向队列添加项目之前它不会做任何事情。 为此,我们可以通过调整第一个函数来在调用Web挂钩时将项目放入队列中来实现。

{ bindings: [...],{"name": "myQueueItem","type": "queue","direction": "out","queueName": "js-queue-items","connection": "AzureWebJobsStorage"}
}

Now we can update our code to add an item to the queue:

现在,我们可以更新代码以将项目添加到队列中:

[...]if (req.query.name) {context.bindings.myQueueItem = {name: req.query.name,ts: new Date()}
[...]

If you open your functions in separate windows, you can watch the whole thing happen in the logs for each function. Send your request, watch it be consuming in the http trigger, then the queue trigger will pick up the message placed by the first function. Cool, right?

如果在单独的窗口中打开函数,则可以在每个函数的日志中看到整个过程。 发送您的请求,查看它是否在http触发器中消耗,然后队列触发器将提取第一个函数放置的消息。 酷吧?

This is some powerful stuff for creating asynchronous jobs in a way that prevents your functions from taking each other out. In a "monolith" model, if one function misbehaves, it can prevent others from executing. In this model, if there is something wrong with the queue trigger, it won't prevent the server from responding appropriately. Naturally, this adds a layer of complexity and consideration that doesn't exist in a monolith - there are always trade-offs.

这是一些强大的功能,可用于创建异步作业,从而防止您的函数相互淘汰。 在“整体”模型中,如果一个函数行为不当,则可能阻止其他函数执行。 在此模型中,如果队列触发器出现问题,则不会阻止服务器适当地响应。 自然,这增加了整体中不存在的复杂性和考虑层-总是存在权衡取舍。

But if we go back to our theoretical use-case where we are creating a new user with an http trigger, image having 10 queues to do various things. Our http trigger might create a document in the database and return success in the request.

但是,如果我们回到理论上的用例,即用http触发器创建一个新用户,则该图像具有10个队列来执行各种操作。 我们的http触发器可能会在数据库中创建一个文档,并在请求中返回成功。

The same job might add to a queue about sending an email, triggering a coupon SMS to send in an hour, putting a Slack message to a marketing team, or any of a million other things that might need to happen when a new user is created. If any one of them fails (Slack is down?), the others go on happily on their way.

相同的工作可能会增加有关发送电子邮件,触发优惠券SMS在一小时内发送,向市场营销团队发送Slack消息或创建新用户时可能需要发生的其他百万种事情中的队列。 。 如果其中任何一个发生故障(Slack掉线了吗?),其他两个就继续快乐地前进。

This article has only brushed the surface of what's available. I'm looking forward to exploring more of what Azure can do. Let me know if you have some tips for what to build next!

本文仅介绍了可用的内容。 我期待探索更多Azure可以做什么。 如果您有一些下一步的建议,请告诉我!

You can read more of my articles on my blog at wilkie.tech.

您可以在wilkie.tech的博客上我的文章。

翻译自: https://www.freecodecamp.org/news/getting-started-with-microsoft-azure/

桌面应用程序 azure

相关文章:

开始Flask项目

新建Flask项目。设置调试模式。理解Flask项目主程序。使用装饰器,设置路径与函数之间的关系。使用Flask中render_template,用不同的路径,返回首页、登录员、注册页。用视图函数反转得到URL,url_for(‘login’),完成导航…

JavaScript中的加法运算

<head runat"server"> <title>JavaScript实现加法计算器</title> <script type"text/javascript"> function Sum() { var txtbox1 document.getElementById("txtbox1"); var txtbox2 document.getElementById("…

计算机视觉技术 图像分类_如何训练图像分类器并教您的计算机日语

计算机视觉技术 图像分类介绍 (Introduction) Hi. Hello. こんにちは你好 你好。 こんにちは Those squiggly characters you just saw are from a language called Japanese. You’ve probably heard of it if you’ve ever watched Dragon Ball Z.您刚刚看到的那些蠕动的字符…

history对象

history对象记录了用户曾经浏览过的页面(URL)&#xff0c;并可以实现浏览器前进与后退相似导航的功能。 注意:从窗口被打开的那一刻开始记录&#xff0c;每个浏览器窗口、每个标签页乃至每个框架&#xff0c;都有自己的history对象与特定的window对象关联。 语法&#xff1a; w…

无限循环动画实现

先来个效果图 示例代码是先缩小移动&#xff0c;然后无限循环左右晃动&#xff0c;希望能够帮助到你&#xff0c;点个赞吧~ 实现代码 <image class"element1" load"element1_load" :animation"animationData" src"../../static/element…

利用属性封装复杂的选项

1、考虑这样一个场景。 我们的程序中有一个“选项”窗口&#xff0c;这个窗口包含很多选项。其中有一个选项是单选类型的&#xff0c;用户可以从N个选项值中选择一个。 我们需要在用户单击“确定”按钮后把用户选择的值保存到文件中&#xff0c;程序下次启动时再读取到内存中。…

react 渲染道具_关于React道具的另一篇文章

react 渲染道具You could say this topic has been done to death, but lately I’ve started using a technique that I dont recall having come across elsewhere. While its not particularly clever, it is concise. So please forgive one more post on the topic...你…

高可用集群的概念

一&#xff1a;什么是高可用集群 高可用集群&#xff08;High Availability Cluster&#xff0c;简称HA Cluster&#xff09;&#xff0c;是指以减少服务中断时间为目的得服务器集群技术。它通过保护用户得业务程序对外部间断提供的服务&#xff0c;把因为软件&#xff0c;硬件…

node.js 出现cannot find module ‘xxx‘ 解决办法

找不到模块的解决方案 &#xff1a; 把node_module整个文件夹删掉,然后npm clean cache,看下package.json里有没有express的依赖项,有的话直接npm install,没有的话 npm install express --save npm clean cache 如果不行的话用 npm cache clean

poj 1904 King's Quest

Kings Quest 题意&#xff1a;有N个王子和N个妹子;(1 < N < 2000)第i个王子喜欢Ki个妹子&#xff1b;(详见sample)题给一个完美匹配&#xff0c;即每一个王子和喜欢的一个妹子结婚&#xff1b;问每一个王子可以有几种选择(在自己喜欢的妹子里面选)&#xff0c;并输出可选…

数据预处理--噪声_为什么数据对您的业务很重要-以及如何处理数据

数据预处理--噪声YES! Data is extremely important for your business.是&#xff01; 数据对您的业务极为重要。 A human body has five sensory organs, and each one transmits and receives information from every interaction every second. Today, scientists can det…

C++ template

&#xff08;转自http://www.cnblogs.com/gw811/archive/2012/10/25/2738929.html&#xff09; C模板 模板是C支持参数化多态的工具&#xff0c;使用模板可以使用户为类或者函数声明一种一般模式&#xff0c;使得类中的某些数据成员或者成员函数的参数、返回值取得任意类型。 模…

js根据经纬度取随机附近经纬度

实现功能&#xff1a; 小程序根据当前经纬度得出随机的附近经纬度显示在地图上做标记点&#xff0c;效果图 实现代码 // map.js var app getApp() var mymap ; var lat ; var long ; var that;function latlog(lat, lon, d 1,d23) {var angle Math.random(1, 360);var …

讽刺笑话_完全不讽刺的2019年网络设计指南

讽刺笑话I’ve written about how to design for the modern web before, way back in 2018. But the web moves forward quickly so those guidelines are already obsolete and outdated, as more modern conventions have become mainstream.早在2018年&#xff0c;我就已经…

模拟城市2.0

题目背景 博弈正在机房颓一个叫做《模拟城市2.0》的游戏。 2048年&#xff0c;经过不懈努力&#xff0c;博弈终于被组织委以重任&#xff0c;成为D市市委书记&#xff01;他勤学好问&#xff0c;励精图治&#xff0c;很快把D市建设成富强民主文明和谐的美好城市。为了进一步深化…

bzoj:1221;vijos 1552 软件开发

Description 某软件公司正在规划一项n天的软件开发计划&#xff0c;根据开发计划第i天需要ni个软件开发人员&#xff0c;为了提高软件开发人员的效率&#xff0c;公司给软件人员提供了很多的服务&#xff0c;其中一项服务就是要为每个开发人员每天提供一块消毒毛巾&#xff0c;…

u-charts 曲线图中间有部分没数据,导致点和点无法连成线的问题解决

解决曲线图或者折线图在两端中间没有数据时无法绘画成线的问题源码修改, 解决方案: 在数据之间填充假数据,并且创建一个和点的数据同级的 list 来验证是不是假数据,如果是假数据就不绘制点,是真数据才绘制点,达到点和点之间没数据但是能连线的效果 先看效果图: 数据格…

python构建json_如何使用Python构建JSON API

python构建jsonThe JSON API specification is a powerful way for enabling communication between client and server. It specifies the structure of the requests and responses sent between the two, using the JSON format.JSON API规范是启用客户端和服务器之间通信的…

样式集 - 自适应居中弹窗

效果图&#xff1a; 弹窗1代码 <!-- 答题正确弹窗 --><block v-if"answer_true_show"><view class"answer_true_bg"></view><view class"answer_true"><img class"true_bg_img" :src"qualifyi…

struts2中 ServletActionContext与ActionContext区别

1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作. 我们需要在Action中取得req…

[记录]calculate age based on date of birth

calculate age based on date of birth know one new webiste:eval.in run php code转载于:https://www.cnblogs.com/fsong/p/5190273.html

有抱负的Web开发人员应考虑的6件事

Becoming a web developer can be as challenging as working out every day.成为网络开发人员就像每天锻炼一样具有挑战性。 It’s important to know what it will take to succeed as a web developer.重要的是要知道要成为一名Web开发人员要取得成功。 Here are 6 things…

阿里云OSS上传图片实现流程

前置&#xff0c;在阿里云开通OSS对象储存。然后在下图文件管理配置文件储存目录和图中传输管理配置访问域名。 1.复制 uploadFileUtil 文件夹和 uploadFile.js 文件在 util 文件夹 2.在使用的页面 引入 uploadFile 效果图&#xff1a; 实现代码 <template><view c…

修改远程桌面连接3389端口号

修改注册表&#xff1a; HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\Wds\Repwd\Tds\Tcp 键&#xff1a;PortNumber&#xff0c;以十进制显示&#xff1a;3389&#xff0c;修改成55555&#xff0c;保存刷新注册表。 HKEY_LOCAL_MACHINE\SYSTEM\Curre…

理解 : UDID、UUID、IDFA、IDFV

iOS获取设备唯一标识的各种方法&#xff1f;IDFA、IDFV、UDID分别是什么含义&#xff1f;iOS获取设备ID总结IDFA解释 关于UUID的理解 : 英文名称是&#xff1a;Universally Unique Identifier,翻译过来就是通用唯一标识符。 UUID是指在一台机器上生成的数字&#xff0c;它保证对…

推箱子2-向右推!_保持冷静,砍箱子-me脚

推箱子2-向右推!Hack The Box (HTB) is an online platform allowing you to test your penetration testing skills. It contains several challenges that are constantly updated. Some of them simulating real world scenarios and some of them leaning more towards a C…

H5面试题---介绍js的基本数据类型

js的基本数据类型 Undefined、Null、Boolean、Number、String 转载于:https://www.cnblogs.com/songchunmin/p/7789582.html

Node.js express 之mongoose 从异步回调函数返回值,类似于同步

http://my.oschina.net/antianlu/blog/187023转载于:https://www.cnblogs.com/cylblogs/p/5192314.html

小程序登录、用户信息相关接口调整说明

为&#xfeff;优化用户的使用体验&#xff0c;平台将进行以下调整&#xff1a; 2021年2月23日起&#xff0c;若小程序已在微信开放平台进行绑定&#xff0c;则通过wx.login接口获取的登录凭证可直接换取unionID2021年4月13日后发布的小程序新版本&#xff0c;无法通过wx.getU…

小程序 reduce_使用Reduce制作的10个JavaScript实用程序功能

小程序 reduceThe multi-tool strikes again. 多功能工具再次触击。 In my last article I offered you a challenge to recreate well-known functions using reduce. This article will show you how some of them can be implemented, along with some extras! 在上一篇文章…