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

快速入门:将 Java Spring Boot 应用程序部署到 Service FabricQuickstart: Deploy a Java Spring Boot application to Service Fabric

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

本快速入门演示如何将 Spring Boot 应用程序部署到 Service Fabric。This quickstart shows how to deploy a Spring Boot application to Service Fabric. 本快速入门使用 Spring 网站中的入门示例。This quickstart uses the Getting Started sample from the Spring website. 本快速入门逐步讲解如何使用熟悉的命令行工具,将 Spring Boot 示例部署为 Service Fabric 应用程序。Using familiar command-line tools, this quickstart walks you through deploying the Spring Boot sample as a Service Fabric application. 完成后,Spring Boot 入门示例将在 Service Fabric 上正常运行。When you're finished, you have the Spring Boot Getting Started sample working on Service Fabric.

应用程序屏幕截图

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

  • 将 Spring Boot 应用程序部署到 Service FabricDeploy a Spring Boot application to Service Fabric
  • 将应用程序部署到本地群集Deploy the application to your local cluster
  • 跨多个节点横向扩展应用程序Scale-out the application across multiple nodes
  • 在不影响可用性的情况下执行服务故障转移Perform failover of your service with no hit to availability

先决条件Prerequisites

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

  1. 安装 Service Fabric SDK 和 Service Fabric 命令行界面 (CLI)Install Service Fabric SDK & Service Fabric Command Line Interface (CLI)

    a.a. MacMac

    b.b. LinuxLinux

  2. 安装 GitInstall Git

  3. 安装 YeomanInstall Yeoman

    a.a. MacMac

    b.b. LinuxLinux

  4. 设置 Java 环境Set up Java Environment

    a.a. MacMac

    b.b. LinuxLinux

下载示例Download the sample

在终端窗口中运行以下命令,将 Spring Boot 入门示例应用克隆到本地计算机。In a terminal window, run the following command to clone the Spring Boot Getting Started sample app to your local machine.

git clone https://github.com/spring-guides/gs-spring-boot.git

生成 Spring Boot 应用程序Build the Spring Boot application

  1. gs-spring-boot/complete 目录中,运行以下命令以生成此应用程序。Inside the gs-spring-boot/complete directory, run the command below to build the application

    ./gradlew build
    

打包 Spring Boot 应用程序Package the Spring Boot application

  1. 在克隆的 gs-spring-boot 目录中运行 yo azuresfguest 命令。Inside the gs-spring-boot directory in your clone, run the yo azuresfguest command.

  2. 每次出现提示时,请输入以下详细信息。Enter the following details for each prompt.

    Yeoman 输入内容

  3. SpringServiceFabric/SpringServiceFabric/SpringGettingStartedPkg/code 文件夹中,创建名为 entryPoint.sh 的文件。In the SpringServiceFabric/SpringServiceFabric/SpringGettingStartedPkg/code folder, create a file called entryPoint.sh. 将以下内容添加到 entryPoint.sh 文件中。Add the following to the entryPoint.sh file.

    #!/bin/bash
    BASEDIR=$(dirname $0)
    cd $BASEDIR
    java -jar gs-spring-boot-0.1.0.jar
    
  4. gs-spring-boot/SpringServiceFabric/SpringServiceFabric/SpringGettingStartedPkg/ServiceManifest.xml 文件中添加终结点资源Add the Endpoints resource in the gs-spring-boot/SpringServiceFabric/SpringServiceFabric/SpringGettingStartedPkg/ServiceManifest.xml file

        <Resources>
          <Endpoints>
            <Endpoint Name="WebEndpoint" Protocol="http" Port="8080" />
          </Endpoints>
       </Resources>
    

    ServiceManifest.xml 现在如下所示:The ServiceManifest.xml now looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <ServiceManifest Name="SpringGettingStartedPkg" Version="1.0.0"
                     xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" >
    
       <ServiceTypes>
          <StatelessServiceType ServiceTypeName="SpringGettingStartedType" UseImplicitHost="true">
       </StatelessServiceType>
       </ServiceTypes>
    
       <CodePackage Name="code" Version="1.0.0">
          <EntryPoint>
             <ExeHost>
                <Program>entryPoint.sh</Program>
                <Arguments></Arguments>
                <WorkingFolder>CodePackage</WorkingFolder>
             </ExeHost>
          </EntryPoint>
       </CodePackage>
        <Resources>
          <Endpoints>
            <Endpoint Name="WebEndpoint" Protocol="http" Port="8080" />
          </Endpoints>
       </Resources>
     </ServiceManifest>
    

