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

Preparations

  • 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 Nacos Server release notes.Skip this step if you have installed Nacos Server.
    1. Decompress the downloaded Nacos Server package.
    2. Go to the nacos/bin directory to start Nacos Server.
      • On Linux, UNIX, or macOS, run the sh startup.sh -m standalone command.
      • On Windows, double-click the startup.cmd file to run the file.
  • Download demo applications.

    This topic describes key information about how to develop applications in an on-premises environment. To obtain the complete Spring Cloud applications, 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.

      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>                                
    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 the prefix /provider1 needs to be truncated.
              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.

    For more information how to create a service provider application, 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. 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 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. The returned result indicates a successful call.

      EDAS Spring Cloud application development: build a Zuul gateway
    • 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 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.

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.
Note Spring Cloud Edgware has reached the end of life. Therefore, we recommend that you do not use this version to develop applications.