快速入門:將 Java 可靠服務應用程式部署至 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. 若要確認該叢集完全啟動,請存取位於 http://localhost:19080 的 Service Fabric Explorer。To confirm that the cluster is fully up, access the Service Fabric Explorer at http://localhost:19080. 五個狀況良好的節點表示本機叢集已啟動並執行。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. 開啟您最愛的網頁瀏覽器,並存取 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.