微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

springcloud学习之网关-Gateway

由于Gateway底层使用了netty框架,所以性能很高,是异步非阻塞的
新建工程

在这里插入图片描述

添加pom依赖

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

添加yml配置

在这里插入图片描述

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/

主启动类,网关需注册注册中心

在这里插入图片描述

测试

在这里插入图片描述

在这里插入图片描述

编码方式配置路由,和配置文件配置两种方法选一种即可

在这里插入图片描述


gateway过滤器

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