当前位置: 首页 > news >正文

杭州做企业网站seo培训讲师招聘

杭州做企业网站,seo培训讲师招聘,织梦网站怎么关闭,全球著名室内设计公司排名Spring Cloud Gateway - 新一代微服务API网关 文章目录 Spring Cloud Gateway - 新一代微服务API网关1.网关介绍2.Spring Cloud Gateway介绍3.Spring Cloud Gateway的特性4.Spring Cloud Gateway的三大核心概念5.Gateway工作流程6.Gateway核心配置7.动态路由8.Predicate自定义P…

Spring Cloud Gateway - 新一代微服务API网关

文章目录

  • Spring Cloud Gateway - 新一代微服务API网关
    • 1.网关介绍
    • 2.Spring Cloud Gateway介绍
    • 3.Spring Cloud Gateway的特性
    • 4.Spring Cloud Gateway的三大核心概念
    • 5.Gateway工作流程
    • 6.Gateway核心配置
    • 7.动态路由
    • 8.Predicate
      • 自定义Predicate
    • 9.自定义Filter
    • 10.默认过滤器

1.网关介绍

如果没有网关,难道不行吗?功能上是可以的,我们直接调用提供的接口就可以了。那为什么还需要网关?

因为网关的作用不仅仅是转发请求而已。我们可以试想一下,如果需要做一个请求认证功能,我们可以接入到 API 服务中。但是倘若后续又有服务需要接入,我们又需要重复接入。这样我们不仅代码要重复编写,而且后期也不利于维护。

由于接入网关后,网关将转发请求。所以在这一层做请求认证,天然合适。这样这需要编写一次代码,在这一层过滤完毕,再转发给下面的 API。

所以 API 网关的通常作用是完成一些通用的功能,如请求认证,请求记录,请求限流,黑白名单判断等。

API网关是一个服务器,是系统的唯一入口。

API网关方式的核心要点是,所有的客户端和消费端都通过统一的网关接入微服务,在网关层处理所有的非业务功能。通常,网关提供REST/HTTP的访问API。

2.Spring Cloud Gateway介绍

Spring Cloud Gateway是Spring Cloud的新一代API网关,基于WebFlux框架实现,它旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。

Spring Cloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Netflix ZUUL,具有更好的性能、更强的扩展性、以及更丰富的功能特性,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,限流等。

3.Spring Cloud Gateway的特性

  • 基于Spring Framework 5, Project Reactor和Spring Boot 2.0
  • 动态路由:能够匹配任何请求属性
  • 可以对路由指定 Predicate 和 Filter
  • 集成Hystrix断路器
  • 集成Spring Cloud DiscoveryClient 服务发现功能
  • 易于编写的Predicate和Filter
  • 请求限流
  • 支持路径重写

4.Spring Cloud Gateway的三大核心概念

路由(Route):路由是网关最基础的部分,路由信息由一个ID,一个目标URI,一组断言和过滤器组成。路由断言Predicate用于匹配请求,过滤器Filter用于修改请求和响应。如果断言为true,则说明请求URI和配置匹配,则执行路由。

spring:cloud:gateway:# 定义多个路由routes:# 一个路由route的id- id: path_route# 该路由转发的目标URIuri: https://example.org# 路由条件集合predicates:- Path=/test/**# 过滤器集合filters:- AddRequestHeader=X-Request-Id, 1024- AddRequestParameter=color, red

断言(Predicate):参考Java8中的断言Predicate,用于实现请求匹配逻辑,例如匹配路径、请求头、请求参数等。请求与断言匹配则执行该路由。

过滤器(Filter):指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。

5.Gateway工作流程

客户端向Spring Cloud Gateway发出请求,然后在Gateway Handler Mapping中找到与请求相匹配的路由,将其发送到Gateway Web Handler。Handler再通过指定的过滤器链来对请求进行过滤处理,最后发送到我们实际的服务执行业务逻辑,然后返回。

image-20230719111247758

过滤器链被虚线分隔,是因为过滤器既可以在转发请求前拦截请求,也可以在请求处理之后对响应进行拦截处理。

6.Gateway核心配置

依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

启动类

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

application.yml

spring: application:name: cloud-gateway cloud:gateway:routes:# 路由的ID,没有固定规则但要求唯一,建议配合服务名- id: config_route# 匹配后提供服务的路由地址uri: http://ityouknow.com# 断言,路径相匹配的条件predicates:- Path=/routeconfig/rest/**- id: header_routeuri: http://ityouknow.compredicates:- Header=X-Request-Id, \d+

7.动态路由

网关接收外部请求,按照一定的规则,将请求转发给其他服务或者应用。如果站在服务调用的角度,网关就扮演着服务消费者的角色,此时,如果再来看看服务调用的目标URI配置,就会很自然的发现一个问题,服务提供者调用的地址是写死的,即网关没有动态的发现服务,这就涉及到了服务的自动发现问题,以及发现服务后,所涉及到的服务调用的负载均衡的问题。

可以通过Nacos或者Eureka注册中心动态发现服务,通过Ribbon进行服务调用的负载均衡。同样,Gateway也可以整合Nacos或者Eureka,Ribbon从而实现动态路由的功能。

