面向.NET开发人员的Dapr——入门

目录:

  • 面向.NET开发人员的Dapr——前言

  • 面向.NET开发人员的Dapr——分布式世界

  • 面向.NET开发人员的Dapr——俯瞰Dapr

Get started with Dapr

Dapr 入门

In the first two chapters, you learned basic concepts about Dapr. It's time to take it for a test drive. This chapter will guide you through preparing your local development environment and building two Dapr .NET applications.

前两章介绍了有关 Dapr 的基本概念。现在实操一下。本章将指导你完成本地开发环境的准备工作,并构建两个 Dapr .NET 应用程序。

Install Dapr into your local environment

将 Dapr 安装到本地环境中

You'll start by installing Dapr on your development computer. Once complete, you can build and run Dapr applications in self-hosted mode.

  1. Install the Dapr CLI. It enables you to launch, run, and manage Dapr instances. It also provides debugging support.

  2. Install Docker Desktop. If you're running on Windows, make sure that Docker Desktop for Windows is configured to use Linux containers.

首先,在开发计算机上安装 Dapr。完成后,可以在 自承载模式下构建并运行 Dapr 应用程序。

  1. 安装 DAPR CLI。它使您可以启动、运行和管理 Dapr 实例。它还提供调试支持。

  2. 安装 Docker Desktop。如果在 Windows 上运行,请确保将 适用于 windows 的 Docker Desktop 配置为使用 Linux 容器。

Note

By default, Dapr uses Docker containers to provide you the best out-of-the-box experience. To run Dapr outside of Docker, you can skip this step and execute a slim initialization. The examples in this chapter require you use Docker containers.

备注

默认情况下,Dapr 使用 Docker 容器为你提供最佳的开箱即用体验。若要在 Docker 外部运行 Dapr,可以跳过此步骤并 执行至简初始化。本章中的示例要求使用 Docker 容器。

  1. Initialize Dapr. This step sets up your development environment by installing the latest Dapr binaries and container images.

  2. Install the .NET Core 3.1 SDK.

  1. 初始化 Dapr。此步骤通过安装最新的 Dapr 二进制文件和容器映像来设置开发环境。

  2. 安装 .NET Core 3.1 SDK。

Now that Dapr is installed, it's time to build your first Dapr application!

安装 Dapr 后,就可以构建你的第一个 Dapr 应用程序了!

Build your first Dapr application

构建第一个 Dapr 应用程序

You'll start by building a simple .NET Console application that consumes the Dapr state management building block.

首先,你将构建一个简单的 .NET 控制台应用程序,该应用程序使用 Dapr 状态管理 构建块。

Create the application

创建应用程序

  1. Open up the command shell or terminal of your choice. You might consider the terminal capabilities in Visual Studio Code. Navigate to the root folder in which you want to build your application. Once there, enter the following command to create a new .NET Console application:       打开命令行终端。可以考虑使用Visual Studio Code中的终端。 导航到要构建应用程序的根文件夹。完成后,输入以下命令以创建新的 .NET 控制台应用程序:

    dotnet new console -o DaprCounter

    The command scaffolds a simple "Hello World" .NET Core application.      该命令搭建简单的"Hello World“.NET Core 应用程序。

  2. Then, navigate into the new directory created by the previous command:      然后,定位到上一命令创建的新目录:

    cd DaprCounter
  3. Run the newly created application using the dotnet run command. Doing so writes "Hello World!" to the console screen:      使用 dotnet run 命令运行新创建的应用程序。这样做会在控制台屏幕上输出"Hello World!" :

dotnet run

Add Dapr State Management

添加 Dapr 状态管理

Next, you'll use the Dapr state management building block to implement a stateful counter in the program.

接下来,你将使用 Dapr 状态管理构建块 在程序中实现有状态计数器。

