Build a Spring Boot microservice


In this module, we will build Spring Boot microservice that is cloud-enabled: it uses a Spring Cloud Service Registry and a Spring Cloud Config Server which are both managed and supported by Azure Spring Cloud.

This microservice will use Spring Data JPA to read and write data from an Azure database for MySQL database:

  • That database will be automatically bound to our service by Azure Spring Cloud.
  • Azure database for MySQL is a fully managed version of MySQL, running on Azure.

Create the application on Azure Spring Cloud

Create a specific todo-service application in your Azure Spring Cloud instance:

az spring-cloud app create -n todo-service

Create a MySQL database

Now create an Azure database for MySQL:

az mysql server create \
    --name ${SPRING_CLOUD_NAME}-mysql \
    --sku-name B_Gen5_1 \
    --storage-size 5120 \
    --admin-user "spring"

This operation can take a few minutes, and will output a JSON document: copy the password attribute in that document, as we will use it later.

Now create a todos database in that server, and open up its firewall so that Azure Spring Cloud can access it:

az mysql db create \
    --name "todos" \
    --server-name ${SPRING_CLOUD_NAME}-mysql

az mysql server firewall-rule create \
    --name ${SPRING_CLOUD_NAME}-mysql-allow-azure-ip \
    --server ${SPRING_CLOUD_NAME}-mysql \
    --start-ip-address "" \
    --end-ip-address ""

Once this operation completes, you can have a look at what was created in the resource group that you created for this workshop.

Bind the MySQL database to the application

Azure Spring Cloud can automatically bind the MySQL database we created to our microservice.

  1. Go to Apps in your Azure Spring Cloud instance.
  2. Select the todo-service application.
  3. Go to Service bindings.
  4. Click on Create service binding.
    1. Give your binding a name, for example mysql-todos.
    2. In the Binding type list, select Azure database for MySQL.
    3. The Resource name should be the one of the MySQL database you created earlier.
    4. The Database name should be todos
    5. The User name should be spring@YOUR_DATABASE_NAME, with YOUR_DATABASE_NAME being the name of your database, that we set up earlier as ${SPRING_CLOUD_NAME}-mysql when creating it.
    6. The Password is the password attribute that we copied earlier, when creating the server.

Select Create, and your Spring Boot application will have the MySQL database connection set up.

Create a Spring Boot microservice

Now that we've provisioned the Azure Spring Cloud instance and configured the service binding, let's get the code for todo-service ready.

To create our microservice, we will use with the command line:

curl -d dependencies=web,mysql,data-jpa,cloud-eureka,cloud-config-client -d baseDir=todo-service -d bootVersion=2.3.6.RELEASE -d javaVersion=1.8 | tar -xzvf -


We use the Spring Web, MySQL Driver, Spring Data JPA, Eureka Discovery Client and the Config Client components.

Add Spring code to manage data using Spring Data JPA

Next to the DemoApplication class, create a Todo JPA entity:

package com.example.demo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Todo {

    public Todo() {

    public Todo(String description, boolean done) {
        this.description = description;
        this.done = done;

    private Long id;

    private String description;

    private boolean done;

    public Long getId() {
        return id;

    public void setId(Long id) { = id;

    public String getDescription() {
        return description;

    public void setDescription(String description) {
        this.description = description;

    public boolean isDone() {
        return done;

    public void setDone(boolean done) {
        this.done = done;

Then, create a Spring Data JPA repository to manage this entity, called TodoRepository:

package com.example.demo;


public interface TodoRepository extends JpaRepository<Todo, Long> {

And finish coding this application by adding a Spring MVC controller called TodoController:

package com.example.demo;

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

import javax.annotation.PostConstruct;
import java.util.Arrays;

public class TodoController {

    private final TodoRepository todoRepository;

    public TodoController(TodoRepository todoRepository) {
        this.todoRepository = todoRepository;

    public void init() {
                new Todo("First item", true),
                new Todo("Second item", true),
                new Todo("Third item", false)));

    public Iterable<Todo> getTodos() {
        return todoRepository.findAll();

Configure Spring Boot to create the database tables

In order to automatically generate the database tables when the application is deployed, add this line to your src/main/resources/ configuration file:


Deploy the application

You can now build your "todo-service" project and send it to Azure Spring Cloud:

cd todo-service
./mvnw clean package -DskipTests
az spring-cloud app deploy -n todo-service --jar-path target/demo-0.0.1-SNAPSHOT.jar
cd ..

If you want to check the logs of the application, in case something fails, you can use the az spring-cloud app logs command:

az spring-cloud app logs -n todo-service -f

Test the project in the cloud

Now that the application is deployed, it's time to test it!

  1. In the Azure portal, go to Apps in your Azure Spring Cloud instance.
    1. Verify todo-service has a Registered Instance that says 0/1. This shows that it is correctly registered in the Spring Cloud Service Registry.
    2. Select todo-service to have more information on the microservice.
  2. Copy/paste the "Test Endpoint" that is provided.

You can now use cURL to test the endpoint. Your test command should look like:


And the result of this command should be the three items that were previously inserted in the MySQL database:

[{"id":"1","description":"First item","done":true},{"id":"2","description":"Second item","done":true},{"id":"3","description":"Third item","done":false}]

Check your knowledge


To deploy a Spring Boot microservice to Azure Spring Cloud, what do you need to change in your application?


By default, how can you access your microservice once it is deployed?