快速入门:将 Java 应用部署到 Linux 上的 Azure Service FabricQuickstart: Deploy a Java app to Azure Service Fabric on Linux

在本快速入门中,你将在 Linux 开发人员计算机上使用 Eclipse IDE 将 Java 应用程序部署到 Azure Service Fabric。In this quickstart, you deploy a Java application to Azure Service Fabric using the Eclipse IDE on a Linux developer machine. 完成后,将生成一个带 Java Web 前端的投票应用程序,用于将投票结果保存到群集的有状态后端服务中。When you're finished, you have a voting application with a Java web front end that saves voting results in a stateful back-end service in the cluster.

Azure Service Fabric 是一款分布式系统平台,可用于部署和管理微服务和容器。Azure Service Fabric is a distributed systems platform for deploying and managing microservices and containers.

先决条件Prerequisites

下载示例Download the sample

在命令窗口中,运行以下命令,将示例应用程序存储库克隆到本地计算机。In a command window, run the following command to clone the sample app repository to your local machine.

git clone https://github.com/Azure-Samples/service-fabric-java-quickstart.git

在本地运行应用程序Run the application locally

  1. 通过运行以下命令来启动本地群集:Start your local cluster by running the following command:

    sudo /opt/microsoft/sdk/servicefabric/common/clustersetup/devclustersetup.sh
    

    启动本地群集需要一些时间。The startup of the local cluster takes some time. 若要确认群集已完全启动,请访问 Service Fabric Explorer(网址:http://localhost:19080)。To confirm that the cluster is fully up, access the Service Fabric Explorer at http://localhost:19080. 5 个节点均正常即表示本地群集运行正常。The five healthy nodes indicate the local cluster is up and running.

    Azure Service Fabric Explorer 显示运行正常的节点

  2. 打开 Eclipse。Open Eclipse.

  3. 选择“文件” > “导入” > “Gradle” > “现有 Gradle 项目” ,并按照向导进行操作。Select File > Import > Gradle > Existing Gradle Project and follow the wizard.

  4. 选择“目录” ,然后在从 GitHub 克隆的 service-fabric-java-quickstart 文件夹中选择 Voting 目录。Select Directory and choose the Voting directory from the service-fabric-java-quickstart folder you cloned from GitHub. 选择“完成” 。Select Finish.

    将 Gradle 项目导入 Eclipse

  5. Eclipse 的包资源管理器中现拥有 Voting 项目。You now have the Voting project in the Package Explorer for Eclipse.

  6. 右键单击该项目并选择“Service Fabric”下拉列表中的“发布应用程序” 。Right-click on the project and select Publish Application under the Service Fabric dropdown. 选择“PublishProfiles/Local.json”作为目标配置文件,然后选择“发布” 。Choose PublishProfiles/Local.json as the Target Profile and select Publish.

    Azure Service Fabric 发布本地 JSON

  7. 打开喜欢的 Web 浏览器并访问应用程序(网址:http://localhost:8080)。Open your favorite web browser and access the application by accessing http://localhost:8080.

    Azure Service Fabric 本地主机

现在可以添加一组投票选项,并开始进行投票。You can now add a set of voting options, and start taking votes. 此应用程序可以运行,并将所有数据存储到 Service Fabric 群集中,而无需单独提供数据库。The application runs and stores all data in your Service Fabric cluster, without the need for a separate database.

Azure Service Fabric 投票示例

在群集中缩放应用程序和服务Scale applications and services in a cluster

可跨群集缩放服务来适应服务负载的变化。Services can be scaled across a cluster to accommodate for a change in the load on the services. 可以通过更改群集中运行的实例数量来缩放服务。You scale a service by changing the number of instances running in the cluster. 有许多方法可以缩放服务。There are many ways of scaling your services. 例如,可以通过 Service Fabric CLI (sfctl) 使用脚本或命令。For example, you can use scripts or commands from Service Fabric CLI (sfctl). 以下步骤使用 Service Fabric Explorer。The following steps use Service Fabric Explorer.

Service Fabric Explorer 在所有 Service Fabric 群集中运行,并且可以通过浏览到群集的 HTTP 管理端口 (19080) 从浏览器进行访问。Service Fabric Explorer runs in all Service Fabric clusters and can be accessed from a browser by browsing to the cluster's HTTP management port (19080). 例如,http://localhost:19080For example, http://localhost:19080.

若要缩放 Web 前端服务,请执行以下操作:To scale the web front-end service, do the following:

  1. 在群集中打开 Service Fabric Explorer。Open Service Fabric Explorer in your cluster. 例如,https://localhost:19080For example, https://localhost:19080.

  2. 在树视图中选择“fabric:/Voting/VotingWeb”节点旁边的省略号 ( ... ),再选择“缩放服务” 。Select the ellipsis ( ... ) next to the fabric:/Voting/VotingWeb node in the treeview and select Scale Service.

    在 Azure Service Fabric 中缩放服务

    现在可以缩放 Web 前端服务的实例数量。You can now choose to scale the number of instances of the web front-end service.

  3. 将数字更改为 2 ,选择“缩放服务” 。Change the number to 2 and select Scale Service.

  4. 在树视图中选择“fabric:/Voting/VotingWeb” 节点,再展开分区节点(由 GUID 表示)。Select the fabric:/Voting/VotingWeb node in the tree-view and expand the partition node (represented by a GUID).

    在 Azure Service Fabric 中缩放服务

    现在可以看到,服务有两个实例。在树视图中可以查看实例的运行节点。You can now see that the service has two instances, and in the tree view you see which nodes the instances run on.

通过这一简单的管理任务,你已让前端服务用来处理用户负载的资源数量翻了一番。Through this simple management task, you've doubled the resources available for the front-end service to process user load. 有必要了解的是,服务无需多个实例便能可靠运行。It's important to understand that you don't need multiple instances of a service for it to run reliably. 如果服务出现故障,Service Fabric 可确保在群集中运行新的服务实例。If a service fails, Service Fabric makes sure that a new service instance runs in the cluster.

后续步骤Next steps

在此快速入门中,读者学习了如何:In this quickstart, you learned how to:

  • 使用 Eclipse 处理 Service Fabric Java 应用程序Use Eclipse as a tool for your Service Fabric Java applications
  • 将 Java 应用程序部署到本地群集Deploy Java applications to your local cluster
  • 跨多个节点横向扩展应用程序Scale-out the application across multiple nodes

若要详细了解如何在 Service Fabric 中使用 Java 应用,请继续学习适用于 Java 应用的教程。To learn more about working with Java apps in Service Fabric, continue to the tutorial for Java apps.