创建第一个 Azure Service Fabric 应用程序Create your first Azure Service Fabric application

Service Fabric 提供用于在 Linux 上使用 .NET Core 和 Java 构建服务的 SDK。Service Fabric provides SDKs for building services on Linux in both .NET Core and Java. 本教程介绍如何在 .NET Core 2.0 中使用 C# 创建适用于 Linux 的应用程序和生成服务。In this tutorial, we look at how to create an application for Linux and build a service using C# on .NET Core 2.0.

必备条件Prerequisites

开始之前,请确保已设置 Linux 开发环境Before you get started, make sure that you have set up your Linux development environment. 如果使用的是 Mac OS X,则可以使用 Vagrant 在虚拟机中设置 Linux 单机环境If you are using Mac OS X, you can set up a Linux one-box environment in a virtual machine using Vagrant.

还需要安装 Service Fabric CLIYou will also want to install the Service Fabric CLI

为 C# 安装和设置生成器Install and set up the generators for C#

Service Fabric 提供基架工具,可以借助此类工具,使用 Yeoman 模板生成器从终端创建 Service Fabric 应用程序。Service Fabric provides scaffolding tools which help you create Service Fabric applications from a terminal using Yeoman template generators. 遵循以下步骤安装适用于 C# 的 Service Fabric Yeoman 模板生成器:Follow these steps to set up the Service Fabric Yeoman template generators for C#:

  1. 在计算机上安装 nodejs 和 NPMInstall nodejs and NPM on your machine

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash 
    nvm install node 
    
  2. 通过 NPM 在计算机上安装 Yeoman 模板生成器Install Yeoman template generator on your machine from NPM

    npm install -g yo
    
  3. 通过 NPM 安装 Service Fabric Yeoman C# 应用程序生成器Install the Service Fabric Yeoman C# application generator from NPM

    npm install -g generator-azuresfcsharp
    

创建应用程序Create the application

Service Fabric 应用程序可以包含一个或多个服务,每个服务都在提供应用程序功能时具有特定角色。A Service Fabric application can contain one or more services, each with a specific role in delivering the application's functionality. 用于 C# 的 Service Fabric Yeoman 生成器是在上一步安装的,利用它可以轻松地创建第一个服务,以及在以后添加其他服务。The Service Fabric Yeoman generator for C#, which you installed in last step, makes it easy to create your first service and to add more later. 让我们使用 Yeoman 创建包含单个服务的应用程序。Let's use Yeoman to create an application with a single service.

  1. 在终端中,键入以下命令开始构建基架: yo azuresfcsharpIn a terminal, type the following command to start building the scaffolding: yo azuresfcsharp

  2. 为应用程序命名。Name your application.

  3. 选择第一个服务的类型并将其命名。Choose the type of your first service and name it. 对于本教程,我们会选择“Reliable Actor 服务”。For the purposes of this tutorial, we choose a Reliable Actor Service.

    适用于 C# 的 Service Fabric Yeoman 生成器

备注

有关选项的详细信息,请参阅 Service Fabric 编程模型概述For more information about the options, see Service Fabric programming model overview.

构建应用程序Build the application

Service Fabric Yeoman 模板包含构建脚本,可用于从终端构建应用程序(在导航到应用程序文件夹后)。The Service Fabric Yeoman templates include a build script that you can use to build the app from the terminal (after navigating to the application folder).

cd myapp
./build.sh

部署应用程序Deploy the application

生成应用程序后,可以将其部署到本地群集。Once the application is built, you can deploy it to the local cluster.

  1. 连接到本地 Service Fabric 群集。Connect to the local Service Fabric cluster.

    sfctl cluster select --endpoint http://localhost:19080
    
  2. 运行模板中提供的安装脚本可将应用程序包复制到群集的映像存储区、注册应用程序类型和创建应用程序实例。Run the install script provided in the template to copy the application package to the cluster's image store, register the application type, and create an instance of the application.

    ./install.sh
    

