当前位置:网站首页>网关gateway-88
网关gateway-88
2022-07-01 08:08:00 【蝶衣_疯魔】
动态上下线:发送请求需要知道商品服务的地址,如果商品服务器有1,2,3号服务器,1号
掉线后,还得改,所以需要网关动态地管理,他能从注册中心中实时地感知某个服务上线还是下线。网关总是能帮我们将请求正确的路由到指定位置【先通过网关,网关路由到服务提供者】
第二种需求:
鉴权、监控;如果每个服务都做,就会有很多重复开发

客户端先请求网关,再由网关转给其它服务


性能比较
拦截:请求也要加上询问权限,看用户有没有权限访问这个请求,也需要网关。
所以我们使用spring cloud的gateway组件做网关功能。
网关是请求流量的入口,常用功能包括路由转发,权限校验,限流控制等。springcloud gateway取代了zuul网关。
https://spring.io/projects/spring-cloud-gateway
参考手册:https://cloud.spring.io/spring-cloud-gateway/2.2.x/reference/html/
三大核心概念:
Route: The basic building block of the gateway. It is defined by an ID, a destination URI, a collection of predicates断言, and a collection of filters. A route is matched if the aggregate predicate is true.发一个请求给网关,网关要将请求路由到指定的服务。路由有id,目的地uri,断言的集合,匹配了断言就能到达指定位置,(路由有一个标识性的id,路由之间由id来区分。断言的集合:,过滤器的集合:,路由只要匹配了断言,断言为真,路由就匹配了,能到达指定位置,请求发给API网关,到底要不要路由到一个地方,我们得有一个条件判断,这个条件判断就是断言)
Predicate断言 : This is a Java 8 Function Predicate. The input type is a Spring Framework ServerWebExchange. This lets you match on anything from the HTTP request, such as headers or parameters.就是java8里的断言函数,匹配当次请求里的任何信息,包括请求头等。根据请求头路由哪个服务
Filter: These are instances of Spring Framework GatewayFilter that have been constructed with a specific factory. Here, you can modify requests and responses before or after sending the downstream request.过滤器请求和响应都可以被修改。
客户端发请求给服务端。中间有网关。先交给映射器,如果能处理就交给handler处理,然后交给一系列filer,然后给指定的服务,再返回回来给客户端。

总结一句话:请求到达
网关,网关断言,请求是否符合某一个规则,如果符合,就按这个路由规则,路由到指定地方,去指定地方的途中需要一系列的过滤。所有难点:我们如何定义路由规则,断言怎么判断成功失败,怎么配置,要使用哪些filter自己心里得要清楚
断言的作用:满足断言条件3,就指定到路由2
有很多断言。
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
-代表数组,可以设置Cookie等内容。只有断言成功了,才路由到指定的地址。
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
创建微服务,使用initilizer,Group:com.atguigu.gulimall,Artifact: gulimall-gateway,package:com.atguigu.gulimall.gateway。 搜索
gateway选中。
pom.xml里加上common依赖, 修改jdk版本,
在
gateway服务中开启注册服务发现@EnableDiscoveryClient,配置nacos注册中心地址applicaion.properties。这样gateway也注册到了nacos中,其他服务就能找到nacos,网关也能通过nacos找到其他服务
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.application.name=gulimall-gateway
server.port=88
bootstrap.properties 填写nacos配置中心地址
spring.application.name=gulimall-gateway
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=bfa85f10-1a9a-460c-a7dc-efa961b45cc1
本项目在nacos中的服务名
spring:
application:
name: gulimall-gateway
再去nacos里创建命名空间gateway(项目与项目用命名空间隔离),然后在命名空间里创建文件guilmall-gateway.yml
在项目里创建application.yml,根据条件转发到uri等
spring:
cloud:
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq
- id: product_route
uri: lb://gulimall-product
predicates:
- Path=/api/product/**
filters:
- RewritePath=/api/(?<segment>.*),/$\{
segment}
- id: third_party_route
uri: lb://gulimall-third-party
predicates:
- Path=/api/thirdparty/**
filters:
- RewritePath=/api/thirdparty/(?<segment>.*),/$\{
segment}
- id: member_route
uri: lb://gulimall-member
predicates:
- Path=/api/member/**
filters:
- RewritePath=/api/(?<segment>.*),/$\{
segment}
- id: ware_route
uri: lb://gulimall-ware
predicates:
- Path=/api/ware/**
filters:
- RewritePath=/api/(?<segment>.*),/$\{
segment}
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters: # 这段过滤器和验证码有关,api内容缓存了/renren-fast,还得注意/renren-fast也注册到nacos中
- RewritePath=/api/(?<segment>.*),/renren-fast/$\{
segment}
## 前端项目,/api前缀。先来到网关后断言先匹配到,过滤器修改url,比如跳转到renren微服务,所以要注意renren后端项目也注册到 nacos里
## http://localhost:88/api/captcha.jpg http://localhost:8080/renren-fast/captcha.jpg
## http://localhost:88/api/product/category/list/tree http://localhost:10000/product/category/list/tree
测试 localhost:8080/hello?url=baidu
网关使用的是Netty,Netty比Tomcat性能高
2021-12-31 16:20:37.276 INFO 19364 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 88
predicates:
- Query=url(key),baidu(value)
边栏推荐
- Gui Gui programming (XV) - use scale to control font size changes
- Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier
- The difference between interceptors and filters
- Sqlalchemy creating MySQL_ Table
- 图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
- Aardio - [problem] the problem of memory growth during the callback of bass Library
- [untitled]
- How can beginners correctly understand Google's official suggested architectural principles (questions?)
- How to get a SharePoint online site created using the office365 group template
- 【入门】取近似值
猜你喜欢

Access报表实现小计功能

軟鍵盤高度報錯

SharePoint - modify web application authentication using PowerShell

Lm08 mesh series mesh inversion (fine)

5大组合拳,解决校园6大难题,护航教育信息化建设

Learn reptiles for a month and earn 6000 a month? Tell you the truth about the reptile, netizen: I wish I had known it earlier

使用beef劫持用户浏览器

Access report realizes subtotal function
![[untitled]](/img/b9/6922875009c2d29224a26ed2a22b01.jpg)
[untitled]

Connect timed out of database connection
随机推荐
【入门】提取不重复的整数
String coordinates of number to excel
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Using settoolkit to forge sites to steal user information
Two expressions of string
Cyclic neural network
Lm08 mesh series mesh inversion (fine)
如何使用layui将数据库中的数据以表格的形式展现出来
Differential: definition of total differential, partial derivative, gradient
SharePoint - modify web application authentication using PowerShell
量化交易之读书篇 - 《征服市场的人》读书笔记
Aardio - 自己构造的getIconHandle的方法
Airsim雷达相机融合生成彩色点云
[getting started] enter the integer array and sorting ID, and sort its elements in ascending or descending order
Use threejs simple Web3D effect
Aardio - [problem] the problem of memory growth during the callback of bass Library
getInputStream() has already been called for this request
5大组合拳,解决校园6大难题,护航教育信息化建设
Embedded-c language-10-enumeration / (function) pointer (function) / multi-level pointer /malloc dynamic allocation / file operation
slice扩容机制分析

