Environment Setup
Spring Cloud Alibaba 2022 requires Spring Boot 3.0+ and JDK 17 or higher. Check the official documentation for compatibility details.
The project structure uses a Maven multi-module setup with the following parent pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-alibaba-demo</name>
<description>spring-cloud-alibaba-demo</description>
<packaging>pom</packaging>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.0.2</spring-boot.version>
<spring-cloud-alibaba.version>2022.0.0.0-RC2</spring-cloud-alibaba.version>
<spring-cloud.version>2022.0.0</spring-cloud.version>
</properties>
<modules>
<module>consumerservice01</module>
<module>providerservice01</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.springcloudalibabademo.SpringCloudAlibabaDemoApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Nacos Overview and Installation
Nacos stands for Dynamic Naming and Configuration Service. It provides dynamic service discovery, configuration management, and service management capabilities for building cloud-native applications.
Download and run Nacos Server, then access the login page at port 8848. The default username and password are both nacos.
Service Registration and Discovery
To enable service registration and discovery, add the Nacos discovery dependency to each module's pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>consumerservice01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumerservice01</name>
<description>consumerservice01</description>
<parent>
<artifactId>spring-cloud-alibaba-demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
Create an application.yml file to configure the Nacos Server address:
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: consumerservice01
server:
port: 8890
After starting the consumer service, it appears in the service list under Nacos management console.
Unlike Eureka, Nacos does not require the @EnableDiscoveryClient annotation on the main application class.
Nacos Service Tiered Storage Model
The storage model consists of three levels:
- Level 1: Service (e.g., consumerservice01)
- Level 2: Cluster
- Level 3: Instance
spring:
cloud:
nacos:
discovery:
cluster-name: SH
Restart the application to see the cluster name displayed.
Environment Isolation
Namespace
Namespaces provide tenant-level configuration isolation. The same Group or Data ID can exist in different namespaces.
Use case: Typically used for environment isolation, such as separating test and production environments.
Group
Groups provide a secondary level of isolation beneath namespaces.
Use case: Used to differentiate between projects when multiple projects share a single Nacos instance.
Key points:
- Services can only communicate with each other when they are in the same namespace and same group
- Services registered to Nacos belong to the
publicnamespace andDEFAULT_GROUPby default
Configure namespace and group in application.yml:
spring:
cloud:
nacos:
discovery:
namespace: 8ff552f0-0212-43b2-8ea0-e4c2aa359084
group: testgroup1
Load Balancing
Enable load balancing in application.yml:
spring:
cloud:
loadbalancer:
nacos:
enabled: true
Adjust instance weights in the Nacos console—higher weights increase the probability of that instance being selected. The implementation details are in the NacosLoadBalancer class.
Nacoss Configuration Center
The configuration center solves the coupling problem between microservices and configuration files. Without it, modifying configuration across multiple service instances would require updating numerous files and restarting services.
A challenge arises: the application.yml file is loaded after the application starts, but connection to Nacos requires knowing the Nacos Server address beforehand.
The solution involves using a bootstrap.yml file, which loads during application startup before the main configuration. This allows the Nacos Server address to be specified early in the bootstrap process. Spring Cloud Config follows the same principle.
Add the Nacos configuration client dependencies:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.1</version>
</dependency>
Create bootstrap.yml with the configuration:
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
namespace: 8ff552f0-0212-43b2-8ea0-e4c2aa359084
group: DEFAULT_GROUP
application:
name: consumerservice
profiles:
active: dev
Create a corresponding configuration file in the Nacos console matching the service name, namespace, and group settings.
Configuration Hot Reload
Two approaches exist for hot reloading:
- Add the
@RefreshScopeannotation to classes using@Valueinjection - Use the
@ConfigurationPropertiesannotation instead of@Value
Multi-Environment Configuration Sharing
Configuration files created in the Nacos console support profile-based organization and shared configuration across environments when properly grouped.