Skip to content

Latest commit

 

History

History
110 lines (73 loc) · 5.05 KB

File metadata and controls

110 lines (73 loc) · 5.05 KB

10 - Blue/Green deployment

This guide is part of the Azure Spring Apps training

The blue-green deployment pattern allows you to test latest application changes on production infrastructure, but without exposing the changes to consumers until your testing is complete. In this section, we'll perform a blue-green deployment with Azure CLI. Although we will go through the deployment steps manually, the Azure CLI commands we'll use can be automated in a CI/CD pipeline.

🛑 This API is currently in transition. It's recommended that for the time being, you skip this guide and proceed directly to the next one: 11 - Configure CI/CD


We are going to deploy a new release of the "weather-service" microservice that was developed in 07 - Build a Spring Boot microservice using MySQL.

The microservice that we develop in this guide is available here, which is a slightly modified version of the service that was developed earlier.

Modify the current application

In the "weather-service" application, commit your current code and switch to a new branch where you will do your changes.

Open the WeatherController class and modify its getWeatherForCity() method so it always returns sunny weather (this will be an easy-to-spot graphical view of our modifications in the code):

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path="/weather")
public class WeatherController {

    private final WeatherRepository weatherRepository;

    public WeatherController(WeatherRepository weatherRepository) {
        this.weatherRepository = weatherRepository;
    }

    @GetMapping("/city")
    public Optional<Weather> getWeatherForCity(@RequestParam("name") String cityName) {
        return weatherRepository.findById(cityName).map(weather -> {
            weather.setDescription("It's always sunny on Azure Spring Apps");
            weather.setIcon("weather-sunny");
            return weather;
        });
    }
}

Deploy the new application to a new "green" deployment

Build a new version of the application and deploy it to a new deployment called green:

cd weather-service
./mvnw clean package -DskipTests
az spring app deployment create --name green --app weather-service --runtime-version Java_17 --artifact-path target/demo-0.0.1-SNAPSHOT.jar
cd ..

Once the application is deployed, if you go to https://spring-training.azureedge.net/ you will still have the same data, as the new version of the microservice is now in a staging area and not in production yet.

Navigate to the Azure Spring Apps instance in the Azure portal:

  • Look for your Azure Spring Apps instance in your resource group
  • Go to "Apps"
    • Select the weather-service microservice
    • Click on "Deployments" in the menu. You should now see the "green" deployment, under the "default" deployment:

Deployment Pane

You can test the green deployment by invoking the same URL as in section 7, but replacing the deployment name default with green:

curl "https://***.test.azuremicroservices.io/weather-service/green/weather/city?name=Paris%2C%20France"

And you should see the result of the recent modification:

{"city":"Paris, France","description":"It's always sunny on Azure Spring Apps","icon":"weather-sunny"}

Note: we're not testing the green deployment through the gateway application. The purpose of a green deployment is to test changes to a microservice before routing production traffic to it. Therefore, if you access weather-service through the public Gateway URL, as you did in section 8, you will be routed to the original version of the service.

To put this green deployment into production, you can use the command line:

az spring app set-deployment -n weather-service -d green

Another solution is to use the Azure portal:

  • Find your Azure Spring Apps instance
  • Click on the "Apps" menu
  • Select the weather-service application and click on "Deployments"

If you want to reuse a deployment name, you need first to delete the previous deployment under that name:

az spring app deployment delete --name green --app weather-service

Once you have swapped deployments and see that green is active, you need to wait a few seconds for the Spring Cloud Service Registry to synchronize and use this new version from the gateway application. You will then be able to see the new modified data:

Green deployment


⬅️ Previous guide: 09 - Putting it all together, a complete microservice stack

➡️ Next guide: 11 - Configure CI/CD