• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringClou项目搭建--网关Gateway笔记七

武飞扬头像
liucan1020
帮助1


前言

zuul 属于netflix公司不更新了,用gateway代替 属于spring公司


一、Gateway网关介绍

官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/

1、gateway是什么

底层使用WebFlux Netty 异步非阻塞
学新通

gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能,如:熔断、限流、重试等

路由:是构建网关的基本模块,他由ID、目标URL 一系列的断言和过滤链组成,如果断言为true则匹配该路由

Gateway工作流程:
学新通

1、作用

  1. 反向代理
  2. 鉴权
  3. 流量控制
  4. 熔断
  5. 日志监控

二、使用

1、添加maven依赖

<?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>cloud2022</artifactId>
        <groupId>com.lc.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-gateway-gateway9527</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>


</project>
学新通

2、yml配置文件

server:
  port: 9527

eureka:
  instance:
    hostname: cloud-gateway-service
  client: #服务提供者provider注册进eureka服务列表内
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://localhost:7001/eureka

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8001          #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/get/**         # 断言,路径相匹配的进行路由

        - id: hystrix_route2 #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8001          #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/hystrix/**         # 断言,路径相匹配的进行路由
学新通

3、配置路由

1、配置文件配置实现

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_route #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8001          #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/get/**         # 断言,路径相匹配的进行路由

        - id: hystrix_route2 #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: http://localhost:8001          #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/hystrix/**         # 断言,路径相匹配的进行路由

学新通

2、配置类实现

@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

        routes.route("path_route_route1",
                r->r.path("/v/douga")
                        .uri("http://www.bilibili.com"));
        return routes.build();
    }

    @Bean
    public RouteLocator paymentRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

        routes.route("path_route_route2",
                r->r.path("/payment/get/**")
                        .uri("http://localhost:8001"));
        return routes.build();
    }

} 
学新通

三、根据微服务名实现动态路由

默认情况下gateway活根据注册中心注册的服务列表,以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能

需要注意的是uri的协议为lb,表示启用Gateway的负载均衡功能。lb://serviceName是spring cloud gateway在微服务中自动为我们创建的负载均衡uri。

学新通

四、Predicate断言的使用

Predicate就是为了实现一组匹配规则让请求过来找到对应的Route进行处理

  1. path路径
  2. 时间之后
  3. 时间之前
  4. 时间之间
  5. cookie,key-value(正则)
  6. Header请求头,key-value(正则)
  7. Host
  8. Query
  9. RemoteAdder

如:
学新通

五、Gateway过滤器Filter

1、过滤器的作用

路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应请求,路由过滤器只能指定路由进行使用

生命周期:pre(业务逻辑之前)和post(业务逻辑之后)
种类:GatewayFilter(单一)和GlobalFilter(全局)

全局日志记录,鉴权

2、自定义过滤器

实现implements GlobalFilter, Ordered 接口

@Slf4j
@Configuration
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("******** com in MyLogGateWayFilter:{}",new Date());
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if (uname == null) {
            log.info("******* 用户名为null,非法用户");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
学新通

LC1020…966231

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjaggk
系列文章
更多 icon
同类精品
更多 icon
继续加载