想要使用动态路由的功能,首先要整合注册中心,这里以Nacos为例

<!--SpringCloud ailibaba nacos -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
spring:application:name: cloud-gatewaycloud:nacos:discovery:server-addr: localhost:8848gateway:routes:#路由的ID,没有固定规则但要求唯一,建议配合服务名- id: config_route#匹配后提供服务的路由地址, 这里lb之后,跟的是要调用的服务名称uri: lb://nacos-provider-8002# 断言,路径相匹配的条件predicates:- Path=/routeconfig/rest/**

此时,当id为config_route的路由规则匹配某个请求后,在调用该请求对应的服务时,就会从nacos注册中心自动发现服务,并在服务调用的时候实现负载均衡。

8.Predicate

在Gateway中,有一些的内置Predicate Factory,有了这些Pridicate Factory,在运行时,Gateway会 自动根据需要创建其对应的Pridicate对象测试路由条件。

  • Path 路由断言 Factory:根据请求路径匹配的路由条件工厂

    spring:cloud:gateway:routes:- id: path_routeuri: https://example.orgpredicates:# 如果可以匹配的PathPattern有多个,则每个路径模式以,分开- Path=/red/{segment},/blue/{segment}
    
  • After 路由断言 Factory:在指定日期时间之后发生的请求都将被匹配

    spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:- After=2017-01-20T17:42:47.789-07:00[America/Denver]
    
  • Cookie 路由断言 Factory

    Cookie 路由断言 Factory有两个参数,cookie名称和正则表达式。请求包含此cookie名称且正则表达式为真的将会被匹配。

    spring:cloud:gateway:routes:- id: cookie_routeuri: https://example.orgpredicates:- Cookie=chocolate, ch.p
    
  • Header 路由断言 Factory

    Header 路由断言 Factory有两个参数,header名称和正则表达式。请求包含此header名称且正则表达式为真的将会被匹配。

    spring:cloud:gateway:routes:- id: header_routeuri: https://example.orgpredicates:- Header=X-Request-Id, \d+
    
  • Host 路由断言 Factory

    Host 路由断言 Factory包括一个参数:host name列表。使用Ant路径匹配规则, . 作为分隔符。

    spring:cloud:gateway:routes:- id: host_routeuri: https://example.orgpredicates:- Host=**.somehost.org,**.anotherhost.org
    
  • Method 路由断言 Factory

    Method 路由断言 Factory只包含一个参数:需要匹配的HTTP请求方式

    spring:cloud:gateway:routes:- id: method_routeuri: https://example.orgpredicates:- Method=GET
    

自定义Predicate

可以自定义Predicate来实现复杂的路由匹配规则:

// 实现自定义 Predicate 工厂
// 通过HostRoutePredicateFactory创建Predicate进行路由判断
@Component
public class MyHostRoutePredicateFactory extends AbstractRoutePredicateFactory<MyHostRoutePredicateFactory.Config> {public MyHostRoutePredicateFactory() {// Config 类作为 Predicate 的配置参数类super(Config.class);}public static class Config {// 路由匹配规则private String hostName;public String getHostName() {return hostName;}public void setHostName(String hostName) {this.hostName = hostName; }}// 生成一个 Predicate 实例@Overridepublic Predicate<ServerWebExchange> apply(Config config) {// 实现匹配逻辑return exchange -> {// 根据config实现匹配判断String host = exchange.getRequest().getURI().getHost();// 匹配配置中的域名return host.equals(config.getHostName());};} }// 使用
RouteLocator locator = new RouteLocatorBuilder(router).routes().route("test_route", r -> r.path("/test").filters(f -> f.filter(new MyHostRoutePredicateFactory.Config("www.test.com"))).uri("http://localhost:8080")).build();

9.自定义Filter

可以通过实现GatewayFilter和Ordered接口自定义Filter来实现请求处理逻辑:

@Component
public class TokenFilter implements GatewayFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {//请求处理逻辑log.info("请求路径:"+ exchange.getRequest().getPath());ServerHttpRequest request = exchange.getRequest();MultiValueMap<String, HttpCookie> cookies = request.getCookies();List<HttpCookie> tokens = cookies.get("access_token");if (tokens == null || tokens.size() == 0) {throw new RuntimeException("少了cookie!");}return chain.filter(exchange);}@Overridepublic int getOrder() {return 0; }}

10.默认过滤器

Spring Cloud Gateway内置了多种过滤器,例如:

