SpringCloud Gateway service gateway, assertion

SpringCloud Gateway uses non-blocking mode, has better performance than Netflix Zuul, and supports long connections. Its core components are assertions and filters.

In the previous Consul main project, the gateway module was added to realize the service gateway function.

The SpringCloud Gateway routing function uses assertions to match requests and routes. The matched requests are sent to the corresponding gateway web processor for processing, and the processor passes through a series of filters during processing.

1. Maven dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2. Configuration file

server:
  port: 8801
spring:
  application:
    name: gateway
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: gateway
    gateway:
      discovery:
        locator:
          enabled: false
          lower-case-service-id: true
      routes:
        - id: logservice
          uri: lb://logservice
          predicates:
          - Path=/log/**
          filters:
          - StripPrefix=1
        - id: orderservice
          uri: lb://orderservice
          predicates:
          - Path=/order/**
          filters:
          - StripPrefix=1

# 配置日志
logging:
  level:
    org.springframework.cloud.gateway: debug

Multiple routes can be set under the routes node, and each route has a unique id to match the corresponding service.

Configure assertions under the predicates node. Gateway has a variety of built-in assertions. Commonly used are Path and path routing assertions. The lb in the above uri means to use load balancing, or it can be a specific URL.

The Method method routing assertion allows only the specified request method type to pass. Others include Header, Cookie, Host, QueryParam, RemoteAddr, DateTime.

 

3. Add annotations to the startup class

@EnableDiscoveryClient

4. Access log service and order service from the gateway

http://localhost:8801/log/log

http://localhost:8801/order/order

 

Guess you like

Origin blog.csdn.net/suoyx/article/details/115025535