谓词 Host · SpringCloud微服务实战 · 看云

6.谓词 Host

导航

网关的谓词校验不止可以设置Cookie、Header ,还可以匹配校验Host ,对应用也更安全。本节演示虽Host 的校验来实现转发的控制。

本节代码地址


6.1 应用配置

这里配置的域名支持二级域名,可以是www.fwcloud.top。也可以是doc.fwcloud.top

server:
  port: 8699
spring:
  application:
    name: fw-gateways-gateway
  profiles:
    active: host_route
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  cloud:
    gateway:
      routes:
        - id: host_route
          uri: lb://fw-cloud-ribbon-server
          predicates:
            - Host=**.fwcloud.top
  profiles: host_route

6.2 应用启动

Postman 请求localhost:8699/user/2必须携带Host 在Header中,否则会404
842d756293c62c8e13c85982a9684b76_MD5.webp