You can invoke Dapr APIs across any development platform using Dapr's native support for HTTP and gRPC. However, .NET Developers will find the Dapr .NET SDK more natural and intuitive. It provides a strongly typed .NET client to call the Dapr APIs. The .NET SDK also tightly integrates with ASP.NET Core.      可以使用 Dapr 对 HTTP 和 gRPC 的本机支持跨任何开发平台调用 Dapr API。然而,.NET 开发者会发现 Dapr .NET SDK 更自然、更直观。它提供强类型的 .NET 客户端来调用 Dapr API。.NET SDK 还与 ASP.NET Core 紧密集成。

  1. From the terminal window, add the Dapr.Client NuGet package to your application:      在终端窗口中,将 Dapr.Client 的NuGet 包添加到应用程序:

    dotnet add package Dapr.Client

    Note

    If you're working with a pre-release version of Dapr, be sure to add the --prerelease flag to the command.

    备注

    如果使用 Dapr 的预发行版本,请确保在命令中添加 --prerelease 标志。

  2. Open the Program.cs file in your favorite editor and update its contents to the following code:      在你喜欢的编辑器中打开  Program.cs 文件,将其内容更新为以下代码:

    using System;
    using System.Threading.Tasks;
    using Dapr.Client;namespace DaprCounter
    {class Program{static async Task Main(string[] args){const string storeName = "statestore";const string key = "counter";var daprClient = new DaprClientBuilder().Build();var counter = await daprClient.GetStateAsync<int>(storeName, key);while (true){Console.WriteLine($"Counter = {counter++}");await daprClient.SaveStateAsync(storeName, key, counter);await Task.Delay(1000);}}}
    }

    The updated code implements the following steps:      更新的代码实现以下步骤:

  • First a new DaprClient instance is instantiated. This class enables you to interact with the Dapr sidecar.      首先实例化一个 DaprClient 新实例。此类使你可以与 Dapr 边车交互。

  • From the state store, DaprClient.GetStateAsync fetches the value for the counter key. If the key doesn't exist, the default int value (which is 0) is returned.      DaprClient.GetStateAsync 从状态存储中获取counter键对应的值 。如果该键不存在,返回默认的整数值0。

  • The code then iterates, writing the counter value to the console and saving an incremented value to the state store.      然后,代码将在控制台上循环输出计数器值,并将递增的值保存到状态存储中。

  • The Dapr CLI run command starts the application. It invokes the underlying Dapr runtime and enables both the application and Dapr sidecar to run together. If you omit the app-id, Dapr will generate a unique name for the application. The final segment of the command, dotnet run, instructs the Dapr runtime to run the .NET core application.       使用Dapr CLI run 命令启动应用程序。它调用底层 Dapr 运行时,并使应用程序和 Dapr 边车一起运行。如果省略 app-id参数,则 Dapr 将为应用程序生成唯一的名称。命令的最后一段 dotnet run 指示 Dapr 运行时运行 .net core 应用程序。

    Important

    Care must be taken to always pass an explicit app-id parameter when consuming the state management building block. The block uses the application id value as a prefix for its state key for each key/value pair. If the application id changes, you can no longer access the previously stored state.

    重要

    使用状态管理构建块时,必须注意始终显式传递app-id参数。对于每个键/值对,状态管理构建块使用应用程序 id 值作为其状态键的 前缀 。如果应用程序 id 发生更改,则无法再访问以前存储的状态。

    Now run the application with the following command:      现在,用以下命令运行应用程序:

    dapr run --app-id DaprCounter dotnet run

    Try stopping and restarting the application. You'll see that the counter doesn't reset. Instead it continues from the previously saved state. The Dapr building block makes the application stateful.

  •              尝试停止并重新启动应用程序。你将看到计数器未被重置。相反,它会从以前保存的状态继续。Dapr 构建块会使应用程序具有状态。

    Important

    It's important to understand your sample application communicates with a pre-configured state component, but has no direct dependency on it. Dapr abstracts away the dependency. As you'll shortly see, the underlying state store component can be changed with a simple configuration update.

    重要

    务必了解你的示例应用程序与预配置状态组件的通信,但没有直接依赖它。Dapr 消除依赖关系。如你稍后所见,可以通过简单的配置更新来替换底层状态存储组件。

    You might be wondering, where exactly is the state stored?

    你可能想知道状态存储在何处?

    Component configuration files

    组件配置文件

    When you first initialized Dapr for your local environment, it automatically provisioned a Redis container. Dapr then configured the Redis container as the default state store component with a component configuration file, entitled statestore.yaml. Here's a look at its contents:

    首次为本地环境初始化 Dapr 时,会自动预配 Redis 容器。然后,Dapr 使用名为  statestore.yaml 的组件配置文件将 Redis 容器配置为默认状态存储组件。下面查看其内容:

    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:name: statestore
    spec:type: state.redisversion: v1metadata:- name: redisHostvalue: localhost:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"

    Note

    Default component configuration files are stored in the $HOME/.dapr/components folder on Linux/macOS, and in the %USERPROFILE%\.dapr\components folder on Windows.

    备注

    默认组件配置文件存储在 Linux/macOS 上的 $HOME/.dapr/components文件夹中,Windows 上的 %USERPROFILE%\.dapr\components 文件夹中。

    Note the format of the previous component configuration file:

    • Each component has a name. In the sample above, the component is named statestore. We used that name in our first code example to tell the Dapr sidecar which component to use.

    • Each component configuration file has a spec p. It contains a type field that specifies the component type. The version field specifies the component version. The metadata field contains information that the component requires, such as connection details and other settings. The metadata values will vary for the different types of components.

    请注意上一个组件配置文件的格式:

    • 每个组件都有一个名称。在以上示例中,组件名为 statestore 。我们在第一个代码示例中使用了该名称来告知 Dapr 边车要使用哪个组件。

    • 每个组件配置文件都有一个 spec节。它包含一个指定组件类型的 type 字段。 version字段指定组件版本。 metadata字段包含组件的必要信息,例如连接详细信息和其他设置。元数据值因不同类型的组件而异。

    A Dapr sidecar can consume any Dapr component configured in your application. But, what if you had an architectural justification to limit the accessibility of a component? How could you restrict the Redis component to Dapr sidecars running only in a production environment?

    Dapr 边车可以使用应用程序中配置的任何 Dapr 组件。但是,假如有构造的正当理由限制组件的可访问性,该怎么办呢?如何限制仅在生产环境中运行的Dapr 边车上使用 Redis 组件呢?

    To do so, you could define a namespace for the production environment. You might name it production. In self-hosted mode, you specify the namespace of a Dapr sidecar by setting the NAMESPACE environment variable. When configured, the Dapr sidecar will only load the components that match the namespace. For Kubernetes deployments, the Kubernetes namespace determines the components that are loaded. The following sample shows the Redis component placed in a production namespace. Note the namespace declaration in the metadata element:

    为此,可以为生产环境定义一个命名空间。可以将其命名为 production 。在自承载模式下,通过设置 NAMESPACE环境变量来指定 Dapr 边车的命名空间 。配置后,Dapr 边车将仅加载与该命名空间匹配的组件。对于 Kubernetes 部署,Kubernetes 命名空间决定加载的组件。下面的示例演示 production命名空间中的 Redis 组件 。请注意 metadata 元素中的namespace 声明 :

    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:name: statestorenamespace: production
    spec:type: state.redisversion: v1metadata:- name: redisHostvalue: localhost:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"

    Important

    A namespaced component is only accessible to applications running in the same namespace. If your Dapr application fails to load a component, make sure that the application namespace matches the component namespace. This can be especially tricky in self-hosted mode where the application namespace is stored in a NAMESPACE environment variable.

    重要

    只有在同一命名空间中运行的应用程序才能访问带命名空间组件。如果 Dapr 应用程序无法加载组件,请确保应用程序命名空间与组件命名空间相匹配。在自承载模式下要注意的一点是:应用程序命名空间存储于 NAMESPACE 环境变量中(不知怎么翻译合适)。

    If needed, you could further restrict a component to a particular application. Within the production namespace, you may want to limit access of the Redis cache to only the DaprCounter application. You do so by specifying scopes in the component configuration. The following example shows how to restrict access to the Redis statestore component to the application DaprCounter in the production namespace:

    如果需要,可以进一步将组件限制到特定的应用程序。在 production 命名空间中,你可能需要将 Redis 缓存的访问限制为仅限 DaprCounter 应用程序。为此,请在组件配置中指定 scopes 。下面的示例演示如何将对 Redis 状态存储组件的访问限制 到 production 命名空间中的应用程序 DaprCounter :

    apiVersion: dapr.io/v1alpha1
    kind: Component
    metadata:name: statestorenamespace: production
    spec:type: state.redisversion: v1metadata:- name: redisHostvalue: localhost:6379- name: redisPasswordvalue: ""- name: actorStateStorevalue: "true"scopes:- DaprCounter 

    Build a multi-container Dapr application

    构建多容器 Dapr 应用程序

    In the first example, you created a simple .NET console application that ran side-by-side with a Dapr sidecar. Modern distributed applications, however, often consist of many moving parts. They can simultaneously run independent microservices. These modern applications are typically containerized and require container orchestration tools such as Docker Compose or Kubernetes.

    在第一个示例中,创建了一个与 Dapr 边车并行运行的简单 .NET 控制台应用程序。不过,现代分布式应用程序通常由许多移动部件组成。它们可以同时运行,不依赖于微服务。这些现代应用程序通常容器化,并需要容器编排工具,例如 Docker Compose 或 Kubernetes。

    In the next example, you'll create a multi-container application. You'll also use the Dapr service invocation building block to communicate between services. The solution will consist of a web application that retrieves weather forecasts from a web API. They will each run in a Docker container. You'll use Docker Compose to run the container locally and enable debugging capabilities.

    在下一个示例中,将创建多容器应用程序。你还将使用 Dapr 服务调用 构建块在服务之间进行通信。该解决方案将包含一个从 Web API 检索天气预报的 Web 应用程序。它们在 各自的Docker 容器中运行。你将使用 Docker Compose在本地运行容器并启用调试功能。

    Make sure you've configured your local environment for Dapr and installed the .NET Core 3.1 Development Tools (instructions are available at the beginning of this chapter).

    请确保为 Dapr 配置了本地环境并安装了 .NET Core 3.1 开发工具 (请参阅本章开头部分) 。

    Additionally, you'll need complete this sample using Visual Studio 2019 with the .NET Core cross-platform development workload installed.

    此外,需要在安装了 .NET Core 跨平台开发工作负载的 Visual Studio 2019中完成此示例。

    Create the application

    创建应用程序

    1. In Visual Studio 2019, create an ASP.NET Core Web App project:      在 Visual Studio 2019 中,创建ASP.NET Core Web 应用项目 :

    2. Name your project DaprFrontEnd and your solution DaprMultiContainer:      项目命名为 DaprFrontEnd ,解决方案命名为 DaprMultiContainer :

    3. Select Web Application to create a web application with Razor pages. Don't select Enable Docker Support. You'll add Docker support later.      选择“Web 应用程序”,创建使用 Razor Pages 的 Web 应用程序 。请勿选择"启用 Docker 支持"。 稍后添加 Docker 支持。

    4. Add a ASP.NET Core Web API project to the same solution and call it DaprBackEnd. Select API as the project type. By default, a Dapr sidecar relies on the network boundary to limit access to its public API. So, clear the checkbox for Configure for HTTPS.      在解决方案中添加 ASP.NET Core Web API 项目,并命名为 DaprBackEnd。选择 "API" 作为项目类型。默认情况下,Dapr 边车依赖于网络边界来限制对公共 API 的访问。因此,请清除"为 HTTPS 配置"复选框。

    Add Dapr service invocation

    添加 Dapr 服务调用

    Now, you'll configure communication between the services using Dapr service invocation building block. You'll enable the web app to retrieve weather forecasts from the web API. The service invocation building block features many benefits. It includes service discovery, automatic retries, message encryption (using mTLS), and improved observability. You'll use the Dapr .NET SDK to invoke the service invocation API on the Dapr sidecar.

    现在,你将使用 Dapr 服务调用构建基块 配置服务 之间的通信。你将允许 Web 应用从 Web API 检索天气预报。服务调用构建基块具有许多优点。它包括服务发现、自动重试、 (使用 mTLS) 的消息加密,并改进了可观察性。你将使用 Dapr .NET SDK 调用 Dapr 边车上的服务调用 API。

    1. In Visual Studio, open the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console) and make sure that DaprFrontEnd is the default project. From the console, add the Dapr.AspNetCore NuGet package to the project:      在 Visual Studio 中,打开包管理器控制台 (工具 > "NuGet 包管理器" > 程序包管理器控制台) ,并确保这 DaprFrontEnd 是默认项目。从控制台中,将 Dapr.AspNetCore NuGet 包添加到项目:

      Install-Package Dapr.AspNetCore

      Note

      If you're targeting a version of Dapr.AspNetCore that is in prerelease, you need to specify the -Prerelease flag.

      备注

      如果要以 Dapr.AspNetCore 的预发行版本为目标,则需要指定 -Prerelease 标志。

    2. In the DaprFrontEnd project, open the Startup.cs file, and replace the ConfigureServices method with the following code:      在 DaprFrontEnd 项目中,打开 Startup.cs 文件,并将 ConfigureServices 方法替换为以下代码:

      // This method gets called by the runtime. Use this method to add services to the container.
      public void ConfigureServices(IServiceCollection services)
      {services.AddControllers().AddDapr();services.AddRazorPages();
      }

      The call to AddDapr registers the DaprClient class with the ASP.NET Core dependency injection system. With the client registered, you can now inject an instance of DaprClient into your service code to communicate with the Dapr sidecar, building blocks, and components.      对 AddDapr 的调用会将DaprClient 类注册到 ASP.NET Core 依赖关系注入系统。在注册客户端的情况下,你现在可以将DaprClient 的实例注入到你的服务代码中,以便与 Dapr边车、构建块和组件通信。

    3. Add a new C# class file named WeatherForecast to the DaprFrontEnd project:      将名为 WeatherForecast 的新 c # 类文件添加到 DaprFrontEnd 项目:

      using System;namespace DaprFrontEnd
      {public class WeatherForecast{public DateTime Date { get; set; }public int TemperatureC { get; set; }public int TemperatureF { get; set; }public string Summary { get; set; }}
      } 
    4. Open the Index.cshtml.cs file in the Pages folder, and replace its contents with the following code:      打开 Pages 文件夹中 的 Index.cshtml.cs 文件,然后将其 内容替换为以下代码:

      using System;
      using System.Collections.Generic;
      using System.Net.Http;
      using System.Threading.Tasks;
      using Dapr.Client;
      using Microsoft.AspNetCore.Mvc.RazorPages;namespace DaprFrontEnd.Pages
      {public class IndexModel : PageModel{private readonly DaprClient _daprClient;public IndexModel(DaprClient daprClient){_daprClient = daprClient ?? throw new ArgumentNullException(nameof(daprClient));}public async Task OnGet(){var forecasts = await _daprClient.InvokeMethodAsync<IEnumerable<WeatherForecast>>(HttpMethod.Get,"daprbackend","weatherforecast");ViewData["WeatherForecastData"] = forecasts;}}
      }

      You add Dapr capabilities into the web app by injecting the DaprClient class into IndexModel constructor. In the OnGet method, you call the API service with the Dapr service invocation building block. The OnGet method is invoked whenever a user visits the home page. You use the DaprClient.InvokeMethodAsync method to invoke the weatherforecast method of the daprbackend service. You'll configure the web API to use daprbackend as its application ID later on when configuring it to run with Dapr. Finally, the service response is saved in view data.      通过将DaprClient 类注入IndexModel 的构造函数,可将 Dapr 功能添加到 web 应用中 。在 OnGet 方法中,通过 Dapr 服务调用构建块来调用 Web API 服务Daprbackend。 每当用户访问主页时,都会调用OnGet方法。使用 DaprClient.InvokeMethodAsync 方法来调用daprbackend 服务的 weatherforecast 方法 。稍后,将 web API 配置为使用Dapr,并将其应用程序Id设置为daprbackend。最后,将服务响应保存在ViewData中。

    5. Replace the contents of the Index.cshtml file in the Pages folder, with the following code. It displays the weather forecasts stored in the view data to the user:      用以下代码替换 Pages 文件夹中的Index.cshtml 文件的内容。它会将ViewData中存储的天气预报显示给用户:

    @page
    @model IndexModel
    @{ViewData["Title"] = "Home page";
    }<div class="text-center"><h1 class="display-4">Welcome</h1><p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>@foreach (var forecast in (IEnumerable<WeatherForecast>)ViewData["WeatherForecastData"]){<p>The forecast for @forecast.Date is @forecast.Summary!</p>}
    </div>

    Add container support

    添加容器支持

    In the final part of this example, you'll add container support and run the solution using Docker Compose.

    最后,你将添加容器支持,并使用 Docker Compose 运行解决方案。

    1. Right-click the DaprFrontEnd project, and choose Add > Container Orchestrator Support. The Add Container Orchestrator Support dialog appears:      右击 DaprFrontEnd 项目,然后选择 "添加 > 容器编排支持"。将显示 "添加容器编排支持 "对话框:

      Choose Docker Compose.      选择“Docker Compose”。

    2. In the next dialog, select Linux as the Target OS:      下一个对话框中,选择 "Linux" 作为"目标 OS":

      Visual Studio creates a docker-compose.ymlfile and a .dockerignore file in the docker-compose folder in the solution:      Visual Studio在解决方案中的 docker-compose 文件夹中创建 docker-compose.yml 文件和 .dockerignore 文件:

      The docker-compose.yml file has the following content:      docker-compose.yml 文件包含以下内容(不太清楚DOCKER_REGISTRY,它可能是表示docker注册中心的环境变量,如果没有设置此环境变量,则使用默认值(空字符串),待验证;image指令指定使用的镜像名称(和标签),context指令指定dockerfile文件所在的目录或一个git仓库,它可以是一个相对路径(相对于docker-compose.yml),dockerfile指定dockerfile文件(相对于context指定的路径)):

      version: "3.4"services:daprfrontend:image: ${DOCKER_REGISTRY-}daprfrontendbuild:context: .dockerfile: DaprFrontEnd/Dockerfile

      The .dockerignore file contains file types and extensions that you don't want Docker to include in the container. These files are associated with the development environment and source control and not the app or service you're deploying.      .dockerignore 文件包含你不希望 在Docker容器中包含的文件类型和扩展名。这些文件与开发环境和源代码管理相关联,而不是与要部署的应用或服务相关联。

      In the root of the DaprFrontEnd project directory, a new Dockerfile was created. A Dockerfile is a sequence of commands that are used to build an image. For more information, see Dockerfile reference.      在 DaprFrontEnd 项目的根目录中,创建了一个新的 Dockerfile。 Dockerfile 是一系列用于生成镜像的命令。有关详细信息,请参阅 Dockerfile 参考。

      The Dockerfile contains the YAML:      Dockerfile 包含如下内容:

      FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
      WORKDIR /app
      EXPOSE 80
      EXPOSE 443FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
      WORKDIR /src
      COPY ["DaprFrontEnd/DaprFrontEnd.csproj", "DaprFrontEnd/"]
      RUN dotnet restore "DaprFrontEnd/DaprFrontEnd.csproj"
      COPY . .
      WORKDIR "/src/DaprFrontEnd"
      RUN dotnet build "DaprFrontEnd.csproj" -c Release -o /app/buildFROM build AS publish
      RUN dotnet publish "DaprFrontEnd.csproj" -c Release -o /app/publishFROM base AS final
      WORKDIR /app
      COPY --from=publish /app/publish .
      ENTRYPOINT ["dotnet", "DaprFrontEnd.dll"]

      The preceding Dockerfile sequentially performs the following steps when invoked:      在调用上面 Dockerfile 时按顺序执行以下步骤(此处用到了docker的多阶段构建方式,最后生成的镜像,以最后一条 FROM 为准,之前的 FROM作为中间阶段将会被抛弃):

    • Targeting the Release configuration and outputs to /app/publish.      将发布配置和输出定位到 /app/publish

    • Targeting the Release configuration and outputs to /app/build.      将 发布配置和输出定位到 /app/build

    1. Pulls the mcr.microsoft.com/dotnet/aspnet:3.1 image and names it base.      拉取 mcr.microsoft.com/dotnet/aspnet:3.1 镜像,将其命名为 base 。

    2. Sets the working directory to /app.      将工作目录设置为 /app。WORKDIR指令指定后续指令的工作目录

    3. Exposes port 80 and 443.      声明窗口要公开 80 端口和 443端口,这些端口都是容器内部端口,非宿主端口 。

    4. Pulls the mcr.microsoft.com/dotnet/sdk:3.1 image and names it build.      拉取 mcr.microsoft.com/dotnet/sdk:3.1 镜像,将其命名为 build 。

    5. Sets the working directory to /src.      将工作目录设置为 /src

    6. Copies the DaprFrontEnd/DaprFrontEnd.csproj to a new directory named DaprFrontEnd/.      将 DaprFrontEnd/DaprFrontEnd.csproj 复制到名为 DaprFrontEnd/ 的新目录。COPY指令将Dockerfile所在目录中的文件拷贝到镜像中

    7. Calls dotnet restore on the project.      对项目调用 dotnet restore  。

    8. Copies everything from the root directory into the image's root.      将根目录中的所有内容复制到镜像的根目录中。

    9. Sets the working directory to /src/DaprFrontEnd.      将工作目录设置为 /src/DaprFrontEnd

    10. Calls dotnet build on the project.      对该项目调用 dotnet build 

    11. Initializes a new build stage from the existing build base image and names it publish.      从现有build镜像初始化新的构建阶段并命名为 publish 。

    12. Calls dotnet publish on the project.      对项目调用 dotnet publish

    13. Initializes a new build stage from the existing publish base image and names it final.      从现有base镜像初始化新的构建阶段并命名为 final 。

    14. Sets the working directory to /app.      将工作目录设置为 /app

    15. Copies the /app/publish directory from the publish image into the root of the final image.     从publish 镜像将 /app/publish 目录复制到final镜像的根目录 。

    16. Sets the entry point as the image to dotnet and passes the DaprFrontEnd.dll as an arg.      设置镜像的入口点为dotnet,并将 DaprFrontEnd.dll 作为参数传递。

  • In the DaprBackEnd web API project, right-click on the project node, and choose Add > Container Orchestrator Support. Choose Docker Compose, and then select Linux again as the target OS.      在 DaprBackEnd web API 项目中,右击项目,然后选择 "添加 > 容器 编排支持"。选择 " Docker Compose",然后再次选择 " Linux " 作为 "目标 OS"。

    In the root of the DaprBackEnd project directory, a new Dockerfile was created. The Dockerfile contains the following YAML:      在 DaprBackEnd 项目的根目录中,创建了一个新的 Dockerfile 。 Dockerfile 包含以下 YAML:

    FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
    WORKDIR /app
    EXPOSE 80
    EXPOSE 443FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
    WORKDIR /src
    COPY ["DaprBackEnd/DaprBackEnd.csproj", "DaprBackEnd/"]
    RUN dotnet restore "DaprBackEnd/DaprBackEnd.csproj"
    COPY . .
    WORKDIR "/src/DaprBackEnd"
    RUN dotnet build "DaprBackEnd.csproj" -c Release -o /app/buildFROM build AS publish
    RUN dotnet publish "DaprBackEnd.csproj" -c Release -o /app/publishFROM base AS final
    WORKDIR /app
    COPY --from=publish /app/publish .
    ENTRYPOINT ["dotnet", "DaprBackEnd.dll"]

    Open the docker-compose.yml file again and examine its contents. Visual Studio has updated the Docker Compose file. Now both services are included:      再次打开 docker-compose.yml docker-compose.override.yml 文件并检查其内容。Visual Studio 已更新 Docker Compose 文件。现在包括两个服务:

    version: '3.4'services:daprfrontend:image: ${DOCKER_REGISTRY-}daprfrontendbuild:context: .dockerfile: DaprFrontEnd/Dockerfiledaprbackend:image: ${DOCKER_REGISTRY-}daprbackendbuild:context: .dockerfile: DaprBackEnd/Dockerfile 
  • To use Dapr building blocks from inside a containerized application, you'll need to add the Dapr sidecars containers to your Compose file. Carefully update the content of the docker-compose.yml file to match the following example. Pay close attention to the formatting and spacing and don't use tabs.      若要在容器化应用程序中使用 Dapr 构建块,需要将 Dapr 边车容器添加到Compose文件中。仔细更新 docker-compose.yml 文件的内容,以匹配以下示例。请密切注意格式和间距,请勿使用tabs。

    version: '3.4'services:daprfrontend:image: ${DOCKER_REGISTRY-}daprfrontendbuild:context: .dockerfile: DaprFrontEnd/Dockerfileports:- "51000:50001"daprfrontend-dapr:image: "daprio/daprd:latest"command: [ "./daprd", "-app-id", "daprfrontend", "-app-port", "443", "-app-ssl" ]depends_on:- daprfrontendnetwork_mode: "service:daprfrontend"daprbackend:image: ${DOCKER_REGISTRY-}daprbackendbuild:context: .dockerfile: DaprBackEnd/Dockerfileports:- "52000:50001"daprbackend-dapr:image: "daprio/daprd:latest"command: [ "./daprd", "-app-id", "daprbackend", "-app-port", "443", "-app-ssl" ]depends_on:- daprbackendnetwork_mode: "service:daprbackend"

    In the updated file, we've added daprfrontend-dapr and daprbackend-dapr sidecars for the daprfrontend and daprbackend services respectively. In the updated file, pay close attention to the following changes:      在更新的文件中,我们分别为daprfrontend 和 daprbackend服务添加了 daprfrontend-dapr 和 daprbackend-dapr边车。在更新的文件中,请密切注意以下更改:

    • The sidecars use the daprio/daprd:latest container image. The use of the latest tag isn't recommended for production scenarios. For production, it's better to use a specific version number.      边车使用 daprio/daprd:latest 容器映像。不推荐在生产方案中使用 latest 标记。对于生产环境,最好使用特定的版本号。

    • Each service defined in the Compose file has its own network namespace for network isolation purposes. The sidecars use network_mode: "service:..." to ensure they run in the same network namespace as the application. Doing so allows the sidecar and the application to communicate using localhost.      出于网络隔离目的,Compose 文件中定义的每个服务都有自己的网络命名空间。边车使用 network_mode: "service:..." 来确保它们在与应用程序相同的网络命名空间中运行。这样做可让边车和应用程序使用localhost 进行通信 。

    • The ports on which the Dapr sidecars are listening for gRPC communication (by default 50001) must be exposed to allow the sidecars to communicate with each other.      Dapr 边车侦听 gRPC 通信的端口 (默认为 50001) ,使边车能够相互通信。ports指令使用"宿主(容器所在的主机,此处为wsl)端口:容器端口 (容器内部端口)(HOST:CONTAINER) "格式,或者仅仅指定容器的端口(宿主将会随机选择端口)都可以。

  • Run the solution (F5 or Ctrl+F5) to verify that it works as expected. If everything is configured correctly, you should see the weather forecast data:      运行解决方案 (F5 或 Ctrl+F5) 验证其是否正常工作。如果一切配置正确,应会看到天气预报数据:

    Running locally with Docker Compose and Visual Studio 2019, you can set breakpoints and debug into the application. For production scenarios, it's recommended to host your application in Kubernetes. This book includes an accompanying reference application, eShopOnDapr, that contains scripts to deploy to Kubernetes.      使用 Docker Compose 和 Visual Studio 2019 在本地运行,可以设置断点并在应用程序中调试。对于生产场景,建议在 Kubernetes 中托管应用程序。本书包含随附的参考应用程序 eShopOnDapr,其中包含要部署到 Kubernetes 的脚本。

    To learn more about the Dapr service invocation building block used in this walkthrough, refer to chapter 6.      若要详细了解本演练中使用的 Dapr 服务调用构建块,请参阅第 6 章。

  • Summary

    总结

    In this chapter, you had an opportunity to test drive Dapr. Using the Dapr .NET SDK, you saw how Dapr integrates with the .NET application platform.

    在本章中,你有机会尝试驾驭Dapr 。使用 Dapr .NET SDK,你已了解 Dapr 如何与 .NET 应用平台集成。

    The first example was a simple, stateful, .NET Console application that used the Dapr state management building block.

    第一个示例是使用 Dapr 状态管理构建块的简单有状态 .NET 控制台应用程序。

    The second example involved a multi-container application running in Docker. By using Visual Studio with Docker Compose, you experienced the familiar F5 debugging experience available across all .NET apps.

    第二个示例涉及在 Docker 中运行的多容器应用程序。通过结合使用Visual Studio 与 Docker Compose ,你可以在所有 .NET 应用中使用熟悉的 F5 调试体验 。

    You also got a closer look at Dapr component configuration files. They configure the actual infrastructure implementation used by the Dapr building blocks. You can use namespaces and scopes to restrict component access to particular environments and applications.

    你还可以更深入地了解 Dapr 组件配置文件。它们配置 Dapr 构建块使用的实际基础设施实现。你可以使用命名空间和范围限制特定环境和应用程序对组件的访问。

    In the upcoming chapters, you'll dive deep into the building blocks offered by Dapr.

    在即将推出的章节中,你将深入了解 Dapr 提供的构建块。

    目录:

    • 面向.NET开发人员的Dapr——前言

    • 面向.NET开发人员的Dapr——分布式世界

    • 面向.NET开发人员的Dapr——俯瞰Dapr

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/301387.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

