Microsoft Azure + Spring Cloud Sample application from the scratch
Objectives
In this post, we will:
- Set up an Azure Spring Cloud cluster
- Configure a Spring Cloud Config Server
- Use a Spring Cloud Discovery Server
- Configure service binding
- Create and deploy a Spring Boot microservice
- Create and deploy a Spring Cloud Gateway
Prerequisites
Create an Azure Spring Cloud instance
Configure a Spring Cloud Config Server
- Add the repository URL, for example
https://github.com/<YOUR_USERNAME>/azure-spring-cloud-config
orgit@github.com:<YOUR_USERNAME>/azure-spring-cloud-config.git
. - Add your branch in
Label
. It defaults tomain
on GitHub, but older repositories or alternate Git providers might still usemaster
. - Click on Authentication and select HTTP Basic.
- The username is your GitHub login name.
- The password is the personal token we created in the previous section.
todo-service
application in your Azure Spring Cloud instance: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
Bind mySql database to the application now:
Azure Spring Cloud can automatically bind the MySQL database we created to our microservice.
- Go to Apps in your Azure Spring Cloud instance.
- Select the todo-service application.
- Go to Service bindings.
- Click on Create service binding.
- Give your binding a name, for example mysql-todos.
- In the Binding type list, select Azure database for MySQL.
- The Resource name should be the one of the MySQL database you created earlier.
- The Database name should be todos
- 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.
- 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
src/main/resources/application.properties
configuration file:Now that the application is deployed, it's time to test it!
- In the Azure portal, go to Apps in your Azure Spring Cloud instance.
- Verify todo-service has a Registered Instance that says 0/1. This shows that it is correctly registered in the Spring Cloud Service Registry.
- Select todo-service to have more information on the microservice.
- Copy/paste the "Test Endpoint" that is provided.
You can now use cURL to test the endpoint. Your test command should look like:
curl https://primary:XXXXXXXXXXXXXXXXXXXXXXXXXXXXX@azure-spring-cloud-workshop.test.azuremicroservices.io/todo-service/default/
Build a Spring Cloud Gateway
Gateways are used to route public HTTP traffic to microservices:
- They handle the routing logic.
- They secure the access to the microservices (which will not be publicly available).
- They can also have Quality of Service (QoS) capabilities, like doing HTTP rate limiting.
Go to the project directory and copy followings to application.properties file:
spring.main.allow-bean-definition-overriding=true spring.cloud.gateway.discovery.locator.enabled=true
- The
spring.main.allow-bean-definition-overriding=true
part is to configure Spring Cloud Gateway to use the Spring Cloud Discovery Server bean configured in the Azure Spring Cloud Client library. - The
spring.cloud.gateway.discovery.locator.enabled=true
part is to configure Spring Cloud Gateway to use the Spring Cloud Service Registry to discover the available microservices.
todo-gateway
application in your Azure Spring Cloud instance. As this application is a gateway, we add the --assign-endpoint
flag so it is exposed publicly.Test the project in the cloud
- Go to Apps in your Azure Spring Cloud instance.
- Verify todo-gateway has a Registration status that says 1/1. This shows that it is correctly registered in the Spring Cloud Service Registry.
- Select todo-gateway to have more information on the microservice.
- Copy/paste the public URL that is provided (there is a "Test Endpoint" like for microservices, but the gateway is directly exposed on the Internet, so let's use the public URL). Keep this URL handy for subsequent sections.
As the gateway is connected to the Spring Cloud Service Registry, it should have automatically opened routes to the available microservices, with URL paths in the form of /MICROSERVICE-ID/**
: [The MICROSERVICE-ID must be in capital letters]
Test the todo-service
microservice endpoint by doing: curl https://XXXXXXXX-todo-gateway.azuremicroservices.io/TODO-SERVICE/
(replacing XXXXXXXX with the name of your Azure Spring Cloud instance)
Distributed tracing
We now have a complete microservices stack:
- A Spring Boot microservice, that stores its data in MySQL.
- A todo-gateway based on Spring Cloud Gateway.
However, even with only those two components, it already is quite challenging to monitor and study performance issues in our architecture.
To solve that issue, we're going to use Application Performance Monitoring (APM):
- Go to the Azure portal.
- Go to the overview page of your Azure Spring Cloud cluster and select Application Insights in the menu.
- This setting should already be on Enable.
- You'll have access to an application map, storing your tracing data.
Scale Spring Boot microservices
Spring Boot microservices and gateways running inside Azure Spring Cloud can be scaled vertically or horizontally:
- Vertical scaling means you increase (or decrease) the CPU and RAM of a given service.
- Horizontal scaling means you can add (or remove) nodes for a given service.
Scaling the "todo-service" microservice
- Go to the Azure portal.
- Go to the overview page of your Azure Spring Cloud cluster and select Apps in the menu.
- Select the todo-service application.
- Select Scale up in the left hand-side menu. You can now scale your service vertically, for example you can give it 2 CPUs and 4 Gb of RAM.
- Select Scale out in the left hand-side menu. You can also scale your service horizontally. For example, you can select your service and use Manual scale to have 2 instances of your service.
Comments
Post a Comment