In this exercise, we'll build a similar service to the one from exercise 2, but with the addition of two important Spring Apps features. First, we'll add this service to Spring Apps 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.
-
We are going to build again a simple Spring Boot microservice like in exercise 2, but this time it will use two major Spring Apps features:
-
It will be connected to a Spring Apps Service Registry so it can discover other microservices, as well as being discovered itself!
-
It will get its configuration from the Spring Apps Config server that we configured in exercise 4.
-
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.
-
Open Git bash from the start menu if not already opened.
-
Login to your Azure account by using the following command:
az login # Sign into an azure account
az account show # See the currently signed-in account
- 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=2.7.5 -d javaVersion=17 | tar -xzvf -
This time, we add the
Eureka Discovery Client
and theConfig Client
Spring Boot starters, which will respectively automatically trigger the use of Spring Apps Service Registry and the Spring Apps Config Server.
-
Now minimize the Git Bash window and navigate to the path
C:\Users\demouser\spring-cloud-microservice\src\main\java\com\example\demo
in File Explorer -
Open a notepad, then paste the below code in a new file:
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 a Spring Cloud Server}")
private String message;
@GetMapping("/hello")
public String hello() {
return message + '\n';
}
}
-
Save the file next to
DemoApplication.java
in theC:\Users\demouser\spring-cloud-microservice\src\main\java\com\example\demo
asHelloController.java
by changing the save as type to all files and then save as shown below. -
Navigate to the path
C:\Users\demouser\spring-cloud-microservice\src\main\resources\application.properties
and add the following line then save the file.
spring.config.import=optional:configserver:
-
Run the below command in Git Bash before deploying the microservice to Azure Spring Apps.
cd spring-cloud-microservice ./mvnw spring-boot:run & cd ..
💡 Do not be alarmed when you see exception stack traces: 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. When you receive above warning, you can press enter to make the process run in the background and proceed to next steps to access the endpoint. This is because control will be checking for the available port to host the endpoint.
-
Requesting the
/hello
endpoint should return the "Not configured by a Spring Cloud Server" message.curl http://127.0.0.1:8080/hello
-
Kill the locally running microservice:
kill %1
-
As in exercise 2, you create a specific
spring-cloud-microservice
application in your Azure Spring Apps instance by running the below command in Git BashNote: Replace the DID with value, you can also find it from the Environment details page.
az spring app create -n spring-cloud-microservice -g spring-apps-workshop-DID -s azure-spring-apps-lab-DID --runtime-version Java_17
-
Run the below command to 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 -g spring-apps-workshop-DID -s azure-spring-apps-lab-DID --artifact-path target/demo-0.0.1-SNAPSHOT.jar cd ..
Note: Replace the DID with the value, you can also find it from the Environment details page.
-
Navigate back to Azure Portal.
-
From the resource group spring-apps-workshop-. Select the Azure Spring Apps instance named azure-spring-apps-lab-.
-
Click on the Apps link under Settings on the navigation sidebar.
-
Verify that
spring-cloud-microservice
has aRegistration status
of1/1
. This shows that it is correctly registered in Spring Cloud Service Registry. -
Select
spring-cloud-microservice
to have more information on the microservice. -
Click on 'See more' to see Test Endpoint
-
Copy the Test Endpoint that is provided.
-
Append
hello/
to the URL. Failure to do this will result in a 404 not found. -
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 exercise 4. -
As a result, requesting the
/hello
endpoint should return the message that we configured in theapplication.yml
file, coming from the Spring Cloud Config Server:Configured by Spring Cloud Config Server
-
If successful, you should see the message:
Configured by Spring Cloud Config Server
.
-
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. Make sure to update the given DeploymentID in the below command.
az spring app logs -s azure-spring-apps-lab-DID -g spring-apps-workshop-DID --name spring-cloud-microservice -f
Note: Please be aware it might take a couple of minutes for the logs to show up.
Note: If you do not see the application logs appear, press CTRL+C to stop the stream. The logs should be visible now, scroll up to view the logs.
- Press CTRL+C to stop following the output and return to the shell.
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 exercise 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.
-
In the Azure Portal, search for Azure Spring Apps in the search box and select it.
-
Now click on the azure-spring-cloud-lab- resource from the list.
-
In the Azure Spring Apps resource pane, click on Logs under Monitoring. 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 the 50 most recent logs of the
AppPlatformLogsforSpring
type for the microservice we just deployed: -
Close other pop-up windows within the Logs and insert the below 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
💡 It can also take 1-2 minutes for the console output of an Azure Spring Apps microservice to be read into Log Analytics.
Congratulations, you have deployed a complete Spring Apps microservice, using Spring Apps Service Registry and Spring Apps Config Server!