比乐高还要好玩,50种奇妙装置玩法,将STEM教育一网打尽

▲数据汪特别推荐点击上图进入玩酷屋致砖《小小机器人》套装全新首发电动机械的完美结合先来看看视频过过眼瘾吧来自美国STEAM教育让孩子跨学科学知识积木向来是STEAM教育很重要的一部分&#xff0c;因为它涉及到了多种学科&#xff1a;要搭建得稳固——这是工程学&#xff1b;…

capslock键英语怎么读_刚开始读英文原版书该怎么做?商务英语专业有前途吗?...

是的&#xff0c;看我的朋友圈也能学英文#26从最近的朋友圈和微博中选了一些内容&#xff0c;分享给大家。1. 如何开始读英文原版书&#xff1f;关于读英文原版书&#xff0c;我给出一个简单粗暴的开始和进阶方案。启动方案&#xff1a;首先找一本你特别感兴趣的书&#xff0c;…

【12图】你管这破玩意叫Pulsar

这两年pulsar发展比较快&#xff0c;有好多大公司引入了pulsar&#xff0c;相关的资料和课程也多了&#xff0c;今天一起来了解一下pulsar这款中间件。下图是几款消息中间件的历史&#xff1a;2012年pulsar在Yahoo内部开发&#xff0c;2016年开源并捐献给Apache&#xff0c;201…

