All Products
Search
Document Center

Deploy service gateways

Last Updated: Feb 28, 2019

This topic describes how to, based on Spring Cloud Gateway and Netflix Zuul, deploy service gateways for applications from scratch by using ANS.

[TOC]

Why do service gateways use ANS as the registry?

Application Naming Service (ANS) is a service discovery component provided by EDAS, which is a commercial version of open source Nacos.

org.springframework.cloud:spring-cloud-starter-alicloud-ans implements the standard APIs and specifications of Spring Cloud Registry. ANS can completely replace the service discovery function provided by Spring Cloud Eureka or Spring Cloud Consul.

In addition, ANS offers the following advantages over Spring Cloud Eureka and Spring Cloud Consul:

  • ANS is a shared component that saves you the cost of deploying, operating, or maintaining Spring Cloud Eureka or Spring Cloud Consul.
  • ANS provides link encryption for both service registration and discovery calls, protecting your service from being detected by others.
  • ANS is fully integrated with other EDAS components to provide you with a complete set of microservice solutions.

Preparation

  • Download, start, and configure the lightweight configuration center

    To facilitate local development, EDAS provides a lightweight configuration center that has the basic features of the EDAS service registry.You can deploy the applications developed based on the lightweight configuration center to EDAS without making any modifications to code or configurations.

    For more information about how to download, start, and configure the lightweight configuration center, see Configure the lightweight configuration center.The latest version is recommended.

  • Download Maven and set environment variables (skip this step if Maven is installed locally).

Deploy service gateways based on Spring Cloud Gateway

The following describes how to use ANS to deploy service gateways from scratch based on Spring Cloud Gateway.

