In this section, we'll build an application that uses a Cosmos DB database in order to access a globally-distributed database with optimum performance.
We'll use the reactive programming paradigm to build our microservice in this section, leveraging the Spring reactive stack. In contrast, we'll build a more traditional data-driven microservice in the next section.
-
Navigate to your CosmosDB account named sclabc- in the resource group spring-apps-workshop-.
-
Click on the Data Explorer menu item
-
Expand the container named
azure-spring-apps-cosmosdb
. -
In that container, expand the container named
City
. -
Click on "Items" and use the "New Item" button to create some sample items and Save:
{ "name": "Paris, France" }
{ "name": "London, UK" }
-
Minimise the browser and navigate back to Git Bash.
-
To create our microservice, we will invoke the Spring Initializer service from the command line:
curl https://start.spring.io/starter.tgz -d type=maven-project -d dependencies=webflux,cloud-eureka,cloud-config-client -d baseDir=city-service -d bootVersion=2.7.5 -d javaVersion=17 | tar -xzvf -
Note: We use the
Spring Webflux
,Eureka Discovery Client
and theConfig Client
Spring Boot starters.
-
Navigate to the path
C:\Users\demouser\city-service
. In the application's right click onpom.xml
file and select open with visual studio. Then add the Cosmos DB dependency just after thespring-cloud-starter-netflix-eureka-client
dependency and Save.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>4.30.1</version>
</dependency>
- Navigate to the path
C:\Users\demouser\city-service\src\main\java\com\example\demo
. Next to theDemoApplication
class, create aCity.java
file:
package com.example.demo;
class City {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- Then, in the same location, create a new
CityController.java
file that contains the code that will be used to query the database.
The CityController class will get its Cosmos DB configuration from the Azure Spring Cloud service binding that we will configure later.
package com.example.demo;
import com.azure.cosmos.CosmosAsyncContainer;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.FeedResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import javax.annotation.PostConstruct;
import java.util.List;
@RestController
public class CityController {
@Value("${azure.cosmosdb.uri}")
private String cosmosDbUrl;
@Value("${azure.cosmosdb.key}")
private String cosmosDbKey;
@Value("${azure.cosmosdb.database}")
private String cosmosDbDatabase;
private CosmosAsyncContainer container;
@PostConstruct
public void init() {
container = new CosmosClientBuilder()
.endpoint(cosmosDbUrl)
.key(cosmosDbKey)
.buildAsyncClient()
.getDatabase(cosmosDbDatabase)
.getContainer("City");
}
@GetMapping("/cities")
public Flux<List<City>> getCities() {
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
return container.queryItems("SELECT TOP 20 * FROM City c", options, City.class)
.byPage()
.map(FeedResponse::getResults);
}
}
- As in exercise 2, create a specific
city-service
application in your Azure Spring Apps instance by running the below command in Git Bash.
Note: Replace the DID with the value, you can also find it from the Environment details page.
az spring app create -n city-service -g spring-apps-workshop-DID -s azure-spring-apps-lab-DID --runtime-version Java_17
Azure Spring Apps can automatically bind the Cosmos DB database we created to our microservice.
-
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
Apps
undersettings
. -
Select the
city-service
application -
Go to
Service bindings
underSettings
and click on+ Create service binding
. -
On
Create service binding
page, provide the following details.
- Give your binding a name, for example
cosmosdb-city
- Select the available
Subscription
in the drop down list - Select sclabc- as Resource name.
- Select the
Azure Cosmos DB
as theBinding type
andazure-spring-apps-cosmosdb
for the Database name we created and keep the defaultsql
API type - In the drop-down list, select the primary master key
- Click on
Create
to create the database binding
- Navigate to Git Bash and now you can build your city-service project and send it to Azure Spring Apps
cd city-service
./mvnw clean package -DskipTests
az spring app deploy -n city-service -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-.
-
Select
Apps
underSettings
. -
Verify that
city-service
has aRegistration status
which says1/1
. This shows that it is correctly registered in Spring Cloud Service Registry. -
Select
city-service
to have more information on the microservice. -
Click on see more under the overview blade, if the Test endpoint is not visible and copy the Test Endpoint that is provided.
-
Append
/cities
at the end. Now you can now use CURL to test the/cities
endpoint, and it should give you the list of cities you created. For example, if you only createdParis, France
andLondon, UK
like it is shown in this guide, you should get:[[{"name":"Paris, France"},{"name":"London, UK"}]]