每日一笑 | 为什么英语老师让我们养成背单词的习惯?

全世界只有3.14 % 的人关注了数据与算法之美&#xff08;图片来源于网络&#xff0c;侵权删&#xff09;

把C#.NET程序移植到DB2上的经验浅谈(C#连接DB2可以用IBM.Data.DB2.dll)

感谢博客园不再封杀&#xff0c;差点儿搬家的念头都产生了&#xff0c;博客园还是有大胸怀&#xff0c;继续留在博客园写口水文应该没错&#xff0c;娱乐自己、娱乐大家&#xff0c;给枯燥的编程生活增加一点儿笑料&#xff0c;也给大家充当个开心果&#xff0c;让大家高兴一下…

postgis数据库优化_国内首个 Serverless 数据库来了,技术架构全揭秘!

头图 | CSDN 下载自东方 IC本文为企业投稿省却成本&#xff0c;缩短产品上市时间&#xff0c;减少运维与开发团队之间的摩擦是 Serverless 最核心的所在&#xff0c;从 AWS 发布「Lambda」让「Serverless」越来越多地为开发者所知到今天&#xff0c;已经过去了 6 年的时光&…

丰富自己, 比取悦他人更有力量

如果有那么一天&#xff0c;你不再记得&#xff0c;我也不再记得&#xff0c;时光一定会代替我们记得。丰富自己&#xff0c; 比取悦他人更有力量&#xff01;下面给大家推荐几个优秀公众号它们每天更新有态度、有干货的原创文字坚持看一段时间 颜值和人生都不一样了【关注方式…

