springcloud学习之网关-Gateway

由于Gateway底层使用了netty框架,所以性能很高,是异步非阻塞的
新建工程
springcloud学习之网关-Gateway
添加pom依赖

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

添加yml配置
springcloud学习之网关-Gateway

server:
  port: 9527
spring:
  application:
    name: cloud-gateway-service
  cloud:
      gateway:
        routes:  #路由
          - id: payment-routh  # 路由ID,格式没有固定规则,但是必须要唯一,建议结合服务名使用,请求 http://localhost:8100/data-service1/test会转发到data-producer服务
            uri: lb://cloud-payment-service  #在服务注册中心找服务名为 data-producer的服务
            predicates:
              - Path=/payment/get/**  #设置路由断言,代理servicerId为cloud-payment-service的/payment/get/路径
          - id: payment-routh2   # 请求 http://localhost:8100/data-service2/test转发到 http://localhost:8080/test
            uri: http://localhost:8001
            predicates:
              - Path=/api/payment/discovery/**
            filters:
              - StripPrefix=1  #前缀, 在当前路径匹配中表示去掉第一个前缀 /data-service2
#eureka配置
eureka:
  instance:
    hostname: localhost  #eureka服务端实例名称
  client:
#  不注册自己
    register-with-eureka: true
#    false表示我自己就是注册中心,职责是维护实例,不需要去检索服务
    fetch-registry: true
    service-url:
#    注册地址
#单机版
      defaultZone: http://localhost:7001/eureka/
#      集群配置
#      defaultZone: http://euraka7001.com:7001/eureka/,http://euraka7002.com:7002/eureka/

主启动类,网关需注册到注册中心
springcloud学习之网关-Gateway
测试
springcloud学习之网关-Gateway
springcloud学习之网关-Gateway
编码方式配置路由,和配置文件配置两种方法选一种即可

springcloud学习之网关-Gateway
gateway过滤器
springcloud学习之网关-Gateway

上一篇:8Manage为旅游多元化企业构筑集中采购管理平台


下一篇:SpringCloudGateway内置GatewayFilter工厂类之Path类过滤器 (三)