gateway 网关

构建于springboot2.x,spring webFlux,project reactor之上,更加符合未来的技术体系

使用非阻塞的方式

pom

        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  eureka client将gateWay注册到eureka

yml
server:
  port: 9527
spring:
  application:
    name: cloud-gateway

    #gate-way配置
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true                      # 开启从注册中心动态创建路由的功能,利用微服务名进行路由
      routes:
#        - id: payment-eureka                # 路由的ID,没有固定规则但要求唯一,简易配合服务名
#          uri:  http://localhost:8001       # 匹配提供服务的路由地址(与lb的形式不能共存,共存会出错)
#          predicates:
#            - Path=/payment/eureka/**       #  断言,路径相匹配的进行路由

        - id: payment-service
          uri:  lb://cloud-payment-service   # 匹配后提供服务的路由地址,lb后跟提供服务的微服务的名,不要写错
          predicates:
            - Path=/payment/**,/payment/eureka/**
            - Header=token, ^w+$            # head头上需要带token,值任意(针对某些接口需要登录携带token)
            - Method=Get,Post                # 支持get,post请求



eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/

    



    
两个id,匹配两个路由

  需要匹配多个,使用“,”分割(例如:请求类型仅仅支持get post)



原文地址:https://www.cnblogs.com/draymond/p/12782880.html