WPF Grid动态显示或隐藏一列的一种方法

项目中有一个需求&#xff0c;需要根据用户的设置动态显示一列&#xff0c;研究了一波后&#xff0c;发现&#xff0c;Grid并没有这个功能&#xff0c;于是通过绑定宽度 的方法&#xff0c;实现的需求。。思路&#xff1a;将需要隐藏的列宽度 绑定到一个属性上&#xff0c;隐藏…

.Net Core with 微服务 - Seq 日志聚合

上一次我们介绍并演示了如果使用 Consul 做为我们微服务的注册中心&#xff0c;来实现服务的注册与发现。那么本次我们讲会演示如何做日志聚合。日志聚合比较常用的有 ELK 等&#xff0c;但是这次我想要介绍的是一款比较小众的日志聚合工具 - Seq 。日志聚合日志是我们写程序离…

苹果白屏一直显示苹果_最新消息显示:苹果还要发新品

距离2020年结束已经进入了倒计时阶段&#xff0c;但最新消息显示苹果可能还会在这个最后的时间节点上召开一场新品发布会。今日&#xff0c;有外媒援引一份苹果内部备忘录报道&#xff0c;称苹果通过内部备忘录向服务提供商发送通知&#xff0c;计划在美国太平洋标准时间 12月8…

