SAE's built-in service registry is a commercial version of Nacos. When you deploy an application to SAE, it automatically configures the Nacos Server address, service port, namespace, AccessKey pair, and context path—no manual configuration needed. This guide shows how to build service gateways using Spring Cloud Gateway and Spring Cloud Netflix Zuul with Nacos as the service registry.
SAE service registry advantages
Compared with Nacos, Eureka, and Consul, the SAE service registry provides the following advantages:
Shared components: Reduces the costs of deploying, managing, and maintaining Nacos, Eureka, or Consul.
Encrypted communication: Service registry and discovery calls are encrypted to protect your services from unauthorized access.
SAE integration: Integrated with other SAE components to provide a full range of microservice solutions, including environment isolation and canary release.
Version compatibility
Choose your dependency versions based on the Spring Cloud version you use:
| Spring Cloud version | Spring Cloud Alibaba version |
|---|---|
| Greenwich | 2.1.1.RELEASE |
| Finchley | 2.0.1.RELEASE |
| Edgware | 1.5.1.RELEASE |
Spring Cloud Edgware has reached end of life. Use Greenwich or Finchley for new applications.
The examples in this guide use Spring Cloud Greenwich with Spring Cloud Alibaba 2.1.1.RELEASE.
How it works
Both Spring Cloud Gateway and Zuul integrate with Nacos through the spring-cloud-starter-alibaba-nacos-discovery dependency. When a gateway starts, it registers itself with the Nacos service registry. Incoming requests are matched against routing rules and forwarded to backend services by their Nacos-registered service name using client-side load balancing (lb://).
The request flow is:
Client sends a request to the gateway.
The gateway matches the request path against defined route predicates.
The gateway resolves the backend service name from the Nacos registry.
The gateway applies filters (such as
StripPrefix) and forwards the request to the backend service.
When you deploy to SAE, the registry connection details are automatically overridden. You can retain or delete your existing local Nacos configuration.
Prerequisites
Before you begin, ensure that you have:
A local Nacos Server running at
127.0.0.1:8848(for local verification)Maven installed
Java 8 or later
Build a gateway with Spring Cloud Gateway
Step 1: Create the Maven project
Create a Maven project named spring-cloud-gateway-nacos and add the following to pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>Step 2: Create the startup class
Create a class named GatewayApplication:
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}Step 3: Configure the gateway
Add the following to application.yaml. The routes section defines how the gateway forwards requests: all requests prefixed with /provider1/ are routed to the service-provider backend, with the /provider1 prefix stripped before forwarding.
Replace 127.0.0.1:8848 with your Nacos Server address if it runs on a different host.
server:
port: 18012
spring:
application:
name: spring-cloud-gateway-nacos
cloud:
gateway:
routes:
- id: service-provider
uri: lb://service-provider # Route to service-provider via load balancing
predicates:
- Path=/provider1/**
filters:
- StripPrefix=1 # Strip the /provider1 prefix before forwarding
nacos:
discovery:
server-addr: 127.0.0.1:8848Step 4: Create a service provider
The gateway needs a backend service to forward requests to. For a complete example, see Migrate Spring Cloud applications to SAE.
A minimal service provider looks like this:
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
@RestController
public class EchoController {
@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
public String echo(@PathVariable String string) {
return string;
}
}
}Verify the gateway
Local verification
Start both the gateway and the service provider, then send a test request:
curl http://127.0.0.1:18012/provider1/echo/helloExpected output:
helloThe gateway strips the /provider1 prefix and forwards /echo/hello to the service-provider backend.
To confirm the gateway registered with Nacos, log in to the Nacos console at http://127.0.0.1:8848/nacos (default credentials: nacos/nacos). Go to Service Management > Services and verify that spring-cloud-gateway-nacos appears in the list. You can also view detailed service information by clicking Details.
Verify in SAE
Deploy the application to SAE. SAE automatically updates the Nacos connection details—IP address, service port, namespace, AccessKey ID, AccessKey secret, and context path. No additional configuration is needed. Your existing local Nacos settings can be retained or removed.
Build a gateway with Spring Cloud Netflix Zuul
Step 1: Create the Maven project
Create a Maven project named spring-cloud-zuul-nacos and add the following to pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>Step 2: Create the startup class
Create a class named ZuulApplication:
@SpringBootApplication
@EnableZuulProxy
@EnableDiscoveryClient
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}Step 3: Configure the gateway
Add the following to application.properties. The zuul.routes entries define routing rules: requests prefixed with /provider1/ are forwarded to the service-provider backend service.
Replace 127.0.0.1:8848 with your Nacos Server address if it runs on a different host.
spring.application.name=spring-cloud-zuul-nacos
server.port=18022
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
zuul.routes.opensource-provider1.path=/provider1/**
zuul.routes.opensource-provider1.serviceId=service-providerStep 4: Create a service provider
Use the same service provider as the Spring Cloud Gateway example. For details, see Migrate Spring Cloud applications to SAE.
Verify the gateway
Local verification
Start both the Zuul gateway and the service provider, then send a test request:
curl http://127.0.0.1:18022/provider1/echo/helloExpected output:
helloTo confirm the gateway registered with Nacos, log in to the Nacos console at http://127.0.0.1:8848/nacos (default credentials: nacos/nacos). Go to Service Management > Services and verify that spring-cloud-zuul-nacos appears in the list. You can also view detailed service information by clicking Details.
Verify in SAE
Deploy the application to SAE. SAE automatically updates the Nacos connection details—IP address, service port, namespace, AccessKey ID, AccessKey secret, and context path. No additional configuration is needed. For deployment steps, see Migrate Spring Cloud applications to SAE.