其他分享
首页 > 其他分享> > springcloud学习之网关-Gateway

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过滤器
在这里插入图片描述

标签:网关,http,springcloud,payment,eureka,cloud,data,Gateway,localhost
来源: https://blog.csdn.net/qq_45810225/article/details/121424406