“探月计划”来袭,美国米德天文望远镜助孩子观月赏月

▲数据汪特别推荐点击上图进入玩酷屋孩子对于未知的世界充满了好奇&#xff0c;尤其是对于月亮&#xff0c;总是有问不完的问题。例如月亮上是怎么样的&#xff1f;怎么很多人说十五的月亮十六圆&#xff1f;月亮有动物吗&#xff1f;不是说有月兔吗&#xff1f;月亮怎么有时是…

为什么async/await方法不能有lock或Monitor

相信有不少朋友知道async/await有lock语法的情况一般都会编译错误&#xff0c;这个时候可以通过Monitor来解决编译错误的问题&#xff1b;虽然这样解决的编译错误但本质问题还是没有解决。其实并不是async/await方法不能用lock或Monitor&#xff0c;而是在lock或Monitor的作用域…

每日一笑 | 程序员和产品经理打架了,怎么办?

全世界只有3.14 % 的人关注了数据与算法之美&#xff08;图片来源于网络&#xff0c;侵权删&#xff09;

新建项目EasyUiAutotest,安装Appium-Python-Client

一、前置说明 Appium-Python-Client 是 Appium 的 Python 客户端库&#xff0c;它提供了一系列的类和方法&#xff0c;用于与 Appium 服务器进行通信&#xff0c;并执行各种移动应用测试操作&#xff0c;包括启动应用、模拟用户输入、点击等操作。 二、操作步骤 1. 启动Pych…

