Skip to content

Latest commit

 

History

History
180 lines (113 loc) · 7.86 KB

File metadata and controls

180 lines (113 loc) · 7.86 KB

05 - Build a Spring Boot microservice using Spring Cloud features

This guide is part of the Azure Spring Apps training

In this section, we'll build a similar service to the one from section 2, but with the addition of two important Spring Cloud features. First, we'll add this service to Spring Cloud registry for discovery by other services. Second, we'll use Spring Cloud Config to inject a setting from a Git repository into the application and display it on the screen.


What we are going to build

This guide builds upon the previous guides: we are going to build again a simple Spring Boot microservice like in 02 - Build a simple Spring Boot microservice, but this time it will use two major Spring Cloud features:

  • It will be connected to a Spring Cloud Service Registry so it can discover other microservices, as well as being discovered itself!

  • It will get its configuration from the Spring Cloud Config server that we configured in the previous guide, 04 - Configure a Spring Cloud Config server

For both features, it will just be a matter of adding an official Spring Boot starter, and Azure Spring Apps will take care of everything else.

Create a simple Spring Cloud microservice

The microservice that we create in this guide is available here.

To create our microservice, we will invoke the Spring Initalizer service from the command line:

curl https://start.spring.io/starter.tgz -d type=maven-project -d dependencies=web,cloud-eureka,cloud-config-client -d baseDir=spring-cloud-microservice -d bootVersion=3.1.3 -d javaVersion=17 | tar -xzvf -

This time, we add the Eureka Discovery Client and the Config Client Spring Boot starters, which will respectively automatically trigger the use of Spring Cloud Service Registry and the Spring Cloud Config Server.

Add a new Spring MVC Controller

Next to the DemoApplication class, create a new class called HelloController with the following content:

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Value("${application.message:Not configured by Spring Cloud Config Server}")
    private String message;

    @GetMapping("/hello")
    public String hello() {
        return message + '\n';
    }
}

Configure Spring Boot

Edit the file src/main/resources/application.properties and add the following line:

spring.config.import=optional:configserver:

This line makes using the Spring Cloud Config server optional. This will be useful in development mode (when we won't have a Spring Cloud Config server), and can be removed later in production.

Test the project locally

Before deploying the microservice to Azure Spring Apps, let's run it locally.

💡 Do not be alarmed when you see exception stack traces: Exception stack trace Spring Cloud is attempting to contact a local configuration server, which we have not provided. The application will still start using any available local settings and defaults.

To run simple-cloud-microservice locally:

cd spring-cloud-microservice
./mvnw spring-boot:run &
cd ..

Requesting the /hello endpoint should return the "Not configured by Spring Cloud Server" message.

curl http://127.0.0.1:8080/hello

Kill the locally running microservice:

kill %1

Create and deploy the application on Azure Spring Apps

As in 02 - Build a simple Spring Boot microservice, create a specific spring-cloud-microservice application in your Azure Spring Apps instance:

az spring app create -n spring-cloud-microservice --runtime-version Java_17

You can now build your "spring-cloud-microservice" project and send it to Azure Spring Apps:

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

Test the project in the cloud

Go to the Azure portal:

  • Look for your Azure Spring Apps instance in your resource group
  • Go to "Apps"
    • Verify that spring-cloud-microservice has a Registration status of 1/1. This shows that it is correctly registered in Spring Cloud Service Registry.
    • Select spring-cloud-microservice to have more information on the microservice.
  • Copy/paste the "Test endpoint" that is provided.

You can now use cURL again to test the /hello endpoint, this time it is served by Azure Spring Apps and configured using the Spring Config Server from 04 - Configure a Spring Cloud Config server.

As a result, requesting the /hello endpoint should return the message that we configured in the application.yml file, coming from the Spring Cloud Config Server:

Configured by Azure Spring Apps

Stream application logs

When you run an application on your machine, you can see its output in the console. When you run a microservice on Azure Spring Apps, you can also see its console output through Azure CLI:

az spring app logs -n spring-cloud-microservice -f

Please be aware it might take a couple of minutes for the logs to show up.

You should see the console output of spring-cloud-microservice scroll by on your terminal:

Console output

Press CTRL+C to stop following the output and return to the shell.

Query application logs

Streaming the console output as we just did may be helpful in understanding the immediate state of a microservice. However, sometimes it's necessary to look further into the past or to look for something specific. This is easily done with Log Analytics. In section 3, we enabled log aggregation in Azure Log Analytics. Such settings changes can take 1-2 minutes to apply, so by now, you should be able to query Azure Log Analytics.

Open Azure Portal and navigate to your Azure Spring Apps instance. Click on "Logs". This is a shortcut to the Log Analytics workspace that was created earlier. If a tutorial appears, feel free to skip it for now.

This workspace allows you to run queries on the aggregated logs. The most common query is to get the latest log from a specific application:

Important: Spring Boot applications logs have a dedicated AppPlatformLogsforSpring type.

Here is how to get its 50 most recent logs of the AppPlatformLogsforSpring type for the microservice we just deployed:

Insert this text in the text area that states "Type your queries here or click on of the example queries to start". Click the text of the query, then click "Run".

AppPlatformLogsforSpring
| where AppName == "spring-cloud-microservice"
| project TimeGenerated, Log
| order by TimeGenerated desc
| limit 50

Query logs

💡 It can also take 1-2 minutes for the console output of an Azure Spring Apps microservice to be read into Log Analytics.

Conclusion

Congratulations, you have deployed a complete Spring Cloud microservice, using Spring Cloud Service Registry and Spring Cloud Config Server!

If you need to check your code, the final project is available in the "spring-cloud-microservice" folder.


⬅️ Previous guide: 04 - Configure a Spring Cloud Config server

➡️ Next guide: 06 - Build a reactive Spring Boot microservice using Cosmos DB