Quickstart: deploy a Java Spring Boot Application to Azure

Azure Service Fabric is a distributed systems platform for deploying and managing microservices and containers.

Using familiar command-line tools, this quickstart walks you through how to deploy a functional Spring Boot application to Service Fabric on a Mac or Linux developer machine using the Getting Started sample from the Spring website.

Application Screenshot

In this quickstart, you learn how to:

  • Deploy a Spring Boot application to Service Fabric
  • Deploy the application to your local cluster
  • Deploy the application to a cluster in Azure
  • Scale-out the application across multiple nodes
  • Perform failover of your service with no hit to availability

Prerequisites

To complete this quickstart:

  1. Install Service Fabric SDK & Service Fabric Command Line Interface (CLI)

    a. Mac

    b. Linux

  2. Install Git

  3. Install Yeoman

    a. Mac

    b. Linux

  4. Set up Java Environment

    a. Mac

    b. Linux

Download the sample

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

Build the Spring Boot application

  1. Inside the gs-spring-boot/complete directory, run the command below to build the application

    ./gradlew build
    

Package the Spring Boot application

  1. Inside the gs-spring-boot directory in your clone, run the yo azuresfguest command.

  2. Enter the following details for each prompt.

    Yeoman Entries

  3. In the SpringServiceFabric/SpringServiceFabric/SpringGettingStartedPkg/code folder, create a file called 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. 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>
    

    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="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://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>
    

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. Start your local cluster on Ubuntu machines by running the following command:

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

    If using a Mac, start the local cluster from the Docker image (this is assuming you have followed the preqrequisites 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. 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.

    Local cluster healthy

  2. 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. Run the install.sh script.

    ./install.sh
    
  5. Open your favorite web browser and access the application by accessing http://localhost:8080.

    Application front-end Local

You can now access the Spring Boot application that was deployed to a Service Fabric cluster.

Deploy the application to Azure

Set up your Azure Service Fabric Cluster

To deploy the application to a cluster in Azure, create your own cluster.

Party clusters are free, limited-time Service Fabric clusters hosted on Azure and run by the Service Fabric team. You can use party clusters to deploy applications and learn about the platform. The cluster uses a single, self-signed certificate for node-to-node and client-to-node security.

Sign in and join a Linux cluster. Download the PFX certificate to your computer by clicking the PFX link. Click the ReadMe link to find the certificate password and instructions about how to configure various environments to use the certificate. Keep both the Welcome page and the ReadMe page open, you will use some of the instructions in the following steps.

Note

There are a limited number of party clusters available per hour. If you get an error when you try to sign up for a party cluster, you can wait for a period and try again, or you can follow these steps in Create a Service Fabric cluster on Azure to create a cluster in your subscription.

The Spring Boot service is configured to listen on port 8080 for incoming traffic. Make sure that port is open in your cluster. If you are using the Party Cluster, this port is open.

Service Fabric provides several tools that you can use to manage a cluster and its applications:

  • Service Fabric Explorer, a browser-based tool.
  • Service Fabric Command Line Interface (CLI), which runs on top of Azure CLI 2.0.
  • PowerShell commands.

In this quickstart, you use the Service Fabric CLI and Service Fabric Explorer.

To use the CLI, you need to create a PEM file based on the PFX file you downloaded. To convert the file, use the following command. (For party clusters, you can copy a command specific to your PFX file from the instructions on the ReadMe page.)

openssl pkcs12 -in party-cluster-1486790479-client-cert.pfx -out party-cluster-1486790479-client-cert.pem -nodes -passin pass:1486790479

To use Service Fabric Explorer, you need to import the certificate PFX file you downloaded from the Party Cluster website into your certificate store (Windows or Mac) or into the browser itself (Ubuntu). You need the PFX private key password, which you can get from the ReadMe page.

Use whatever method you are most comfortable with to import the certificate on your system. For example:

  • On Windows: Double-click the PFX file and follow the prompts to install the certificate in your personal store, Certificates - Current User\Personal\Certificates. Alternatively, you can use the PowerShell command in the ReadMe instructions.
  • On Mac: Double-click the PFX file and follow the prompts to install the certificate in your Keychain.
  • On Ubuntu: Mozilla Firefox is the default browser in Ubuntu 16.04. To import the certificate into Firefox, click the menu button in the upper right corner of your browser, then click Options. On the Preferences page, use the search box to search for "certificates". Click View Certificates, select the Your Certificates tab, click Import and follow the prompts to import the certificate.

    Install certificate on Firefox

Deploy the application using CLI

Now that the application and your cluster are ready, you can deploy it to the cluster directly from command line.

  1. Navigate to the gs-spring-boot/SpringServiceFabric folder.
  2. Run the following command to connect to your Azure cluster.

    sfctl cluster select --endpoint https://<ConnectionIPOrURL>:19080 --pem <path_to_certificate> --no-verify
    
  3. Run the install.sh script.

    ./install.sh
    
  4. Open your web browser and access the application by accessing: http://<ConnectionIPOrUrl>:8080.

    Application front-end Local

You can now access the Spring Boot application running in a Service Fabric cluster on Azure.

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. There are many ways of scaling your services, for example, you can use scripts or commands from Service Fabric CLI (sfctl). The following steps use Service Fabric Explorer.

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); for example, http://localhost:19080.

To scale the web front-end service, do the following:

  1. Open Service Fabric Explorer in your cluster - for example, http://localhost:19080.
  2. Click on the ellipsis (three dots) next to the fabric:/SpringServiceFabric/SpringGettingStarted node in the treeview and choose Scale Service.

    Service Fabric Explorer Scale Service

    You can now choose to scale the number of instances of the service.

  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. Click on the fabric:/SpringServiceFabric/SpringGettingStarted node in the tree-view and expand the partition node (represented by a GUID).

    Service Fabric Explorer Scale Service Complete

    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. If a service fails, Service Fabric makes sure that a new service instance runs in the cluster.

Fail over services in a cluster

To demonstrate service failover, a node restart is simulated by using Service Fabric Explorer. Ensure only one instance of your service is running.

  1. Open 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 Restart Node

  3. The instance of your service is moved to a different node and your application has no downtime.

    Service Fabric Explorer Restart Node Succeed

Next steps

In this quickstart, you learned how to:

  • Deploy a Spring Boot application to Service Fabric
  • Deploy the application to your local cluster
  • Deploy the application to a cluster in Azure
  • Scale-out the application across multiple nodes
  • Perform failover of your service with no hit to availability

To learn more about working with Java apps in Service Fabric, continue to the tutorial for Java apps.