This topic describes how to use Nacos to build service gateways for applications based on Spring Cloud Gateway and Spring Cloud Netflix Zuul.
SAE service registry
The SAE service registry provides a commercial version of the open-source Nacos Server. Applications developed with the open-source version Spring Cloud Alibaba Nacos Discovery can directly use the commercial service registry provided by SAE.
Compared with Nacos, Eureka, and Consul, the SAE service registry has the following advantages:
Components are shared. This reduces the costs of deploying, managing, and maintaining Nacos, Eureka, or Consul.
The service registry and discovery calls are encrypted to protect your services. You no longer need to worry about unauthorized applications discovering your services.
The SAE service registry is integrated into other components of SAE to provide a full range of microservice solutions, such as environment isolation and canary release.
When you deploy an application on SAE, the SAE service registry automatically specifies the following information at a higher priority: the IP address and the service port of Nacos Server, namespace, and AccessKey pair. You do not need to configure additional settings.
Build a service gateway based on Spring Cloud Gateway
This section describes how to use Nacos to build service gateways based on Spring Cloud Gateway for applications from scratch.
Create a service gateway.
Create a Maven project that is named
spring-cloud-gateway-nacos.Add the dependencies of Spring Boot and Spring Cloud to the
pom.xmlfile.In the following sample code, Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 are added as dependencies.
<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>Develop a service gateway startup class that is named
GatewayApplication.@SpringBootApplication @EnableDiscoveryClient public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }To set the registry address to the address of Nacos Server, add the following configuration to
application.yaml.In the following configuration,
127.0.0.1:8848specifies the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change the value to the corresponding address.The routes setting specifies the routing and forwarding rules of the gateway. In this example, all requests prefixed with
/provider1/are routed to theservice-providerbackend service.server: port: 18012 spring: application: name: spring-cloud-gateway-nacos cloud: gateway: # config the routes for gateway routes: - id: service-provider # Forwards requests that are prefixed with /provider1/ to provider1. uri: lb://service-provider predicates: - Path=/provider1/** filters: - StripPrefix=1 # Indicates that the prefix /provider1 needs to be truncated. nacos: discovery: server-addr: 127.0.0.1:8848Run the main function of the
GatewayApplicationstartup class to start the gateway.Log on to the on-premises Nacos Server console at http://127.0.0.1:8848/nacos. The default username and password are nacos. In the left-side navigation pane, choose . You can find that spring-cloud-gateway-nacos appears in the service list. You can also query the detailed information about the service in Details. This indicates that the gateway has been started and registered. Then, create a downstream service to verify the request forwarding feature of the gateway.
Create a service provider.
For more information how to create a service provider application, see Modify service registration and discovery of applications to Nacos.
The following code provides an example on how to create a service provider:
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication, args); } @RestController public class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return string; } } }Verify the result.
Verify the result in your on-premises environment
Start the created service gateway and service provider in the on-premises environment, and connect to Spring Cloud Gateway to forward requests to the backend service. The returned result indicates a successful call.
Verify the result in Serverless App Engine (SAE)
The SAE registry provides a commercial version of Nacos. When you deploy an application to SAE, SAE updates the following information about your on-premises Nacos Server: the IP address and service port, namespace, AccessKey ID, AccessKey secret, and context path. You do not need to configure additional settings. You can retain or delete the original configurations.
Build a service gateway based on Spring Cloud Netflix Zuul
This section describes how to use Nacos as a registry to build service gateways based on Spring Cloud Netflix Zuul for applications from scratch.
Create a service gateway.
Create a Maven project that is named
spring-cloud-zuul-nacos.Add the dependencies of Spring Boot, Spring Cloud, and Spring Cloud Alibaba to the
pom.xmlfile.Add the dependencies of Spring Boot 2.1.4.RELEASE, Spring Cloud Greenwich.SR1, and Spring Cloud Alibaba 0.9.0.
<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>Develop a service gateway startup class that is named
ZuulApplication.@SpringBootApplication @EnableZuulProxy @EnableDiscoveryClient public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); } }Add the following configuration to
application.propertiesto set the registry address to the address of Nacos Server.In the following configuration,
127.0.0.1:8848specifies the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change the value to the corresponding address.The routes setting specifies the routing and forwarding rules of Zuul. In this example, all requests that are prefixed with
/provider1/are routed to theservice-providerbackend service.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-providerRun the main function of
ZuulApplicationin spring-cloud-zuul-nacos to start the service.Log on to the on-premises Nacos Server console at http://127.0.0.1:8848/nacos. The default username and password are nacos. In the left-side navigation pane, choose Service Management > Services. You can find that spring-cloud-zuul-nacos appears in the service list. You can also query the details about the service in Details. This indicates that the gateway has been started and registered. Then, create a downstream service to verify the request forwarding feature of the gateway.
Create a service provider.
For more information about how to create a service provider, see Modify service registration and discovery of applications to Nacos.
The following code provides an example on how to create a service provider startup class:
@SpringBootApplication @EnableDiscoveryClient public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication, args); } @RestController public class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return string; } } }Verify the result.
Verify the result in your on-premises environment
Start the created Zuul service gateway and service provider in your on-premises environment, and connect to Spring Cloud Netflix Zuul to forward requests to the backend service. The returned result indicates a successful call.

Verify the result in SAE
Deploy an application to SAE and verify the result. For more information, see Modify service registration and discovery of applications to Nacos.
The SAE registry provides a commercial version of Nacos. When you deploy an application to SAE, SAE updates the following information about your on-premises Nacos Server: the IP address and service port, namespace, AccessKey ID, AccessKey secret, and context path. You do not need to configure additional settings. You can retain or delete the original configurations.
Versions
Spring Cloud Greenwich is used in this example. The corresponding Spring Cloud Alibaba version is 2.1.1.RELEASE.
Spring Cloud Finchley corresponds to Spring Cloud Alibaba 2.0.1.RELEASE.
Spring Cloud Edgware corresponds to Spring Cloud Alibaba 1.5.1.RELEASE.
Spring Cloud Edgware has reached the end of life. Therefore, we recommend that you do not use this version to develop applications.