SpringCloud Gateway Gateway

Introduction

  As the entrance of traffic, the gateway has common functions including routing and forwarding, permission verification, current limiting control, etc.; and SpringCloud Gateway, as the second-generation gateway framework officially launched by SpringCloud, replaces the Zuul gateway
  

feature
  1. Built on Spring Framework 5, Project Reactor and Spring Boot 2.0
  2. Able to match any route on request attributes
  3. Routes have unique assertions and filters
  4. Integrated circuit breaker
  5. 集成Spring Cloud DiscoveryClient integration
  6. Easy to write assertions and filters
  7. Request rate limit
  8. Route rewriting
      
      needs to understand several basic concepts before using
    Route: routing, the basic building block of gateways. It is defined by ID, target URI, assertion set and filter set. If the assertion is true, match the route
    Predicate: assertion, Java8 assertion function, allowing developers to match any information requested at the time; its role is to route to the specified path when the assertion rules are met
    Filter: filter, before sending the request or Modify the request and response afterwards
      
work process

Insert picture description here
  Simply put, when our request arrives at the gateway, the gateway first uses an assertion to determine whether the request conforms to the routing rules, and if it meets the routing rules, it is routed to the designated place through a series of filtering according to the routing rules
  

Use Gateway

  The gateway, as the first layer of customer requests, directly forwards customer requests to the corresponding path through the gateway, so it should be a separate module, and it is recommended to use port 80 for convenience
. 1. Import the jar package

<!-- gateway -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Nacos 服务注册发现 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos 配置中心 -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

2. Write yaml configuration

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.0.109:8848
    gateway:
      routes: # 路由规则,可以配置多种,以数组的方式配置
        - id: test_route # 路由ID
          uri: https://www.baidu.com # 跳转的路径
          predicates: # 断言规则,数组形式配置多个
            - Query=url,baidu # 精确匹配,如果携带的参数url等于 "baidu",则跳转
        - id: qq_route
          uri: https://www.qq.com
          predicates:
            - Query=url,qq
  application:
    name: gulimall-gateway
server:
  port: 80

3. Start the test

@SpringBootApplication
@EnableDiscoveryClient //服务注册发现
public class GulimallGatewayApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(GulimallGatewayApplication.class, args);
    }
}

  In the configuration, I did two routing forwardings. When the request carries the "baidu" parameter, it jumps to the Baidu webpage, and when the request carries the "qq" it jumps to the qq homepage.

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/111052962