目录

1、GateWay网关概述

1.1、什么是GateWay?

1.2、为什么要使用微服务网关?

1.3、Zuul与GateWay网关的区别?

2、快速入门

2.1、创建项目

2.2、配置yml文件

2.3、controller层

2.4、启动类

2.5、启动整体项目

2.6、配置全局过滤器


1、GateWay网关概述

1.1、什么是GateWay?

SpringCloud Gateway是Spring Cloud 的一个全新项目,基于Spring 5.0+Spring Boot 2.0和Project Reactor等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。

SpringCloud Gateway 作为Spring Cloud 生态系统中的网关,目标是替代Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上 最新高性能版本进行集成,仍然还是使用的Zuul 1.x非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。

Spring Cloud Gateway的目标提供统一的路由方式且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。

1.2、为什么要使用微服务网关?

不管是之前学习的的Zuul网关,还是今天所学阿里推出的GateWay网关,学习它们的原因是为了什么呢?

微服务网关是整个微服务api接口的入口,可以实现过滤Api接口。

作用:就是可以实现用户的验证登陆、解决跨域、日志拦截、权限控制、限流熔断、负载均衡、黑名单和白名单机制等。 微服务中的架构模式采用前后分离,前端调用接口地址都能够被抓包分析到。

1.3、Zuul与GateWay网关的区别?

之前已经学习过Zuul网关了,为什么还要学习GateWay网关呢?

Zuul网关属于NetFix公司开源框架,属于第一代微服务网关

GateWay属于SpringCloud自己研发的网关框架,属于第二代微服务网关

相比来说GateWay 比Zuul网关的性能要好很多。

主要区别: Zuul网关底层基于Servlet实现,阻塞式api,不支持长连接,依赖SpringBoot-Web. SpringCloudGateWay基于Spring5构建,能够实现响应式非阻塞式api,支持长连接,能够更好的支持Spring体系产品,依赖SpringBoot-WebFux。

2、快速入门

2.1、创建项目

创建一个项目,名字随意

创建用户项目,用来查询用户

创建eureka模块

2.2、配置yml文件

gateway网关模块yml文件

server:port: 9857spring:application:name: gateway-servicecloud:gateway:discovery:locator:##允许通过注册中心获取地址调用enabled: true#路由策略routes:#根据我们的服务名称查找地址实现调用- id: user-apiuri: lb://user-service#匹配规则predicates:- Path=/user/**
#这里使用了eureka服务注册中心,如果想用其他的随意
eureka:client:service-url:defaultZone: http://127.0.0.1:8761/eureka

用户模块的yml文件

