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

Why do service gateways use the EDAS registry?

Spring Cloud Alibaba Nacos Discovery is the GA version of the open-source Nacos Server provided by the EDAS registry. It allows you to directly use the GA version of registry provided by EDAS.

The commercial EDAS registry has the following advantages over Nacos, Eureka, and Consul:

  • Components are shared, which saves the costs of deploying, operating, and maintaining Nacos, Eureka, or Consul.
  • Links are encrypted for the calls to use service registration and discovery, which protects your services from being discovered by unauthorized applications.
  • The EDAS registry is tightly integrated with other EDAS components to provide you with a complete set of microservice solutions, including environment isolation, graceful connection and disconnection, and canary release.

Preparations

  • Download Maven and set the environment variables. Skip this step if you have installed Maven in your local instance.
  • Download the latest version of Nacos Server. Skip this step if you have installed Nacos Server in your local instance.
  • Start Nacos Server.

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

    For the Linux, UNIX, or MacOS operating system, run sh startup.sh -m standalone. For the Windows operating system, run cmd startup.cmd or double-click startup.cmd to run the file.

  • Demo application

    This topic describes key information for developing applications locally. For more information about Spring Cloud, download spring-cloud-gateway-nacos, spring-cloud-zuul-nacos, and nacos-service-provider.

Build service gateways based on Spring Cloud Gateway

This section describes how to use Nacos to build a service gateway from scratch based on Spring Cloud Gateway.

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

      The following takes Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1 as an 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 named GatewayApplication.
          @SpringBootApplication
          @EnableDiscoveryClient
          public class GatewayApplication {
              public static void main(String[] args) {
                  SpringApplication.run(GatewayApplication.class, args);
              }
          }                                
    4. Add the following configuration in application.yaml to set the registry address to the IP address of Nacos Server.

      127.0.0.1:8848 is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change it to the corresponding address.

      routes specifies the routing and forwarding rules of the gateway. In this example, all requests with the prefix /provider1/ are routed to the backend service named service-provider.

          server:
       port: 15012
      
          spring:
       application:
              name: spring-cloud-gateway-eureka
       cloud:
              gateway: # config the routes for gateway
                routes:
                -id: service-provider          # Forwards requests with the prefix /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 Nacos Server console at http://127.0.0.1:8848/nacos, with nacos as both the default user name and password. In the left-side navigation pane, choose Service Management > Services. You can see that spring-cloud-gateway-nacos is available in the list of services. In addition, you can query the details of the service. This indicates that the gateway has been started and registered. Then, create a downstream service to verify the request forwarding function of the gateway.
  2. Create a service provider.

    Create a service provider application. For more information, see Host Spring Cloud applications to EDAS.

    Sample 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 locally.

      Locally start the service gateway and service provider you created and access Spring Cloud Gateway to forward the request to the backend service. The result indicating a successful call is returned.

    • Verify the result in EDAS.

      Deploy your application to EDAS by referring to Host Spring Cloud applications to EDAS, and verify the result.

      The EDAS registry provides a GA version of Nacos Server. When you deploy an application to EDAS, EDAS sets the IP address and service port of Nacos Server, namespace, AccessKey ID, AccessKey secret, and context path by using a method with a higher priority. You do not need to make any additional configurations. In addition, you can choose to retain or delete your original configuration.

Build service gateways based on Zuul

This topic describes how to use Nacos Server as the registry to build service gateways for applications from scratch based on Zuul.

  1. Create a service gateway.
    1. Create a Maven project named spring-cloud-zuul-nacos.
    2. Add the dependencies of Spring Boot, Spring Cloud, and Spring Cloud Alibaba in 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 named ZuulApplication.
          @SpringBootApplication
          @EnableZuulProxy
          @EnableDiscoveryClient
          public class ZuulApplication {
              public static void main(String[] args) {
                  SpringApplication.run(ZuulApplication.class, args);
              }
          }                                
    4. Add the following configuration in application.properties to set the registry address to the IP address of Nacos Server.

      127.0.0.1:8848 is the IP address of Nacos Server. If your Nacos Server is deployed on another machine, change it to the corresponding address.

      Routes specifies the routing and forwarding rules of Zuul. In this example, all requests with the prefix /provider1/ are routed to the backend service named service-provider.

       spring.application.name=spring-cloud-zuul-eureka
       server.port=18022
      
       spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
      
       zuul.routes.service-provider.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 Nacos Server console at http://127.0.0.1:8848/nacos with nacos as both the default user name and password. In the left-side navigation pane, choose Service Management > Services. You can see that spring-cloud-zuul-nacos is available in the list of services. In addition, you can query the details of the service. This indicates that the gateway has been started and registered. Then, create a downstream service to verify the request forwarding function of the gateway.
  2. Create a service provider.
    For more information about how to quickly create a service provider, see Host Spring Cloud applications to EDAS.
    Sample 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 locally.

      Locally start the Zuul service gateway and service provider you created and forward the request to the backend service through Spring Cloud Netflix Zuul. The result indicating a successful call is returned.

      EDAS Spring Cloud application development: build Zuul gateway management
    • Verify the result in EDAS.

      Deploy your application to EDAS by referring to Host Spring Cloud applications to EDAS, and verify the result.

      The EDAS registry provides a GA version of Nacos Server. When you deploy an application to EDAS, EDAS sets the IP address and service port of Nacos Server, namespace, AccessKey ID, AccessKey secret, and context path by using a method with a higher priority. You do not need to make any additional configurations. In addition, you can choose to retain or delete your original configuration.

FAQ

  1. Use other versions

    This example uses Spring Cloud Greenwich, and the corresponding Spring Cloud Alibaba version is 2.1.1.RELEASE. Spring Cloud Finchley must be used with Spring Cloud Alibaba 2.0.1.RELEASE, and Spring Cloud Edgware with Spring Cloud Alibaba 1.5.1.RELEASE.

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

    The EDAS registry configures the data structures of Application Naming Service (ANS) and Nacos in a way that the two are compatible on servers. In the same namespace and when no group is configured in Nacos, the Nacos client and the ANS client can discover services registered by each other.