微服务——网关

x33g5p2x  于2021-10-29 转载在 其他  
字(4.2k)|赞(0)|评价(0)|浏览(293)

1.TomCat请求处理分析

执行流程:
从线程池中获取一个线程对象
→线程对象调用IO从网络中读取数据(遵循http格式)
→解析数据,封装到request对象中
→Filter过滤
→Servlet分发请求
→将处理完的结果封装到response对象中并相应到客户端

2.通过Filter+Servlet理解执行链

package com.jt.common.filter;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
//过滤器 特殊拦截器
interface Filter{
     boolean inoke();
}
//控制器 分发请求
interface Servlet{
     void dispatch();
}
//过滤链
class FilterChain{
    private List<Filter> filters=new CopyOnWriteArrayList<>();//过滤器 请求数据过滤
    private Servlet servlet;//控制器 请求控制逻辑

    public FilterChain(List<Filter> filters, Servlet servlet) {
        this.filters.addAll(filters);
        this.servlet = servlet;
    }

    public void doFilter(){
        for(int i=0;i<filters.size();i++){
            if(!filters.get(i).inoke()) return;
        }
        servlet.dispatch();
    }
}
public class FilterChainTests {
    public static void main(String[] args) {
        List<Filter> filters=new CopyOnWriteArrayList<>();
        Filter filter1=new Filter() {
            @Override
            public boolean inoke() {
                System.out.println("过滤1");
                return true;
            }
        };
        Filter filter2=new Filter() {
            @Override
            public boolean inoke() {
                System.out.println("过滤2");
                return true;
            }
        };
        filters.add(filter1);
        filters.add(filter2);
        Servlet servlet=new Servlet() {
            @Override
            public void dispatch() {
                System.out.println("分发请求");
            }
        };
        FilterChain fc=new FilterChain(filters,servlet);
        fc.doFilter();
    }
}

实现效果

3. 网关Gateway

依赖

  • Netty 网络编程框架 大量使用NIO
  • WebFlux 请求处理

3.1 添加依赖

  • 作用:
  • API网关在微服务架构中也是一个web服务,但这个服务的启动不是依赖于Tomcat,依赖于网络编程框架Netty,添加此依赖后,系统底层会自动帮我们关联下载一个Netty框架
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

拓展–安装MavenHelper插件

3.2 创建启动类

package com.jt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

注意:启动了Netty服务器

3.3 配置yml文件

server:
  port: 9000
spring:
  application:
    name: sca-gateway
  cloud:
    gateway:
      routes: #配置网关路由规则
        - id: route01  #路由id,自己指定一个唯一值即可
          uri: http://localhost:8081/ #网关帮我们转发的url
          predicates: ###断言(谓此):匹配请求规则
            - Path=/nacos/provider/echo/**  #请求路径定义,此路径对应uri中的资源
          filters: ##网关过滤器,用于对谓词中的内容进行判断分析以及处理
            - StripPrefix=1 #转发之前去掉path中第一层路径,例如nacos

实现效果

  • 解析:
  • 我们访问 http://localhost:9000/nacos/provider/echo/gateway时,网关会基于谓此对象对请求url基于yml中定义的path进行比对,假如请求端口号后的内容与path定义的内容匹配,此时会将url交给过滤器进行过滤
  • 过滤器filter对请求过滤后,会将请求地址转发到真实的微服务(uri指定的地址) /*url是uri的子集 uri是统一资源标识

4. 网关负载均衡设计

4.1 添加依赖

  • 添加服务的注册依赖,当我们需要基于Gateway对服务的负载均衡调用, 我们就需要将网关作为一个服务,在Nacos中注册,同时网关也可以基于Nacos中的服务名获取多个服务实例
<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

4.2 修改yml文件

server:
  port: 9000
spring:
  application:
    name: sca-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #服务的注册
    gateway:
      routes: #配置网关路由规则
        - id: route01  #路由id,自己指定一个唯一值即可
          uri: lb://sca-provider  #lb表示负载均衡 sca-provider为服务名
          predicates: ###断言(谓此):匹配请求规则 http://localhost:9000/nacos/provider/echo/gateway
            - Path=/nacos/provider/echo/**  #请求路径定义,此路径对应uri中的资源
          filters: ##网关过滤器,用于对谓词中的内容进行判断分析以及处理
            - StripPrefix=1 #转发之前去掉path中第一层路径,例如nacos
  • 以8081、8082端口号分别启动provider服务以及gateway服务,实现效果

4.3执行流程分析

(图源官网)

  • 请求→处理器映射器 →谓词 true→ web请求处理器 → 过滤器s →业务逻辑

5.Gateway中常用过滤器分类

  • 局部过滤器GatewayFilter 配置文件中的filters配置
  • 全局过滤器 GlobalFilter 无需配置 默认生效

6. 限流设计

6.1 添加依赖

<!--网管层面加限流-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!---->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>

6.2 编辑yml文件

server:
  port: 9000
spring:
  application:
    name: sca-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848  #服务的注册
    sentinel:
      transport:
        dashboard: localhost:8180 #Sentinel 控制台地址
      eager: true #取消Sentinel控制台懒加载,即项目启动即连接
    gateway:
      routes: #配置网关路由规则
        - id: route01  #路由id,自己指定一个唯一值即可
          uri: lb://sca-provider  #lb表示负载均衡 sca-provider为服务名
          predicates: ###断言(谓此):匹配请求规则 http://localhost:9000/nacos/provider/echo/gateway
            - Path=/nacos/provider/echo/**  #请求路径定义,此路径对应uri中的资源
          filters: ##网关过滤器,用于对谓词中的内容进行判断分析以及处理
            - StripPrefix=1 #转发之前去掉path中第一层路径,例如nacos

idea配置

实现效果:取消Sentinel控制台懒加载,即项目启动即连接

6.3 编辑网关流控规则

6.4 idea中如何使用httpclient客户端工具

拓展:使用Postman工具

6.5 API分组限流

总结

  • 重点:
  • Gateway请求处理原理分析
  • Gateway复杂均衡实现

相关文章