当前位置:网站首页>网关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)
边栏推荐
- Using settoolkit to forge sites to steal user information
- Koltin35, headline Android interview algorithm
- 手工挖XSS漏洞
- web254
- 初学者如何正确理解google官方建议架构原则(疑问?)
- Lm08 mesh series mesh inversion (fine)
- 7-26 word length (input and output in the loop)
- Uni hot update
- getInputStream() has already been called for this request
- When using charts to display data, the time field in the database is repeated. How to display the value at this time?
猜你喜欢

Airsim雷达相机融合生成彩色点云

SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?

【Redis】一气呵成,带你了解Redis安装与连接

PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux

Gui Gui programming (XV) - use scale to control font size changes

軟鍵盤高度報錯

P4 安装bmv2 详细教程

如何使用layui将数据库中的数据以表格的形式展现出来

Use threejs simple Web3D effect

OJ input and output exercise
随机推荐
web254
Instead of houses, another kind of capital in China is rising
使用beef劫持用户浏览器
getInputStream() has already been called for this request
php laravel微信支付
软键盘高度报错
Soft keyboard height error
軟鍵盤高度報錯
[introduction] approximate value
Serial port oscilloscope software ns-scope
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Programmer's regimen
String coordinates of number to excel
01 NumPy介绍
Gdip - hatchBrush图案表
力扣每日一题-第31天-202.快乐数
【入门】提取不重复的整数
Rumtime 1200 upgrade: London upgrade support, pledge function update and more
[untitled]