Create a service gateway

  1. Create a Maven project named spring-cloud-example-ans-gateway.

  2. Add Spring Boot and Spring Cloud Finchley dependencies to the pom.xml file.

    The following takes Spring Boot 2.0.6 RELEASE and Spring Cloud Finchley.SR1 as an example.

    Note: Spring Cloud Gateway is a component developed based on Spring Boot 2.0. If you are using Spring Cloud Gateway as the service gateway, select Spring Boot 2.0 or later.If you are using Spring Boot 1.x, we recommend that you upgrade it to Spring Cloud 2.0.

      <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.0.6.RELEASE</version>
         <relativePath/>
     </parent>
    
     <properties>
         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
         <spring-cloud-alibaba-cloud.version>0.2.1.RELEASE</spring-cloud-alibaba-cloud.version>
     </properties>
    
     <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>${spring-cloud.version}</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-alibaba-dependencies</artifactId>
               <version>${spring-cloud-alibaba-cloud.version}</version>
               <type>pom</type>
               <scope>import</scope>
            </dependency>
       </dependencies>
       </dependencyManagement>
    
     <dependencies>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-webflux</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-gateway</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-alicloud-ans</artifactId>
             <exclusions>
                 <! - Spring Cloud Gateway uses Netty as its HTTP server, so you need to **exclude** dependencies on spring-boot-starter-web to prevent startup failures. -->
                 <exclusion>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-web</artifactId>
                 </exclusion>
             </exclusions>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-alicloud-context</artifactId>
         </dependency>
     </dependencies>
    
  3. Develop the service gateway startup class AnsGatewayApplication.

     @SpringBootApplication
     @EnableDiscoveryClient
     <! - Service registration and discovery functions must be enabled for the application -->
     public class AnsGatewayApplication {
         public static void main(String[] args) {
    
             SpringApplication.run(AnsGatewayApplication.class, args);
         }
     }
    
  4. Add the following configuration to the application.yaml file and specify the EDAS lightweight configuration center as the registry.

    where, 127.0.0.1 is the address of the lightweight configuration center, which must be changed to the corresponding IP address if your lightweight configuration center is deployed on another instance.The lightweight configuration center does not support port modification, so port 8080 must be used.

     server:
       port: 15012
     spring:
       application:
         name: spring-gateway-example
       cloud:
         gateway: # config the routes for gateway
           routes:
           - id: lb_service-provider
             uri: lb://service-provider
             predicates:
             - Path=/**
         alicloud:
           ans:
             server-list: 127.0.0.1
             server-port: 8080
    
  5. Execute the main function in the startup class AnsGatewayApplication to enable the service.

  6. Log on to the console of the lightweight configuration center (http://127.0.0.1:8080). In the left-side navigation pane, click Services to view the list of service providers.spring-gateway-example exists in the list of service providers.

Create a service provider

Create an application that serves as the service provider.For more information, see Quick start.

Sample service provider:

@SpringBootApplication
@EnableDiscoveryClient
public class AnsProviderApplication {

    public static void main(String[] args) {

        SpringApplication.run(AnsProviderApplication.class, args);
    }

    @RestController
    public class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return string;
        }
    }
}

Result verification

  1. Locally verify the result.

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

     Result verification

  2. Verify the result in EDAS.

    Deploy the developed service gateway and service provider to EDAS and access Spring Cloud Gateway to forward the request to the backend service. The result indicating a successful call is returned.

     Verify the application deployment in EDAS

Use Spring Boot 2.x to deploy service gateways based on Zuul

The following describes how to use ANS to deploy service gateways from scratch in Spring Boot 2.x based on Netflix Zuul.

Create a service gateway

  1. Create a Maven project named spring-cloud-example-ans-zuul.

  2. Add Spring Boot and Spring Cloud Finchley dependencies to the pom.xml file.

    The following takes Spring Boot 2.0.6 RELEASE and Spring Cloud Finchley.SR1 as an example.

      <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>2.0.6.RELEASE</version>
         <relativePath/>
     </parent>
    
        <properties>
         <spring-cloud.version>Finchley.SR1</spring-cloud.version>
         <spring-cloud-alibaba-cloud.version>0.2.1.RELEASE</spring-cloud-alibaba-cloud.version>
     </properties>
    
     <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>${spring-cloud.version}</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
           <dependency>
                 <groupId>org.springframework.cloud</groupId>
                 <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                 <version>${spring-cloud-alibaba-cloud.version}</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
       </dependencies>
       </dependencyManagement>
    
     <dependencies>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-alicloud-ans</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-alicloud-context</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
         </dependency>
     </dependencies>
    

    If you need to use Spring Boot 1.x, use Spring Boot 1.5.x, Spring Cloud Edgware, and org.springframework.cloud:spring-cloud-starter-alicloud-ans version 0.1.1. RELEASE.

    Note: Spring Boot 1.x will expire in August 2019, so we recommend that you use a later version to develop applications.

  3. Develop the service gateway startup class AnsZuulTwoXApplication.

     @SpringBootApplication
     @EnableDiscoveryClient
     <! - Enable the service registration and discovery functions -->
     @EnableZuulProxy
       <! - Enable the Zuul Server agent -->
     public class AnsZuulTwoXApplication {
    
         public static void main(String[] args) {
    
             SpringApplication.run(AnsZuulTwoXApplication.class, args);
         }
     }
    
  4. Add the following configuration to the application.properties file and specify the EDAS lightweight configuration center as the registry.

    where, 127.0.0.1 is the address of the lightweight configuration center, which must be changed to the corresponding IP address if your lightweight configuration center is deployed on another instance.The lightweight configuration center does not support port modification, so port 8080 must be used.

     spring.application.name=spring-cloud-ans-gateway
     server.port=13012
     spring.cloud.alicloud.ans.server-list=127.0.0.1
     spring.cloud.alicloud.ans.server-port=8080
     # config zuul
     zuul.routes.service-provider.path=/**
    
  5. Execute the main function AnsZuulTwoXApplication in spring-cloud-example-ans-zuul to enable the service.

  6. Log on to the console of the lightweight configuration center http://127.0.0.1:8080. In the left-side navigation pane, click Services to view the list of service providers.spring-cloud-ans-gateway exists in the list of service providers.

Create a service provider

For more information about how to quickly create a service provider, see Quick start.

Sample service provider startup class:

@SpringBootApplication
@EnableDiscoveryClient
public class AnsProviderApplication {

    public static void main(String[] args) {

        SpringApplication.run(AnsProviderApplication.class, args);
    }

    @RestController
    public class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return string;
        }
    }
}

Result verification

  1. Locally verify the result.

    Access the API provided by the backend service through Netflix Zuul. The result indicating a successful call is returned.

     Local result verification

  2. Verify the result in EDAS.

    Deploy the developed service gateway and service provider to EDAS and access the backend service through the service gateway. The result indicating a successful call is returned.

     Verify the application deployment in EDAS

Use Spring Boot 1.x to deploy service gateways based on Netflix Zuul

The following describes how to use ANS to deploy service gateways from scratch in Spring Boot 1.x based on Netflix Zuul.

If you need to use Spring Boot 1.x, use Spring Boot 1.5.x, Spring Cloud Edgware, and org.springframework.cloud:spring-cloud-starter-alicloud-ans version 0.1.1. RELEASE.The content in the pom.xml file is as follows:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <spring-cloud.version>Edgware.SR4</spring-cloud.version>
    <spring-cloud-alibaba-cloud.version>0.1.1.RELEASE</spring-cloud-alibaba-cloud.version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-alicloud-ans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-alicloud-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Note: In contrast to using Spring Boot 2.x to integrate ANS based on Zuul, this method uses different versions of Spring Boot, Spring Cloud, and org.springframework.cloud:spring-cloud-starter-alicloud-ans, but follows the same main method and the same application.properties file.

Result verification

  1. Locally verify the result.

    Access the API provided by the backend service through Netflix Zuul. The result indicating a successful call is returned.

     Local result verification

  2. Verify the result in EDAS.

    Deploy the developed service gateway and service provider to EDAS and access the backend service through the service gateway. The result indicating a successful call is returned.

     Verify the application deployment in EDAS