spring cloud gateway路由模式

update是库存的接口路径

aaa是订单的接口路径 

源码地址12级程序猿-新年正当红/springcloud alibaba+jdk17

请求uri进行路由

server:
  port: 9000
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        namespace: public
        password: nacos
        server-addr: localhost:8848
        username: nacos
    gateway:
      routes:
        #唯一标识 转发到库存服务  请求uri的方式进行路由
        - id: kucun-router
          uri: http://localhost:8081
          #谓词 浏览器访问 http://localhost:9000/kucun/xxx 转发到库存对应的接口上
          predicates:
            - Path=/kucun/**
          #这个必须加 否则无法访问目标服务
          filters:
            - StripPrefix=1

        #唯一标识 转发到订单服务 根据请求uri的方式进行路由
        - id: order-router
          #谓词 浏览器访问 http://localhost:9000/order/xxx 转发到订单对应的接口上
          uri: http://localhost:8082
          predicates:
            - Path=/order/**
          #这个必须加 否则无法访问目标服务
          filters:
            - StripPrefix=1

在浏览器通过下面的地址访问库存接口 通过访问网关,由网关进行转发到库存

http://localhost:9000/kucun/update

在浏览器通过下面的地址访问订单接口 通过访问网关,由网关进行转发到订单

http://localhost:9000/order/addOrder

请求方法进行路由 

server:
  port: 9000
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        namespace: public
        password: nacos
        server-addr: localhost:8848
        username: nacos
    gateway:
      routes:
        #唯一标识 转发到库存服务  请求uri的方式进行路由
        - id: kucun-router
          uri: http://localhost:8081
          #谓词 浏览器访问 http://localhost:9000/kucun/xxx 转发到库存对应的接口上
          predicates:
            - Method=GET
          #这个必须加 否则无法访问目标服务
#          filters:
#            - StripPrefix=1

        #唯一标识 转发到订单服务 根据请求uri的方式进行路由
        - id: order-router
          #谓词 浏览器访问 http://localhost:9000/order/xxx 转发到订单对应的接口上
          uri: http://localhost:8082
          predicates:
            - Method=POST
          #这个必须加 否则无法访问目标服务
#          filters:
#            - StripPrefix=1

使用post请求就访问到了订单服务

使用get请求就访问到了库存服务

 请求头的值进行路由

server:
  port: 9000
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        namespace: public
        password: nacos
        server-addr: localhost:8848
        username: nacos
    gateway:
      routes:
        - id: kucun-router
          uri: http://localhost:8081
          predicates:
            - Header=name,123

        - id: order-router
          uri: http://localhost:8082
          predicates:
            - Header=name,456

在header头这里,设置不同的参数结果,就可以访问到对应的服务 

路径也要输入正确

 请求参数的值进行路由

server:
  port: 9000
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        namespace: public
        password: nacos
        server-addr: localhost:8848
        username: nacos
    gateway:
      routes:
        - id: kucun-router
          uri: http://localhost:8081
          predicates:
            - Query=name,123

        - id: order-router
          uri: http://localhost:8082
          predicates:
            - Query=name,456

在参数这里设置不同的结果就请求到不同的服务,路径一定要正确

根据主机地址进行路由 

在Host里面设置不同的域名 进行映射

server:
  port: 9000
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        namespace: public
        password: nacos
        server-addr: localhost:8848
        username: nacos
    gateway:
      routes:
        - id: kucun-router
          uri: http://localhost:8081
          predicates:
            - Host=kucun.com

        - id: order-router
          uri: http://localhost:8082
          predicates:
            - Host=order.com

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值