在非k8s 环境下 的应用 使用 Dapr Sidekick for .NET

2022年5月24日09:56:56

在k8s 环境下,通过Operator 可以管理Dapr sidecar, 在虚拟机环境下,我们也是非常需要这样的一个管理组件,类似下图:

daprmanage

在这张图片中,在上图左面,我们看到了“dapr.exe”、我们的应用程序和另一个进程“daprd.exe”之间的通信,该进程实际上是 Sidecar 进程。这是通常的方式(“开箱即用”),例如:

dapr run --app-idbackend--app-port 5001-- dotnet run --urls=http://localhost:5001/ -p ./WeatherForecastService/WeatherForecastService.csproj

dapr run --app-idproxy--app-port 6001-- dotnet run --urls=http://localhost:6001/ -p ./WeatherForecastProxyService/WeatherForecastProxyService.csproj

图片右面,使用Sidekick简化了此过程/通信,并且我们可以更好地控制以及其他功能。Dapr Sidekick for .NET 是一个组件,它允许我们将 Dapr 添加到我们的项目中以避免摩擦。简化 .NET 开发和操作。 当我们的项目部署在虚拟机环境时,推荐使用这个组件。通过Sidekick 我们的应用程序/进程负责启动和运行 Dapr 所需的一切。

我的示例代码放在这里:https://github.com/geffzhang/ServiceToService-Sideckick ,通过简单的三步就可以完成这项工作。

1、添加Nuget 包Man.Dapr.Sidekick.AspNetCore :

dotnet add package Man.Dapr.Sidekick.AspNetCore --version 1.2.1

2、修改类Startup.cs 的ConfigureServices 方法如下:

public void ConfigureServices(IServiceCollection services)
{

services.AddControllers();

// Add Dapr Sidekick
services.AddDaprSidekick(Configuration);

}

3、接下来,当我们的调用(或代理)应用程序调用另一个应用程序时,名称/id 为“backend ”,我们需要指定其AppId。以同样的方式,由于我们使用“http”,我们必须指出"AppSsl": false。所有这些规范都通过“appsetings.json ” 文件传递给 Sidekick,如下所示。

项目backend 的配置:

"DaprSidekick": {
   // Set the runtime location of config/components files to be the "dapr" folder under the deployed application
   "RuntimeDirectory": "dapr",
   "Sidecar": {
     "AppId": "backend",
     "AppSsl": false,
     "AppPort": 5001,
     "DaprHttpPort": 3501,
     "DaprGrpcPort": 50001
   },
   "Placement": {},
   "Sentry": {}
}

项目 proxy的配置

"DaprSidekick": {
   // Set the runtime location of config/components files to be the "dapr" folder under the deployed application
   "RuntimeDirectory": "dapr",
   "Sidecar": {
     "AppId": "proxy",
     "AppSsl": false,
     "AppPort": 6001,
     "DaprHttpPort": 3601,
     "DaprGrpcPort": 60001
   },
   "Placement": {},
   "Sentry": {}
}

注意对于上述配置文件,由于我们运行多个项目,我们还必须指定“AppPort ”、“DaprHttpPort ”和“DaprGrpcPort ”属性。其余的“Placement”和“Sentry”部分,以及其他属性,暂时可以忽略

RuntimeDirectory  是Dapr 运行时配置文件位置,我们在示例里测试使用Consul 作为服务注册和服务发现组件。

改造后直接运行就可以了,这个特别适合IOT场景下使用Dapr。

dotnet WeatherForecastService.dll --urls=http://localhost:5001

dotnet WeatherForecastProxyService.dll --urls=http://localhost:6001

  • 作者:张善友
  • 原文链接:https://www.cnblogs.com/shanyou/p/16113415.html
    更新时间:2022年5月24日09:56:56 ,共 2046 字。