This topic describes how to use Nacos to build service gateways based on Spring Cloud Gateway or Spring Cloud Netflix Zuul.

Before you begin

  • Download Maven and set the environment variables.Skip this step if you have installed Maven.
  • Download the latest version of Nacos Server. For more information, see Download Nacos Server.Skip this step if you have installed Nacos Server.
  • Start Nacos Server.

    Decompress the downloaded Nacos Server package and go to the nacos/bin directory.

    On Linux, UNIX, or macOS, run the sh startup.sh -m standalone command. On Windows, run the cmd startup.cmd command or double-click startup.cmd to run the file.

  • Use demo applications.

    This topic describes key information about how to develop applications in an on-premises environment. To obtain more information about Spring Cloud, download spring-cloud-gateway-nacos, spring-cloud-zuul-nacos, and nacos-service-provider.

  • Optional:

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.

  1. Create a service gateway.
    1. Create a Maven project that is named spring-cloud-gateway-nacos.
    2. Add the dependencies of Spring Boot and Spring Cloud to the pom.xml file.

      Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 are used in this example.

       <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>                                
    3. 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);
              }
          }                                
    4. 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:8848 specifies 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 the service-provider backend 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 nacos in the prefix /provider1 must be truncated.
                discovery:
                  spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848                               
    5. Run the main function of the GatewayApplication startup class to start the gateway.
    6. 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-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.
  2. Create a service provider.

    Create a service provider application. For more information, see Implement service registration and discovery.

    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;
            }
        }
    }                        
  3. 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. You can view that the returned result indicates a successful call.

    • Verify the result in Enterprise Distributed Application Service (EDAS).

      You can deploy an application to EDAS and verify the result. For more information, see Implement service registration and discovery.

      The EDAS registry provides a formal commercial version of Nacos. When you deploy an application to EDAS, EDAS 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.

  1. Create a service gateway.
    1. Create a Maven project that is named spring-cloud-zuul-nacos.
    2. Add the dependencies of Spring Boot, Spring Cloud, and Spring Cloud Alibaba to the pom.xml file.

      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>                                
    3. 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);
              }
          }                                
    4. Add the following configuration to application.properties to set the registry address to the address of Nacos Server.

      In the following configuration, 127.0.0.1:8848 specifies 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 the service-provider backend 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-provider                                
    5. Run the main function of ZuulApplication in spring-cloud-zuul-nacos to start the service.
    6. 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.
  2. Create a service provider.
    For more information about how to create a service provider, see Implement service registration and discovery.
    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;
            }
        }
    }                        
  3. 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. You can view that the returned result indicates a successful call.

    • Verify the result in EDAS.

      Deploy an application to EDAS and verify the result. For more information, see Implement service registration and discovery.

      The EDAS registry provides a formal commercial version of Nacos. When you deploy an application to EDAS, EDAS 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.

Release notes

Spring Cloud Greenwich is used in this example. The corresponding Spring Cloud Alibaba version is 2.1.1.RELEASE. Spring Cloud Alibaba 2.0.1.RELEASE corresponds to Spring Cloud Finchley, and Spring Cloud Alibaba 1.5.1.RELEASE corresponds to Spring Cloud Edgware.

Note The Spring Cloud Edgware release reached the end of life. Therefore, we recommend that you do not use this release to develop applications.