您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:将 Java Reliable Services 应用程序部署到 Service FabricQuickstart: Deploy a Java reliable services application to Service Fabric

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

此快速入门演示如何在 Linux 开发人员计算机上使用 Eclipse IDE 将首个 Java 应用程序部署到 Service Fabric。This quickstart shows how to deploy your first Java application to 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.

应用程序屏幕截图

此快速入门介绍如何:In this quickstart, you learn how to:

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

先决条件Prerequisites

完成本快速入门教程:To complete this quickstart:

  1. 安装 Service Fabric SDK 和 Service Fabric 命令行接口 (CLI)Install Service Fabric SDK & Service Fabric Command Line Interface (CLI)
  2. 安装 GitInstall Git
  3. 安装 EclipseInstall Eclipse
  4. 设置 Java 环境,确保遵循可选步骤安装 Eclipse 插件Set up Java Environment, making sure to follow the optional steps to install the Eclipse plug-in

下载示例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.

    本地群集正常运行

  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.

    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.

    本地“发布”对话框

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

    本地应用程序前端

现在可以添加一组投票选项,并开始进行投票。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.

在群集中缩放应用程序和服务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. 存在多种服务缩放方式,例如,可使用 Service Fabric CLI (sfctl) 脚本/命令。There are many ways of scaling your services; 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),例如,http://localhost:19080Service 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); for example, http://localhost:19080.

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

  1. 在群集中打开 Service Fabric Explorer - 例如 https://localhost:19080Open Service Fabric Explorer in your cluster - for 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.

    Service Fabric Explorer 缩放服务

    现在可以缩放 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).

    Service Fabric Explorer 缩放服务完成

    现在可以看到,服务有两个实例。在树视图中可以查看实例的运行节点。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.