Alibaba Cloud Account: Spring Cloud Project Deployment Tutorial Based on Alibaba Cloud Native Microservice Architecture
There are many tutorials on the market that talk about microservice deployment. As soon as you come up, you will be allowed to build Nginx, Nacos, Sentinel, Zipkin... on the server. After a set of combined punches, the server memory will burst. Not to mention, various configuration files can kill people.
In the cloud native era, it is too inefficient to play like this. Aliyun has made all the microservice bases (registration center, configuration center, gateway, current limiting and downgrading)
Cloud Native Managed Services
.
Today's tutorial does not talk about complicated architecture theory or nonsense. We directly take a standard
Spring Cloud Microservices Project
For example, take you to use the most grounded actual combat mode,
Based on the native architecture of Alibaba Cloud (MSE ACK/SAE), the deployment and operation of microservices can be completed from scratch.
.
Core Architecture Design
Before we get started, take a look at the cloud-native microservice deployment topology we want to implement:
client request]
│
▼
[Alibaba Cloud Native Gateway (MSE)]-(Auto Discovery Service)-> [MSE Nacos]]
│ │
├──> [Micro Service A (Lightweight Application/SAE)]
│ │
But-> [microservice B (lightweight application/SAE)]
The traditional approach is to install Nacos and Gateway on ECS, while the cloud native approach is to directly use Alibaba Cloud.
of/’s
MSE (Microservice Engine)
and
SAE(Serverless Application Engine)
. In this way, you do not need to maintain the bottom layer of the server, but also achieve second-level elastic scaling.
Step 1: Microservice Code Transformation (Adapted to Cloud Native)
To move a Spring Cloud project running locally to the cloud, first modify the configuration file to connect it to Alibaba Cloud's managed Nacos.
1. Introducing Dependencies
Make sure your
pom.xml
Standard Spring Cloud Alibaba dependencies are introduced in:
XML
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2. Modify the configuration file
Locally, we usually write the Nacos address as dead.
localhost:8848
. But in cloud native deployments, we
Don't write any IP path in the code.
. Use environment variables directly for dynamic injection:
YAML
# bootstrap.yml or application.yml
spring:
application:
name: order-service# your service name
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER_ADDR:localhost:8848}# Read environment variables first
namespace: ${NACOS_NAMESPACE:public}
config:
server-addr: ${NACOS_SERVER_ADDR:localhost:8848}
file-extension: yaml
💡Why is it written like this? With ${variable name: default value}, if you do not pass the environment variable during local development, you can still go localhost; when you go to the cloud, the cloud platform will automatically help us inject the real Alibaba Cloud cluster address to realize the code "one set of compilation, running everywhere".
Step 2: Activate and configure MSE Nacos on Alibaba Cloud (1 minute)
Now let's go to Aliyun to handle the "brain" of microservices-registration center and configuration center.
Log on to the Alibaba Cloud console and search for Microservice Engine (MSE) ".
Click Instance Management $\rightarrow $Create Instance and select Nacos ".
Version selection: Select the latest stable version (such as Nacos 2.x). The cluster specification is selected according to the business volume. For personal tests or small projects, you can select the development test type (single node). For the production environment, you must select the professional type (3-node high availability).
After the instance is created, go to the instance details page. You will see two core network addresses: intranet address (similar to mse-xxxx-nacos-ctl.tbls.com:8848) and public network address (if public network debugging needs to be opened, it is recommended to only use intranet for safety in the production environment)
Record this intranet address, which is the variable we will give to the microservice next.
Step 3: Deploy microservice components using SAE (4 minutes)
There are many microservice components. If you use the traditional K8s(ACK) to deploy, you need to write a large number of YAML files and understand concepts such as Pod and Deployment.
It is recommended to use a simpler
SAE (Serverless Application Engine)
, it saves you the pain of maintaining K8s cluster and can run by uploading Jar package or mirror directly.
1. Package the project
Execute the Maven command in the local project root directory to package the microservice into a standard Jar package:
Bash
mvn clean package -DskipTests
Take it and pack it up.
order-service.jar
.
2. Create an application in SAE
Go to the Serverless Application Engine SAE console.
Click "Application List" $\rightarrow $"Create Application".
Application deployment method: Select JAR package deployment (if you are familiar with Docker, you can also select image deployment).
Runtime Environment: Select the appropriate JDK version (for example, Java 8 or Java 11/17).
Upload the order-service.jar you just packed.
3. Inject environment variables (core step)
In the deployment page
"Advanced Settings"
In, find.
"Environment Variables"
Options. Remember the space we left in the first step of the code
A bit? make it up here:
Key (key)
Value
NACOS_SERVER_ADDR
Fill in the MSE Nacos intranet address copied in step 2
NACOS_NAMESPACE
If you create a new namespace in Nacos, fill in the ID, if not, fill in public.
4. Start the application
Click Confirm Create and Deploy. SAE automatically schedules computing resources, pulls up containers, and runs JAR packages for you in the background. Wait for 1 to 2 minutes and see that the status changes to "running", indicating that the service has successfully run in the cloud.
You can go back and log in
MSE Nacos Console
, refresh in the "service management" list, you can see
order-service
has been successfully registered up!
Step 4: Configure the cloud native gateway for external access (2 minutes)
Microservices are running in the intranet, but how can users access the extranet? We need a unified entrance, that is, a microservices gateway. Alibaba Cloud MSE provides its own Cloud Native Gateway, which perfectly replaces the traditional Spring Cloud Gateway.
Go to the MSE console $\rightarrow $Cloud Native Gateway $\rightarrow $Gateway instance list and click Create.
Associate the MSE Nacos instance you just created.
After the gateway is created, go to the gateway details page and click Service Source $\rightarrow $Add Service Source on the left ". Select MSE Nacos as the source type, and then select your Nacos instance.
Click "Route Configuration" $\rightarrow $"Create Route" on the left: Route name: for example, order-route. Match path: For example, enter/order/. Target service: directly select the order-service you registered in Nacos from the drop-down menu.
Click Save and Publish.
In this case, access the public IP address and path provided by the cloud native gateway (for example, https:// gateway IP address/order/create)
The gateway will automatically sense the microservice node in Nacos and forward the request perfectly.
Practical Pit-Avoidance and Advanced Tuning Guide
White screen/unable to register? Check the network security group first! MSE Nacos and SAE applications must be in the same VPC (Virtual Private Cloud) and the same VSwitch to communicate with each other through the intranet. If you find that the microservice in SAE reports a crazy error "Connection Nacos Timeout", immediately check whether the VPC attributes of the two products are consistent.
The problem of graceful offline of microservices is that microservices are most afraid of causing users who are accessing to report errors when releasing a new version. Alibaba Cloud SAE comes with the function of graceful offline (lossless offline). original
Li: Before the microservice is destroyed, SAE will actively send a logout signal to MSE Nacos, so that the gateway will not transfer new traffic, and then close the container after the old request is processed. How to use it: In the advanced settings of SAE application, turn on the "microservice lossless offline" switch without modifying the code at all.
Memory Overflow (OOM) Error Many novices confuse JVM memory with system memory. If you have purchased a 1-core 2G instance in SAE, do not write-Xmx2g in the JVM parameter, because the system itself needs memory to run. Golden Rule: The maximum heap memory (-Xmx) of the JVM should be set to about 60% ~ 70% of the instance specification. For example, for a 2G instance, we recommend that you set the JVM parameter to-Xmx1300m.
Congratulations! At this point, a highly available, automatically scalable cloud-native microservice system has been completely run through. Say goodbye to the tedious middleware operations, you can put all your energy into the writing of business code.

