6

Dapr 和 Azure Functions : Hello world

 2 years ago
source link: https://www.cnblogs.com/shanyou/p/15805701.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Dapr 和 Azure Functions : Hello world

本篇文章内容来自 https://charliedigital.com/2021/07/01/dapr-and-azure-functions-part-1-hello-world/ ,是按这篇文章的操作记录。

使用 Azure Functions Core Tools 可以在本地计算机上通过命令提示符或终端开发和测试函数。 本地函数可以连接到实时 Azure 服务,你可以在本地计算机上使用完整的 Functions 运行时调试函数。安装使用 Azure Functions Core Tools: https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-run-local?tabs=v4%2Cwindows%2Ccsharp%2Cportal%2Cbash%2Ckeda

第一步:创建 函数项目:

创建一个项目文件夹 dapr-func,使用Visual Studio code 打开并运行下面的命令

func init --name HelloWorldFunc --worker-runtime dotnet

第二步:添加HelloWorld 函数

运行命令 func function new --name HelloWorld --authlevel anonymous ,选择 HttpTrigger

运行命令 func start 测试函数

浏览器访问 http://localhost:7071/api/HelloWorld

第三步: 用Dapr 来运行函数

使用下列命令通过Dapr 边车运行函数

dapr run --app-id helloworldfuncdapr --app-port 7071 --dapr-http-port 7070 func start

  1. --app-id helloworldfuncdapr 是一个应用标识符,他将是服务URL的一部分
  2. --app-port 7071 是应用程序将与 Dapr 通信的端口
  3. --dapr-http-port 7070 是 Dapr 通过 sidecar 暴露应用程序端口(在本例中为 7071)的地方
  4. func start 运行函数应用程序

我们仍然可以使用 直接访问 Function http://localhost:7071/api/HelloWorld,但现在我们也可以使用 Dapr sidecar 访问它http://localhost:7070/v1.0/invoke/helloworldfuncdapr/method/api/HelloWorld

让我们分解网址:

  1. http://localhost:7070/v1.0/invoke 注意我们指定的端口 7070 --dapr-http-port
  2. /helloworldfuncdapr是我们之前指定的--app-id
  3. /method/api/HelloWorld 是到 Functions 端点的路由

Dapr 和 Functions 在本地机器上的不同进程中运行,Dapr 充当转发代理。func.exe 正在运行我们的 Functions 应用程序,而 Dapr 在这一点上或多或少像一个简单的 HTTP 转发代理。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK