Quickstart: Deploy your first Azure Spring Cloud application

This quickstart explains how to deploy a simple Azure Spring Cloud microservice application to run on Azure.

Note

Steeltoe support for Azure Spring Cloud is currently offered as a public preview. Public preview offerings allow customers to experiment with new features prior to their official release. Public preview features and services are not meant for production use. For more information about support during previews, see the FAQ or file a Support request.

By following this quickstart, you'll learn how to:

  • Generate a basic Steeltoe .NET Core project
  • Provision an Azure Spring Cloud service instance
  • Build and deploy the app with a public endpoint
  • Stream logs in real time

The application code used in this quickstart is a simple app built with a .NET Core Web API project template. When you've completed this example, the application will be accessible online and can be managed via the Azure portal and the Azure CLI.

Prerequisites

Install Azure CLI extension

Verify that your Azure CLI version is 2.0.67 or later:

az --version

Install the Azure Spring Cloud extension for the Azure CLI using the following command:

az extension add --name spring-cloud

Log in to Azure

  1. Log in to the Azure CLI

    az login
    
  2. If you have more than one subscription, choose the one you want to use for this quickstart.

    az account list -o table
    
    az account set --subscription <Name or ID of a subscription from the last step>
    

Generate a Steeltoe .NET Core project

In Visual Studio, create an ASP.NET Core Web application named as "hello-world" with API project template. Please notice there will be a auto generated WeatherForecastController which will be our test endpoint later on.

  1. Create a folder for the project source code and generate the project.

    mkdir source-code
    
    cd source-code
    
    dotnet new webapi -n hello-world --framework netcoreapp3.1
    
  2. Navigate into the project directory.

    cd hello-world
    
  3. Edit the appSettings.json file to add the following settings:

    "spring": {
      "application": {
        "name": "hello-world"
      }
    },
    "eureka": {
      "client": {
        "shouldFetchRegistry": true,
        "shouldRegisterWithEureka": true
      }
    }
    
  4. Also in appsettings.json, change the log level for the Microsoft category from Warning to Information. This change ensures that logs will be produced when you view streaming logs in a later step.

    The appsettings.json file now looks similar to the following example:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Information",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
      "spring": {
        "application": {
          "name": "hello-world"
        }
      },
      "eureka": {
        "client": {
          "shouldFetchRegistry": true,
          "shouldRegisterWithEureka": true
        }
      }
    }
    
  5. Add dependencies and a Zip task to the .csproj file:

    <ItemGroup>
      <PackageReference Include="Steeltoe.Discovery.ClientCore" Version="2.4.4" />
      <PackageReference Include="Microsoft.Azure.SpringCloud.Client" Version="1.0.0-preview.1" />
    </ItemGroup>
    <Target Name="Publish-Zip" AfterTargets="Publish">
        <ZipDirectory SourceDirectory="$(PublishDir)" DestinationFile="$(MSBuildProjectDirectory)/deploy.zip" Overwrite="true" />
    </Target>
    

    The packages are for Steeltoe Service Discovery and the Azure Spring Cloud client library. The Zip task is for deployment to Azure. When you run the dotnet publish command, it generates the binaries in the publish folder, and this task zips the publish folder into a .zip file that you upload to Azure.

  6. In the Program.cs file, add a using directive and code that uses the Azure Spring Cloud client library:

    using Microsoft.Azure.SpringCloud.Client;
    
    public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    })
                    .UseAzureSpringCloudService();
    
  7. In the Startup.cs file, add a using directive and code that uses the Steeltoe Service Discovery at the end of the ConfigureServices and Configure methods:

    using Steeltoe.Discovery.Client;
    
    public void ConfigureServices(IServiceCollection services)
    {
        // Template code not shown.
    
        services.AddDiscoveryClient(Configuration);
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // Template code not shown.
    
        app.UseDiscoveryClient();
    }
    
  8. Build the project to make sure there are no compile errors.

    dotnet build
    

Provision a service instance

The following procedure creates an instance of Azure Spring Cloud using the Azure portal.

  1. Open the Azure portal.

  2. From the top search box, search for Azure Spring Cloud.

  3. Select Azure Spring Cloud from the results.

    ASC icon start

  4. On the Azure Spring Cloud page, select + Add.

    ASC icon add

  5. Fill out the form on the Azure Spring Cloud Create page. Consider the following guidelines:

    • Subscription: Select the subscription you want to be billed for this resource.
    • Resource group: Create a new resource group. The name you enter here will be used in later steps as <resource group name>.
    • Service Details/Name: Specify the <service instance name>. The name must be between 4 and 32 characters long and can contain only lowercase letters, numbers, and hyphens. The first character of the service name must be a letter and the last character must be either a letter or a number.
    • Region: Select the region for your service instance.

    ASC portal start

  6. Select Review and create.

Build and deploy the app

The following procedure builds and deploys the project that you created earlier.

  1. Make sure the command prompt is still in the project folder.

  2. Run the following command to build the project, publish the binaries, and store the binaries in a .zip file in the project folder.

    dotnet publish -c release -o ./publish
    
  3. Create an app in your Azure Spring Cloud instance with a public endpoint assigned. Use the same application name "hello-world" that you specified in appsettings.json.

    az spring-cloud app create -n hello-world -s <service instance name> -g <resource group name> --is-public
    
  4. Deploy the .zip file to the app.

    az spring-cloud app deploy -n hello-world -s <service instance name> -g <resource group name> --runtime-version NetCore_31 --main-entry hello-world.dll --artifact-path ./deploy.zip
    

    The --main-entry option identifies the .dll file that contains the application's entry point. After the service uploads the .zip file, it extracts all the files and folders and tries to execute the entry point in the .dll file specified by --main-entry.

    It takes a few minutes to finish deploying the application. To confirm that it has deployed, go to the Apps blade in the Azure portal.

Test the app

Once deployment has completed, access the app at the following URL:

https://<service instance name>-hello-world.azuremicroservices.io/weatherforecast

The app returns JSON data similar to the following example:

[{"date":"2020-09-08T21:01:50.0198835+00:00","temperatureC":14,"temperatureF":57,"summary":"Bracing"},{"date":"2020-09-09T21:01:50.0200697+00:00","temperatureC":-14,"temperatureF":7,"summary":"Bracing"},{"date":"2020-09-10T21:01:50.0200715+00:00","temperatureC":27,"temperatureF":80,"summary":"Freezing"},{"date":"2020-09-11T21:01:50.0200717+00:00","temperatureC":18,"temperatureF":64,"summary":"Chilly"},{"date":"2020-09-12T21:01:50.0200719+00:00","temperatureC":16,"temperatureF":60,"summary":"Chilly"}]

Stream logs in real time

Use the following command to get real time logs from the App.

az spring-cloud app logs -n hello-world -s <service instance name> -g <resource group name> --lines 100 -f

Logs appear in the output:

[Azure Spring Cloud] The following environment variables are loaded:
2020-09-08 20:58:42,432 INFO supervisord started with pid 1
2020-09-08 20:58:43,435 INFO spawned: 'event-gather_00' with pid 9
2020-09-08 20:58:43,436 INFO spawned: 'dotnet-app_00' with pid 10
2020-09-08 20:58:43 [Warning] No managed processes are running. Wait for 30 seconds...
2020-09-08 20:58:44,843 INFO success: event-gather_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2020-09-08 20:58:44,843 INFO success: dotnet-app_00 entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
←[40m←[32minfo←[39m←[22m←[49m: Steeltoe.Discovery.Eureka.DiscoveryClient[0]
      Starting HeartBeat
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:1025
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /netcorepublish/6e4db42a-b160-4b83-a771-c91adec18c60
2020-09-08 21:00:13 [Information] [10] Start listening...
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 GET http://asc-svc-hello-world.azuremicroservices.io/weatherforecast
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'hello_world.Controllers.WeatherForecastController.Get (hello-world)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "Get", controller = "WeatherForecast"}. Executing controller action with signature System.Collections.Generic.IEnumerable`1[hello_world.WeatherForecast] Get() on controller hello_world.Controllers.WeatherForecastController (hello-world).
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      Executing ObjectResult, writing value of type 'hello_world.WeatherForecast[]'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action hello_world.Controllers.WeatherForecastController.Get (hello-world) in 1.8902ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'hello_world.Controllers.WeatherForecastController.Get (hello-world)'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 4.2591ms 200 application/json; charset=utf-8

Tip

Use az spring-cloud app logs -h to explore more parameters and log stream functionalities.

For advanced log analytics features, visit Logs tab in the menu on Azure portal. Logs here have a latency of a few minutes. Logs Analytics

This quickstart explains how to deploy a simple Azure Spring Cloud microservice application to run on Azure.

The application code used in this tutorial is a simple app built with Spring Initializr. When you've completed this example, the application will be accessible online and can be managed via the Azure portal.

This quickstart explains how to:

  • Generate a basic Spring Cloud project
  • Provision a service instance
  • Build and deploy the app with a public endpoint
  • Stream logs in real time

Prerequisites

To complete this quickstart:

Generate a Spring Cloud project

Start with Spring Initializr to generate a sample project with recommended dependencies for Azure Spring Cloud. The following image shows the Initializr set up for this sample project.

https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.3.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=hellospring&name=hellospring&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.hellospring&dependencies=web,cloud-eureka,actuator,cloud-starter-sleuth,cloud-starter-zipkin

Initializr page

  1. Click Generate when all the dependencies are set. Download and unpack the package, then create a web controller for a simple web application by adding src/main/java/com/example/hellospring/HelloController.java as follows:

    package com.example.hellospring;
    
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @RestController
    public class HelloController {
    
    	@RequestMapping("/")
    	public String index() {
    		return "Greetings from Azure Spring Cloud!";
    	}
    
    }
    

Provision an instance of Azure Spring Cloud

The following procedure creates an instance of Azure Spring Cloud using the Azure portal.

  1. In a new tab, open the Azure portal.

  2. From the top search box, search for Azure Spring Cloud.

  3. Select Azure Spring Cloud from the results.

    ASC icon start

  4. On the Azure Spring Cloud page, click + Add.

    ASC icon add

  5. Fill out the form on the Azure Spring Cloud Create page. Consider the following guidelines:

    • Subscription: Select the subscription you want to be billed for this resource.
    • Resource group: Creating new resource groups for new resources is a best practice. This will be used in later steps as <resource group name>.
    • Service Details/Name: Specify the <service instance name>. The name must be between 4 and 32 characters long and can contain only lowercase letters, numbers, and hyphens. The first character of the service name must be a letter and the last character must be either a letter or a number.
    • Location: Select the region for your service instance.

    ASC portal start

  6. Click Review and create.

Build and deploy the app

The following procedure builds and deploys the application using the Azure CLI. Execute the following command at the root of the project.

  1. Build the project using Maven:

    mvn clean package -DskipTests
    
  2. (If you haven't already installed it) Install the Azure Spring Cloud extension for the Azure CLI:

    az extension add --name spring-cloud
    
  3. Create the app with public endpoint assigned:

    az spring-cloud app create -n hellospring -s <service instance name> -g <resource group name> --is-public
    
  4. Deploy the Jar file for the app:

    az spring-cloud app deploy -n hellospring -s <service instance name> -g <resource group name> --jar-path target\hellospring-0.0.1-SNAPSHOT.jar
    
  5. It takes a few minutes to finish deploying the application. To confirm that it has deployed, go to the Apps blade in the Azure portal. You should see the status of the application.

Once deployment has completed, you can access the app at https://<service instance name>-hellospring.azuremicroservices.io/.

Access app from browser

Streaming logs in real time

Use the following command to get real time logs from the App.

az spring-cloud app logs -n hellospring -s <service instance name> -g <resource group name> --lines 100 -f

Logs appear in the results:

Streaming Logs

Tip

Use az spring-cloud app logs -h to explore more parameters and log stream functionalities.

For advanced logs analytics features, visit Logs tab in the menu on Azure portal. Logs here have a latency of a few minutes.

Logs Analytics

Clean up resources

In the preceding steps, you created Azure resources that will continue to accrue charges while they remain in your subscription. If you don't expect to need these resources in the future, delete the resource group from the portal or by running the following command in the Azure CLI:

az group delete --name <your resource group name; for example: hellospring-1558400876966-rg> --yes

Next steps

In this quickstart, you learned how to:

  • Generate a basic Azure Spring Cloud project
  • Provision a service instance
  • Build and deploy the app with a public endpoint
  • Stream logs in real time

To learn how to use more Azure Spring capabilities, advance to the quickstart series that deploys a sample application to Azure Spring Cloud:

More samples are available on GitHub: Azure Spring Cloud Samples.