现已创建 Spring Boot 入门示例的 Service Fabric 应用程序,可将它部署到 Service Fabric。At this stage, you have created a Service Fabric application for the Spring Boot Getting Started sample that you can deploy to Service Fabric.

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

  1. 通过运行以下命令来启动 Ubuntu 计算机上的本地群集:Start your local cluster on Ubuntu machines by running the following command:

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

    如果使用 Mac,请从 Docker 映像启动本地群集(此处假定你已按照先决条件设置适用于 Mac 的本地群集)。If using a Mac, start the local cluster from the Docker image (this is assuming you have followed the prerequisites to set up your local cluster for Mac).

    docker run --name sftestcluster -d -p 19080:19080 -p 19000:19000 -p 25100-25200:25100-25200 -p 8080:8080 mysfcluster
    

    启动本地群集需要一些时间。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. 导航到 gs-spring-boot/SpringServiceFabric 文件夹。Navigate to the gs-spring-boot/SpringServiceFabric folder.

  3. 运行以下命令连接到本地群集。Run the following command to connect to your local cluster.

    sfctl cluster select --endpoint http://localhost:19080
    
  4. 运行 install.sh 脚本。Run the install.sh script.

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

    本地应用程序前端

现在可以访问已部署到 Service Fabric 群集的 Spring Boot 应用程序。You can now access the Spring Boot application that was deployed to a Service Fabric cluster.

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

可跨群集缩放服务来适应服务负载的变化。Services can be scaled across a cluster to accommodate for a change in 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 - 例如 http://localhost:19080Open Service Fabric Explorer in your cluster - for example, http://localhost:19080.

  2. 在树视图中单击“fabric:/SpringServiceFabric/SpringGettingStarted”节点旁边的省略号(三个点),选择“缩放服务”。Click on the ellipsis (three dots) next to the fabric:/SpringServiceFabric/SpringGettingStarted node in the treeview and choose Scale Service.

    Service Fabric Explorer 缩放服务

    现在可以选择缩放服务的实例数量。You can now choose to scale the number of instances of the service.

  3. 将数字更改为 3,单击“缩放服务”。Change the number to 3 and click Scale Service.

    下面显示了使用命令行缩放服务的另一种方法。An alternative way to scale the service using command line is as follows.

    # Connect to your local cluster
    sfctl cluster select --endpoint https://<ConnectionIPOrURL>:19080 --pem <path_to_certificate> --no-verify
    
    # Run Bash command to scale instance count for your service
    sfctl service update --service-id 'SpringServiceFabric~SpringGettingStarted' --instance-count 3 --stateless 
    
  4. 在树视图中单击“fabric:/SpringServiceFabric/SpringGettingStarted”节点,展开分区节点(由 GUID 表示)。Click on the fabric:/SpringServiceFabric/SpringGettingStarted node in the tree-view and expand the partition node (represented by a GUID).

    Service Fabric Explorer 缩放服务完成

    此服务有三个实例。树状视图显示实例在哪些节点上运行。The service has three instances, and the tree view shows 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.

故障转移群集中的服务Fail over services in a cluster

为了演示服务故障转移,可以使用 Service Fabric Explorer 来模拟节点重启。To demonstrate service failover, a node restart is simulated by using Service Fabric Explorer. 请确保只有一个服务实例在运行。Ensure only one instance of your service is running.

  1. 在群集中打开 Service Fabric Explorer - 例如 http://localhost:19080Open Service Fabric Explorer in your cluster - for example, http://localhost:19080.

  2. 单击运行服务实例的节点旁边的省略号(三个点),并重启节点。Click on the ellipsis (three dots) next to the node running the instance of your service and Restart the node.

    Service Fabric Explorer - 重启节点

  3. 服务实例已转移到另一个节点,且应用程序并未关闭。The instance of your service is moved to a different node and your application has no downtime.

    Service Fabric Explorer - 重启节点成功

后续步骤Next steps

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

  • 将 Spring Boot 应用程序部署到 Service FabricDeploy a Spring Boot application to Service Fabric
  • 将应用程序部署到本地群集Deploy the application to your local cluster
  • 跨多个节点横向扩展应用程序Scale-out the application across multiple nodes
  • 在不影响可用性的情况下执行服务故障转移Perform failover of your service with no hit to availability

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