Spring Cloud Gateway is the second generation of Spring Cloud’s official Gateway framework, replacing Zuul Gateway. As a traffic, the gateway plays a very important role in the micro-service system. The common functions of the gateway include routing and forwarding, permission verification, and traffic limiting control.

In the case in the previous section, we described how to use NACOS as the service registry and configuration center, and Feign and SC Loadbalancer as the service invocation. This section describes how to use the Spring Cloud Gateway as a service gateway.

Engineering construction

Create a gateway project. The project directory is as follows:

The Gateway needs to register with NACOS to introduce the following dependencies:


        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        
Copy the code

In the configuration file application.pom file:

Server: port: 5000 Spring: Application: name: gateway Cloud: nacos: Discovery: server-addr: 127.0.0.1:8848 Gateway: discovery: locator: enabled: false lowerCaseServiceId: true routes: - id: provider uri: lb://provider predicates: - Path=/provider/** filters: - StripPrefix=1 - id: consumer uri: lb://consumer predicates: - Path=/consumer/** filters: - StripPrefix=1Copy the code

Please read the tutorial at the end of this article for an explanation of configuration, which will not be repeated here.

Add relevant notes to the project startup file:

 @SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

}
Copy the code

The gateway consumer Provider project has been successfully registered in NACOS.

Type http://localhost:5000/consumer/hi-feign in the browser, the browser returns a response:

hello feign, i'm provider ,my port:8762
Copy the code

Gateway has many other powerful features that won’t be covered here.

The tutorials

  • At the beginning of Spring Cloud Gateway experience: www.fangzhipeng.com/springcloud…
  • Spring Cloud Gateway Predict article: www.fangzhipeng.com/springcloud…
  • Spring cloud filter paper of gateway: www.fangzhipeng.com/springcloud…
  • Spring cloud gateway current-limiting article: www.fangzhipeng.com/springcloud…
  • Spring cloud service registration and discovery of gateway: www.fangzhipeng.com/springcloud…

Download the source code

Github.com/forezp/Spri…