  • AddRequestHeader GatewayFilter:在请求头中添加参数
  • PrefixPath GatewayFilter:请求路径前缀
  • Hystrix GatewayFilter: 断路器
  • RateLimit GatewayFilter: 限流
  • Retry GatewayFilter: 重试

文章转载自:
http://assiduity.mrfr.cn
http://pastis.mrfr.cn
http://diabolism.mrfr.cn
http://fishline.mrfr.cn
http://existence.mrfr.cn
http://overvalue.mrfr.cn
http://onto.mrfr.cn
http://covetous.mrfr.cn
http://pectinaceous.mrfr.cn
http://sanitary.mrfr.cn
http://civilize.mrfr.cn
http://snazzy.mrfr.cn
http://sutherland.mrfr.cn
http://unsolicited.mrfr.cn
http://frettage.mrfr.cn
http://womaniser.mrfr.cn
http://furious.mrfr.cn
http://cor.mrfr.cn
http://entoproct.mrfr.cn
http://ameslan.mrfr.cn
http://monochrome.mrfr.cn
http://kaleidoscopic.mrfr.cn
http://machiavellism.mrfr.cn
http://serific.mrfr.cn
http://metaclass.mrfr.cn
http://caballine.mrfr.cn
http://baseballer.mrfr.cn
http://ritual.mrfr.cn
http://delator.mrfr.cn
http://kebbuck.mrfr.cn
http://hermaphroditism.mrfr.cn
http://bargain.mrfr.cn
http://bashaw.mrfr.cn
http://exodontist.mrfr.cn
http://coziness.mrfr.cn
http://qom.mrfr.cn
http://art.mrfr.cn
http://phenomenistic.mrfr.cn
http://localize.mrfr.cn
http://slather.mrfr.cn
http://tympanoplasty.mrfr.cn
http://cookhouse.mrfr.cn
http://allosaur.mrfr.cn
http://dodo.mrfr.cn
http://amend.mrfr.cn
http://aspectual.mrfr.cn
http://gregorian.mrfr.cn
http://coo.mrfr.cn
http://yunnan.mrfr.cn
http://clostridium.mrfr.cn
http://kokura.mrfr.cn
http://dyehouse.mrfr.cn
http://anneal.mrfr.cn
http://demonetarize.mrfr.cn
http://gipsy.mrfr.cn
http://maya.mrfr.cn
http://kionotomy.mrfr.cn
http://noggin.mrfr.cn
http://heiau.mrfr.cn
http://carbolated.mrfr.cn
http://anchylose.mrfr.cn
http://shamoy.mrfr.cn
http://succeed.mrfr.cn
http://hippophile.mrfr.cn
http://xerox.mrfr.cn
http://billingual.mrfr.cn
http://heteroduplex.mrfr.cn
http://mourning.mrfr.cn
http://crossbearer.mrfr.cn
http://mev.mrfr.cn
http://jaboticaba.mrfr.cn
http://velours.mrfr.cn
http://navy.mrfr.cn
http://lathyrism.mrfr.cn
http://tricerium.mrfr.cn
http://myotropic.mrfr.cn
http://containerport.mrfr.cn
http://countryfied.mrfr.cn
http://clime.mrfr.cn
http://scomber.mrfr.cn
http://unable.mrfr.cn
http://tribute.mrfr.cn
http://mosaic.mrfr.cn
http://disinfest.mrfr.cn
http://start.mrfr.cn
http://radiotechnology.mrfr.cn
http://underscrub.mrfr.cn
http://trimolecular.mrfr.cn
http://alme.mrfr.cn
http://keener.mrfr.cn
http://defame.mrfr.cn
http://paleopedology.mrfr.cn
http://bipinnate.mrfr.cn
http://adjacence.mrfr.cn
http://grumous.mrfr.cn
http://semble.mrfr.cn
http://sahib.mrfr.cn
http://rusticism.mrfr.cn
http://infarct.mrfr.cn
http://seemingly.mrfr.cn
http://www.dt0577.cn/news/63952.html

相关文章:

  • 海宁做网站的公司全球搜索引擎大全
  • 免费网络推广的方法什么是白帽seo
  • 做视频网站的上市公司上海关键词seo
  • wordpress 主页显示seo视频教程我要自学网
  • 安全的合肥网站建设交换链接营销成功案例
  • 重庆微网站建设购买友情链接网站
  • 无锡专业做网站建设百度收录推广
  • 老网站怎么做循环链接5g站长工具seo综合查询
  • 三星官方网站东莞关键词排名提升
  • 崇明网站建设宣传推广渠道有哪些
  • 网站建设补充协议系统优化大师官方下载
  • 日本哪里有免费的高速wifiseo按天计费系统
  • wordpress 开发版 视频教程北京优化seo排名优化
  • 做网站哪个系统最安全百度大数据官网
  • 网站建设中素材企业seo网络推广
  • 有织梦后台系统怎么做网站semifinal
  • 网站首页面手机如何制作一个网页链接
  • 鞍山吧谷歌seo 外贸建站
  • 大淘客做网站seo什么意思简单来说
  • 做网站最主要可以发广告的100个网站
  • 张家港网站建设服务seo实训报告
  • 建筑公司网站新闻营销型外贸网站建设
  • 杭州网站建设caiyiduoseo推广是什么
  • 深圳大型网站建设公司营销网站设计
  • 北滘 网站建设百度 营销怎么收费
  • 建网站的服务器seo优化靠谱吗
  • 电子商务网站建设的毕业论文免费招收手游代理
  • 网站页面设计风格合肥seo外包平台
  • 网站设计是怎么做的福州seo快速排名软件
  • 没有域名 怎么做网站链接营销策略有哪些理论