This guide is part of the Azure Spring Cloud training
In this section, we'll build a simple Spring boot microservice and deploy it to Azure Spring Cloud. This will give us a starting point for adding Spring Cloud technologies in later sections.
The microservice that we create in this guide is available here.
A typical way to create Spring Boot applications is to use the Spring Initializer at https://start.spring.io/. Feel free to explore it outside this training. For the purposes of this training, we will only invoke the Spring Initializer site via the curl
command.
💡 Note: All subsequent commands in this workshop should be run from the same directory, except where otherwise indicated via
cd
commands.
In an empty directory execute the curl command line below:
curl https://start.spring.io/starter.tgz -d dependencies=web -d baseDir=simple-microservice -d bootVersion=2.3.8 -d javaVersion=1.8 | tar -xzvf -
We force the Spring Boot version to be 2.3.8.
In the simple-microservice/src/main/java/com/example/demo
directory, create a
new file called HelloController.java
next to DemoApplication.java
file with
the following content:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello from Azure Spring Cloud\n";
}
}
The final project is available in the "simple-microservice" folder.
Run the project:
cd simple-microservice
./mvnw spring-boot:run &
cd ..
Requesting the /hello
endpoint should return the "Hello from Azure Spring Cloud" message.
curl http://127.0.0.1:8080/hello
Finally, kill running app:
kill %1
This section shows how to create an app instance and then deploy your code to it.
In order to create the app instance graphically, you can use the Azure portal:
- Look for your Azure Spring Cloud instance in your resource group
- Click on the "Apps" link under "Settings" on the navigation sidebar.
- Click on "Create App" link at the top of the Apps page.
- Create a new application named "simple-microservice"
- Click on "Create".
Alternatively, you can use the command line to create the app instance, which is easier:
az spring-cloud app create -n simple-microservice
You can now build your "simple-microservice" project and deploy it to Azure Spring Cloud:
cd simple-microservice
./mvnw clean package
az spring-cloud app deploy -n simple-microservice --jar-path target/demo-0.0.1-SNAPSHOT.jar
cd ..
This creates a jar file on your local disk and uploads it to the app instance you created in the preceding step. The az
command will output a result in JSON. You don't need to pay attention to this output right now, but in the future, you will find it useful for diagnostic and testing purposes.
Go to the Azure portal:
- Look for your Azure Spring Cloud instance in your resource group
- Click "Apps" in the "Settings" section of the navigation pane and select "simple-microservice"
- Click on 'See more' to see "Test Endpoint"
- Mouse over the URL labeled as "Test Endpoint" and click the clipboard icon that appears.
This will give you something like:https://primary:BBQM6nsYnmmdQREXQINityNx63kWUbjsP7SIvqKhOcWDfP6HJTqg27klMLaSfpTB@rwo1106f.test.azuremicroservices.io/simple-microservice/default/
💡 Note the text between
https://
and@
. These are the basic authentication credentials, without which you will not be authorized to access the service. - 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 served by Azure Spring Cloud. For example.
curl https://primary:...simple-microservice/default/hello/
If successful, you should see the message: Hello from Azure Spring Cloud
.
Congratulations, you have deployed your first Spring Boot microservice to Azure Spring Cloud!
If you need to check your code, the final project is available in the "simple-microservice" folder.
Here is the final script to build and deploy everything that was done in this guide:
curl https://start.spring.io/starter.tgz -d dependencies=web -d baseDir=simple-microservice -d bootVersion=2.3.8 -d javaVersion=1.8 | tar -xzvf -
cd simple-microservice
cat > HelloController.java << EOF
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello from Azure Spring Cloud";
}
}
EOF
mv HelloController.java src/main/java/com/example/demo/HelloController.java
az spring-cloud app create -n simple-microservice
./mvnw clean package
az spring-cloud app deploy -n simple-microservice --jar-path target/demo-0.0.1-SNAPSHOT.jar
⬅️ Previous guide: 01 - Create an Azure Spring Cloud instance
➡️ Next guide: 03 - Configure monitoring