09 Gateway路由及过滤器

Spring Cloud Gateway的核心就是一系列的过滤器,可以将客户端的请求转发到不同的微服务。主要作用:过滤和路由。

创建工程springcloud_gateway

动态路由

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud_parent</artifactId>
        <groupId>li.chen.com</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud_gateway</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>


</project>

配置文件
application.yml

server:
  port: 10010
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        # 路由id,可以任意
        - id: user-service-route
          # 代理的服务地址
          #uri: http://127.0.0.1:9090
          # lb表示从eureka中获取具体服务(lb 之后的服务名必须要在eureka中注册才能使用);动态代理
          uri: lb://UserService
          # 路由断言: 可以匹配映射路径
          predicates:
            #- Path=/**  全匹配
            - Path=/UserController/**
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
    instance:
      prefer-ip-address: true

启动类

package li.chen.com.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class,args);
    }
}

测试:
访问路径
http://127.0.0.1:10010/UserController/7
–>代理 http://127.0.0.1:9090/UserController/7
(之前的user_service等服务都要开启)
在这里插入图片描述

在这里插入图片描述

路由前后缀(局部过滤器)

客户端的请求地址与微服务的服务地址如果不一致的时候,可以通过配置路径过滤器实现路径前缀的添加和去除。

配置文件(前缀)
application.yml

server:
  port: 10010
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        # 路由id,可以任意
        - id: user-service-route
          # 代理的服务地址
          #uri: http://127.0.0.1:9090
          # lb表示从eureka中获取具体服务(lb 之后的服务名必须要在eureka中注册才能使用);动态代理
          uri: lb://UserService
          # 路由断言: 可以匹配映射路径
          predicates:
            #- Path=/**  全匹配
           # - Path=/UserController/**
            - Path=/**
          filters:
          # 添加请求路径的前缀
            - PrefixPath=/UserController
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
    instance:
      prefer-ip-address: true

访问路径 http://127.0.0.1:10010/7
PrefixPath作用 --》http://127.0.0.1:10010/UserController/7

配置文件(后缀)
application.yml

server:
  port: 10010
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        # 路由id,可以任意
        - id: user-service-route
          # 代理的服务地址
          #uri: http://127.0.0.1:9090
          # lb表示从eureka中获取具体服务(lb 之后的服务名必须要在eureka中注册才能使用);动态代理
          uri: lb://UserService
          # 路由断言: 可以匹配映射路径
          predicates:
            #- Path=/**  全匹配
            #- Path=/UserController/**
            - Path=/api/UserController/**
          filters:
            #1表示过滤1个路径,2表示两个路径,以此类推
            - StripPrefix=1

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
    instance:
      prefer-ip-address: true

访问路径 http://127.0.0.1:10010/api/UserController/1
StripPrefix作用 --》 http://127.0.0.1:10010/UserController/1 在这里插入图片描述

路由(全局过滤器)

配置文件(后缀)
application.yml

server:
  port: 10010
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        # 路由id,可以任意
        - id: user-service-route
          # 代理的服务地址
          #uri: http://127.0.0.1:9090
          # lb表示从eureka中获取具体服务 ;lb 之后编写的服务名必须要在eureka中注册才能使用
          uri: lb://UserService
          # 路由断言: 可以匹配映射路径
          predicates:
            #- Path=/UserController/**
            #- Path=/**
            - Path=/api/UserController/**
          filters:
            #1表示过滤1个路径,2表示两个路径,以此类推
            - StripPrefix=1
      # 默认过滤器,对所有的路由都生效(全局)
      default-filters:
        - AddResponseHeader=X-Response-Foo, Bar
        - AddResponseHeader=x-myname,itcast

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
    instance:
      prefer-ip-address: true

访问路径 http://127.0.0.1:10010/api/UserController/1
StripPrefix作用 --》 http://127.0.0.1:10010/UserController/1
default-filters作用:
在这里插入图片描述

使用场景:

  • 用法:在配置文件中指定要使用的过滤器名称;
  • 类型:局部、全局;
  • 使用场景:请求鉴权、异常处理、记录调用时长等。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岿然如故

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值