13、服务网关-Gateway

系列文章目录

1、父工程创建
2、支付模块构建和热部署
3、消费者订单模块
4、服务注册中心-Eureka
5、zookeeper没学习
6、服务注册中心-Consul
7、Eureka、Consul异同
8、服务调用-Ribbon
9、服务调用-OpenFeign

10、服务降级-Hystrix
11、服务降级-Hystrix(二)
12、服务熔断-Hystrix
13、服务网关-Gateway
14-17 在git上做配置中心,没有学习
17、请求链路跟踪-Sleuth
18、Spring Cloud Alibaba-Nacos注册中心与配置中心
19、Spring Cloud Alibaba-Nacos集群和持久化配置
20、Sentinel流控
21、Sentinel熔断降级、热点key限流
22、SentinelResource配置
23、Sentinel 服务熔断与持久化


1. 概述

Gateway是Zuul1.x的替代
Zuul1.x是非Reactor模式老版本

springcloud gateway是基于WebFlux框架实现的,而WebFlux框架底层使用了高性能的Reactor模式通信框架Netty,提升了网关的性能

1.1 能做什么

  • 反向代理
  • 鉴权
  • 流量控制
  • 熔断
  • 日志监控

1.2 微服务架构中网关的位置

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EVOap6qs-1626510064610)(en-resource://database/1186:1)]

1.3 SpringCloud GateWay的特性

  • 动态路由:能够匹配任何请求属性
  • 可以对路由指定断言和过滤器
  • 集成Hystrix的断路器功能
  • 集成spring cloud服务发现功能
  • 易于编写断言与过滤器
  • 请求限流功能
  • 支持路径重写

1.4 Gateway工作流程

重要概念:

  1. Route(路由) - 路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如断言为true则匹配该路由;
  2. Predicate(断言) - 参考的是Java8的java.util.function.Predicate,开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由;
  3. Filter(过滤) - 指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
    在这里插入图片描述
    web请求,通过一些匹配条件,定位到真正的服务节点。并在这个转发过程的前后,进行一些精细化控制。
    predicate就是我们的匹配条件;而fliter,就可以理解为一个无所不能的拦截器。有了这两个元素,再加上目标uri,就可以实现一个具体的路由了

2. 搭建Getway

2.1新建模块

新建module cloud-gateway-getway9527

2.2 POM

<?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>cloud2021</artifactId>
        <groupId>com.chzu</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>
        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- commons -->
        <dependency>
            <groupId>com.chzu</groupId>
            <artifactId>cloud-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--一般基础配置类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

2.3 YML

server:
  port: 9527

spring:
  application:
    name: cloud-gateway

eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://localhost:7001/eureka

2.4 主启动类

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

2.5 如何做路由映射

例如我们访问8001端口的内容,但是现在我并不想暴漏8001端口,希望在8001外套一层9527

yml

server:
  port: 9527

spring:
  application:
    name: cloud-gateway

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

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

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


2.6 测试

  1. 启动cloud-eureka-server7001
  2. 启动cloud-provider-payment8001
  3. 启动cloud-gateway-gateway9527
  4. 访问http://localhost:8001/payment/get/1
  5. 访问http://localhost:9527/payment/get/1

如果配置成功,两者查询结果应该相同

2.7 注意点

在cloud-gateway-gateway9527 微服务中一定要移除 spring-boot-starter-web 这个依赖,要不然会报错,因为spring cloud gateway是基于webflux的,如果非要web支持的话需要导入spring-boot-starter-webflux而不是spring-boot-start-web。

  1. 硬编码配置路由网关
    写一个配置类
@Configurationpublic
class Gateway {    
@Bean    
public RouteLocator routes(RouteLocatorBuilder builder){        return builder.routes()                
.route("routr1",r->r.path("/guonei/**").uri("http://news.baidu.com/guonei")).build();    }}

3. 配置路由的方式

3.1 YML配置

如上节,直接在YML配置即可

3.2 硬编码

写一个配置类

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

        routes.route("path_route_atguigu",
                r -> r.path("/guonei")
                        .uri("http://news.baidu.com/guonei")).build();

        return routes.build();
    }
}

4. Gateway动态路由

4.1 配置

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

server:
  port: 9527

spring:
  application:
    name: cloud-gateway

  cloud:
    gateway:
    #*************************
      #开启从注册中心动态创建路由的功能,利用微服务名进行路由
      discovery:
        locator:
          enabled: true
   #******************************

      routes:
        - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/get/**         # 断言,路径相匹配的进行路由

        - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名\
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/lb/**         # 断言,路径相匹配的进行路由

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

4.2 测试

多次访问http://localhost:9527/payment/get/11
可以看到输出结果中的端口在变化

5. 常用的predicate

断言学习博客

6. Filter 过滤

使用过滤器,可以在请求在路由前后对请求进行修改
Filter学习博客

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值