VS Code + ML.NET 玩转交互式机器学习

在上一篇文章中&#xff0c;我们介绍了 .NET Interactive Notbook&#xff0c;可以进行代码实时运行和交互式展示。这对于ML.NET来说非常棒&#xff0c;可以运用到许多场景&#xff0c;比如数据探索、绘制数据图表、编制教学课件等。下面&#xff0c;我们就用出租车费预测&…

ts定义数组类型_ts基本数据类型

typescript基础数据类型布尔值 示例代码最基本的数据类型就是简单的true/false值&#xff0c;在JavaScript和TypeScript里叫做boolean(其它语言中也一样)。let a: boolean trueconsole.log(a)let b: boolean falseconsole.log(b)let c: booleanconsole.log(c)执行结果truefal…

神还原女神照片!GAN为百年旧照上色

全世界只有3.14 % 的人关注了数据与算法之美如何把女神的黑白照片变成彩照&#xff1f;今日&#xff0c;数据汪发现新加坡 GovTech 数据科学与人工智能部门在 Medium 上介绍了一个为百年旧照上色的项目。这个项目名为 Colourise.sg&#xff0c;最初是该团队&#xff08;成员包括…

cacti+nagios 整合遇到的问题

cacti安装成功了&#xff0c;下载一个npc插件来套用nagios&#xff0c;nagios要用ndoutils和cacti连接。主要安装ndoutils出现的问题。启动ndoutils时出错&#xff1a;/usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg错误提示&#xff1a;Support for th…

谈谈MySQL面试的45个常见问题

前两天&#xff0c;我跟一个面试官聊天&#xff0c;发现一个普遍想象&#xff0c;不少候选人&#xff0c;对数据库的认知&#xff0c;还处在比较基础的阶段&#xff0c;以为会写“增删改查”、做表关联就足够了&#xff0c;那些工作中经常出现的问题&#xff0c;却支支吾吾答不…

TensorFlow的这些骚操作你都知道吗?

TensorFlow Lite 2019 年发展蓝图分为四个关键部分&#xff1a;易用性、性能、优化和可移植性。自 2015 年 11 月 9 号发布之后&#xff0c;TensorFlow 逐渐成为人工智能领域最广泛运用的深度学习框架。那么TensorFlow框架到底是什么&#xff1f;TensorFlow 是一个大规模机器学…