server:port: 8081
spring:application:name: user-servicedatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/ssm_crud?useSSL=false&useUnicode=true&characterEncoding=utf-8username: rootpassword:
mybatis:type-aliases-package: cn.itssl.pojomapper-locations: classpath:mappers/*.xml
eureka:client:service-url:defaultZone: http://127.0.0.1:8761/eurekainstance:# 更倾向使用ip地址,而不是host名prefer-ip-address: true# 续约间隔,默认30秒lease-renewal-interval-in-seconds: 5# 服务失效时间,默认90秒  服务失效时间是要比续约间隔时间大的lease-expiration-duration-in-seconds: 10

eureka模块yml文件

server:port: 8761
spring:application:name: eureka-server
eureka:client:service-url:defaultZone: http://127.0.0.1:8761/eurekafetch-registry: falseregister-with-eureka: falseserver:enable-self-preservation: false  # 关闭自我保护机制 失效服务会被删除eviction-interval-timer-in-ms: 10000  # 剔除失效服务的时间间隔

2.3、controller层

在user-service模块,创建controller层,并实现相关业务

@RestController
@RequestMapping("/user")
public class UserController {@Value("${server.port}")private String port;@Autowiredprivate UserService userService;@RequestMapping("/{id}")public User queryById(@PathVariable String id) {System.out.println("端口号为:"+port+"的user-service服务被调用了");//查询用户业务,自行完成return userService.getUserById(id);}
}

2.4、启动类

gateway-service模块启动类

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

eureka模块启动类

@SpringBootApplication
@EnableEurekaServer  //开启服务端Eureka
public class EurekaServiceApplication {public static void main(String[] args) {SpringApplication.run(EurekaServiceApplication.class, args);}
}

用户模块启动类

@SpringBootApplication
@MapperScan("cn.itssl.mapper")
public class UserServiceApplication {public static void main(String[] args) {SpringApplication.run(UserServiceApplication.class, args);}
}

2.5、启动整体项目

备注:userService2是user-service的一个复制模块,功能一模一样,只是yml文件中spring.name不同而已。形成了一个user集群

连续请求接口localhost:9857/user/2

可以看到请求成功,原本只能通过localhost:8081/user/2,现在在外面又加上一层屏障,把真实的业务请求地址隐藏了,防止了不法分子对真实业务地址进行攻击。

可以看到user集群两个都被访问了,达到了网关的负载均衡的功能。

2.6、配置全局过滤器

在gateway模块中创建一个filter包,创建MyLogGateWayFilter类,实现两个接口,分别是GlobalFiler全局过滤器,以及Ordered类。

@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {log.info("进入了过滤器拦截");String uname = exchange.getRequest().getQueryParams().getFirst("uname");if (uname == null) {log.info("非法请求,进行拦截....");exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);return exchange.getResponse().setComplete();}log.info("符合请求要求,放行!");return chain.filter(exchange);}@Overridepublic int getOrder() {return 0;}
}

重新启动项目,必须在后面带有uname参数才能获取到数据,不带uname参数直接拒绝请求。

SpringCloud—— 微服务网关GateWay相关推荐

  1. SpringCloud 微服务网关Gateway 动态路由配置

    概述:在上一章节<SpringCloud 微服务网关Gateway介绍及简单路由配置>中我们讲述了Gateway的最简单的路由配置方式.但是其中比较明显的问题就是我们在配置路由服务的地址时 ...

  2. SpringCloud 微服务网关Gateway介绍及简单路由配置

    概述:什么是微服务网关?为了解决用户客户端在调用微服务系统中的多个消费者工程接口时,需要维护非常多的消费者应用接口地址等信息,以及可能存在不同应用见的调用跨域等问题,微服务网关组件随即出现.网关作为用 ...

  3. Spring Cloud之(十八)微服务网关GateWay

    十八.微服务网关GateWay Zuul 1.x 是一个基于阻塞 IO 的 API Gateway 以及 Servlet:直到 2018 年 5 月,Zuul 2.x(基于Netty,也是非阻塞的,支 ...

  4. 微服务网关Gateway

    使用场景 不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题: 客户端会多次请求不同的微服务,增加了客户端 ...

  5. 微服务网关GateWay 过滤+路由+限流

    文章目录 1 微服务网关概述 2 微服务网关微服务搭建 3 微服务网关跨域 4 微服务网关过滤器 5 微服务网关限流 5.1 思路分析 5.2 令牌桶算法 5.3 网关限流代码实现 1 微服务网关概述 ...

  6. Spring-Cloud 微服务网关Zuul、ZuulFilter过滤器和限流

    微服务网关 一. Zuul网关 1. 创建工程 并导入依赖 2. application.yml 配置文件 3. 启动类添加注解 4. 依次启动服务 5. 进入浏览器访问测试 二. ZuulFilte ...

  7. SpringCloud Alibaba 微服务 — 微服务网关 Gateway

    一.Spring Cloud Gateway 简介 Spring Cloud Gateway 是 Spring 官方基于 Spring 5.x.Spring Boot 2.x.Spring WebFl ...

  8. 微服务网关Gateway实战

    1. 什么是Spring Cloud Gateway 网关作为流量的入口,常用的功能包括路由转发,权限校验,限流等. Spring Cloud Gateway 是Spring Cloud官方推出的第二 ...

  9. 微服务网关Gateway(七)

    前言 zuul目前已经出现了分歧,zuul 升级到 Zuul2的时候出现了内部分歧,并且导致Zuul的核心人员的离职,导致Zuul2一直跳票,等了两年,目前造成的局面是Zuul已经没人维护,Zuul2 ...

最新文章

  1. 如何拼通网络ip地址_如何解决IP地址冲突
  2. php webuploader大文件,web uploader 上传大文件总结
  3. SQL索引学习-聚集索引
  4. 如何调整ABAP程序的性能
  5. ReentrantLock和synchronized的四点区别
  6. 用友 提示“尚有已全部暂估报销的单据未进行处理,不能进行12月的期末处理!”...
  7. Bash Shell学习笔记四
  8. 选择通过更改内容类型返回的详细程度,第二部分
  9. 克隆一个 AI 替自己开会,爽吗?
  10. 《CLR via C#》读书笔记 之 计算限制的异步操作
  11. 即时网络通讯系统的设计与实现(QQ)
  12. 服务器上怎么安虚拟主机呀,上线虚拟主机产品步骤
  13. 深度学习开源数据集整理
  14. 不属于计算机网络性能指标的是,数据传输速率是计算机网络的一项重要性能指标,下面不属于计算机网络数据传输常用单位的是_______。...
  15. 主板aspm关闭_【装机加人品】主板如何开启与关闭超线程技术?
  16. 独秀日记:童道自然大夫山徒步
  17. Navicat 解决问题:提示No All Pattern Found File Already Patched?
  18. Unity家园系统---建筑交互
  19. 易语言服务器端口总被占用,易语言检测端口是否被占用的代码
  20. Five-degree-of-freedom manipulation of an untethered magnetic device in fluid using a single permane

热门文章

  1. Akamai Ti 加购物车
  2. AutoML系列 | 01-自动化机器学习技术原理
  3. UVA 1339(古老的密码)
  4. Win10 更新1909 更新后网卡消失
  5. Python3 URL解析库 — urlparse
  6. 手机端图片左右滑动效果实现
  7. Matlab 几种卷积的实现与比较(conv与filter,conv2与filter2,imfilter)
  8. 下载android版趣步最新版,趣步app手机版-趣步下载 4.1.1 安卓版 - 河东软件园
  9. java:mysql数据库据转换pdf格式并打印机输出
  10. python制作圆形按钮_Python在pyqt5中创建一个真实的圆圈按钮