部署生成的应用程序时,其方式与部署任何其他 Service Fabric 应用程序相同。Deploying the built application is the same as any other Service Fabric application. 如需详细的说明,请参阅相关文档,了解如何使用 Service Fabric CLI 管理 Service Fabric 应用程序See the documentation on managing a Service Fabric application with the Service Fabric CLI for detailed instructions.

这些命令的参数可以在应用程序包内的生成清单中找到。Parameters to these commands can be found in the generated manifests inside the application package.

应用程序部署完以后,请打开浏览器并导航到 Service Fabric Explorer,其地址为 http://localhost:19080/ExplorerOnce the application has been deployed, open a browser and navigate to Service Fabric Explorer at http://localhost:19080/Explorer. 然后,展开“应用程序” 节点,注意现在有一个条目是用于应用程序类型,另一个条目用于该类型的第一个实例。Then, expand the Applications node and note that there is now an entry for your application type and another for the first instance of that type.

重要

必须将证书配置为向 Service Fabric 运行时验证应用程序,才能将应用程序部署到 Azure 中的安全 Linux 群集。To deploy the application to a secure Linux cluster in Azure, you need to configure a certificate to validate your application with the Service Fabric runtime. 这样做可允许 Reliable Services 服务与基础 Service Fabric 运行时 API 通信。Doing so enables your Reliable Services services to communicate with the underlying Service Fabric runtime APIs. 若要了解详细信息,请参阅将 Reliable Services 应用程序配置为在 Linux 群集上运行To learn more, see Configure a Reliable Services app to run on Linux clusters.

启动测试客户端并执行故障转移Start the test client and perform a failover

执行组件项目没有任何属于自己的项。Actor projects do not do anything on their own. 它们需要其他服务或客户端发送消息给它们。They require another service or client to send them messages. 执行组件模板包含简单的测试脚本,可用于与执行组件服务交互。The actor template includes a simple test script that you can use to interact with the actor service.

  1. 使用监视实用工具运行脚本,查看执行组件服务的输出。Run the script using the watch utility to see the output of the actor service.

    对于 MAC OS X,你需要通过运行以下附加命令将 myactorsvcTestClient 文件夹复制到容器内的同一位置。In case of MAC OS X, you need to copy the myactorsvcTestClient folder into the same location inside the container by running the following additional commands.

    docker cp  [first-four-digits-of-container-ID]:/home
    docker exec -it [first-four-digits-of-container-ID] /bin/bash
    cd /home
    
    cd myactorsvcTestClient
    watch -n 1 ./testclient.sh
    
  2. 在 Service Fabric Explorer 中,找到托管执行组件服务主副本的节点。In Service Fabric Explorer, locate node hosting the primary replica for the actor service. 在以下屏幕截图中,该节点是节点 3。In the screenshot below, it is node 3.

    在 Service Fabric Explorer 中查找主副本

  3. 单击上一步找到的节点,并在“操作”菜单中选择“停用(重启)” 。Click the node you found in the previous step, then select Deactivate (restart) from the Actions menu. 此操作在本地群集中重新启动一个节点,从而强制故障转移到在另一个节点上运行的一个辅助副本。This action restarts one node in your local cluster forcing a failover to a secondary replica running on another node. 在执行此操作时,请注意来自测试客户端的输出,并注意虽然发生故障转移,但是计数器仍将继续递增。As you perform this action, pay attention to the output from the test client and note that the counter continues to increment despite the failover.

将更多服务添加到现有应用程序Adding more services to an existing application

要将另一个服务添加到使用 yo 创建的应用程序,请执行以下步骤:To add another service to an application already created using yo, perform the following steps:

  1. 将目录更改为现有应用程序的根目录。Change directory to the root of the existing application. 例如 cd ~/YeomanSamples/MyApplication(如果 MyApplication 是 Yeoman 创建的应用程序)。For example, cd ~/YeomanSamples/MyApplication, if MyApplication is the application created by Yeoman.
  2. 运行 yo azuresfcsharp:AddServiceRun yo azuresfcsharp:AddService

后续步骤Next steps