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

商务网站建设公司今日热点新闻事件摘抄50字

商务网站建设公司,今日热点新闻事件摘抄50字,青岛辅德网络技术有限公司,网站与维护文章目录 前言一、搭建网关服务1、导入依赖2、在application.yml中写配置 二、路由断言工厂Route Predicate Factory三、路由过滤器 GatewayFilter案例1给所有进入userservice的请求添加一个请求头总结 四、全局过滤器 GlobalFilter定义全局过滤器,拦截并判断用户身…

文章目录

  • 前言
  • 一、搭建网关服务
    • 1、导入依赖
    • 2、在application.yml中写配置
  • 二、路由断言工厂Route Predicate Factory
  • 三、路由过滤器 GatewayFilter
    • 案例1给所有进入userservice的请求添加一个请求头
    • 总结
  • 四、全局过滤器 GlobalFilter
    • 定义全局过滤器,拦截并判断用户身份
      • 步骤1:自定义过滤器
    • 总结
  • 五、过滤器执行顺序
    • 总结
  • 六、跨域问题处理
    • 总结
  • 总结


前言

网关功能(对整个微服务起保护作用):

  • 身份认证和权限校验
  • 服务路由、负载均衡
  • 请求限流

在这里插入图片描述
在SpringCloud中网关的实现包括两种:

  • gateway
  • zuul

两种的性能对比:

Zuul是基于Servlet的实现,属于阻塞式编程。而SpringCloudGateway则是基于Spring5中提供的WebFlux,属于响应式编程的实现,具备更好的性能。


一、搭建网关服务

1、导入依赖

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId></dependency><!--nacos服务发现依赖--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>

2、在application.yml中写配置

server:port: 10010
spring:application:name: gatewaycloud:nacos:server-addr: localhost:8848 #nacos地址gateway:routes:- id: user-service #路由标示,必须唯一# uri: http://127.0.0.1:8081 # 路由的目标地址 http就是固定地址【路由地址配置的第一种模式】uri: lb://userservice #路由的目标地址【路由地址配置的第二种模式】【lb:loadBalance】predicates: #路由断言(布尔表达式),判断请求是否符合规则- Path=/user/** #路径断言,判断路径是否是以/user开头,如果是则符合- id: order-serviceuri: lb://orderservicepredicates:- Path=/order/**

配置包括:

  • 路由id:路由的唯一标示
  • 路由目标(uri):路由的目标地址,http代表固定地址,lb代表根据服务名负载均衡
  • 路由断言(predicates):判断路由的规则,判断请求是否符合要求,符合则转发到路由目的地
  • 路由过滤器(filters):对请求或响应做处理
    在这里插入图片描述

二、路由断言工厂Route Predicate Factory

  • 我们在配置文件中写的断言规则只是字符串,这些字符串会被Predicate Factory读取并处理,转变为路由判断的条件
  • 例如Path=/user/**是按照路径匹配,这个规则是由org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory类来处理的
  • 像这样的断言工厂在SpringCloudGateway还有十几个

Spring提供了11种基本的Predicate工厂:
在这里插入图片描述

官网断言工厂的地址:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories

在这里插入图片描述

三、路由过滤器 GatewayFilter

GatewayFilter是网关中提供的一种过滤器,可以对进入网关的请求和微服务返回的响应做处理:

在这里插入图片描述
Spring提供了38种不同的路由过滤器工厂。例如:
在这里插入图片描述

官网地址:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories

案例1给所有进入userservice的请求添加一个请求头

给所有进入userservice的请求添加一个请求头:Truth=itcast is freaking awesome!

实现方式:在gateway中修改application.yml文件,给userservice的路由添加过滤器:

spring: cloud:    gateway:routes: # 网关路由配置        - id: user-service          uri: lb://userservice          predicates:            - Path=/user/**          filters: # 过滤器            - AddRequestHeader=Truth, Itcast is freaking awesome! # 添加请求头

如果要对所有的路由都生效,则可以将过滤器工厂写到default下。格式如下:

server:port: 10010
logging:level:cn.itcast: debugpattern:dateformat: MM-dd HH:mm:ss:SSS
spring:application:name: gatewaycloud:nacos:server-addr: localhost:8848 # nacos地址discovery:
#        namespace: 48f42a9a-ed33-4b61-a2b1-dfa248a7e6a7 #dev环境gateway:routes: # 网关路由配置- id: user-service # 路由标示,必须唯一uri: lb://userservice # 路由的目标地址predicates: # 路由断言,判断请求是否符合规则- Path=/user/** # 路径断言,判断路径是否是以/user开头,如果是则符合- id: order-serviceuri: lb://orderservicepredicates:- Path=/order/**default-filters:- AddRequestHeader=Truth,Itcast is freaking awesome!

总结

在这里插入图片描述

四、全局过滤器 GlobalFilter

全局过滤器的作用也是处理一切进入网关的请求和微服务响应,与GatewayFilter的作用一样。
区别在于GatewayFilter通过配置定义,处理逻辑是固定的。而GlobalFilter的逻辑需要自己写代码实现。
定义方式是实现GlobalFilter接口。

public interface GlobalFilter {   /** *  处理当前请求,有必要的话通过{@link GatewayFilterChain}将请求交给下一个过滤器处理** @param exchange 请求上下文,里面可以获取Request、Response等信息    * @param chain 用来把请求委托给下一个过滤器     * @return {@code Mono<Void>} 返回标示当前过滤器业务结束    */   Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);}

定义全局过滤器,拦截并判断用户身份

需求:定义全局过滤器,拦截请求,判断请求的参数是否满足下面条件:

  • 参数中是否有authorization,
  • authorization参数值是否为admin

如果同时满足则放行,否则拦截

步骤1:自定义过滤器

自定义类,实现GlobalFilter接口,添加@Order注解:

@Order(-1)
@Component
public class AuthorizeFilter implements GlobalFilter {    @Override    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {        // 1.获取请求参数        MultiValueMap<String, String> params = exchange.getRequest().getQueryParams();        // 2.获取authorization参数       String auth = params.getFirst("authorization");        // 3.校验        if ("admin".equals(auth)) {            // 放行            return chain.filter(exchange);        }        // 4.拦截        // 4.1.禁止访问        exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);        // 4.2.结束处理        return exchange.getResponse().setComplete();    }
}

总结

在这里插入图片描述

五、过滤器执行顺序

请求进入网关会碰到三类过滤器:当前路由的过滤器、DefaultFilter、GlobalFilter
请求路由后,会将当前路由过滤器和DefaultFilter、GlobalFilter,合并到一个过滤器链(集合)中,排序后依次执行每个过滤器

在这里插入图片描述

  • 每一个过滤器都必须指定一个int类型的order值,order值越小,优先级越高,执行顺序越靠前。
  • GlobalFilter通过实现Ordered接口,或者添加@Order注解来指定order值,由我们自己指定
  • 路由过滤器和defaultFilter的order由Spring指定,默认是按照声明顺序从1递增。
  • 当过滤器的order值一样时,会按照 defaultFilter > 路由过滤器 > GlobalFilter的顺序执行。

可以参考下面几个类的源码来查看:

org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator#getFilters()方法是先加载defaultFilters,然后再加载某个route的filters,然后合并。
org.springframework.cloud.gateway.handler.FilteringWebHandler#handle()方法会加载全局过滤器,与前面的过滤器合并后根据order排序,组织过滤器链

总结

在这里插入图片描述

六、跨域问题处理

跨域:域名不一致就是跨域,主要包括:

  • 域名不同: www.taobao.com 和 www.taobao.org 和 www.jd.com 和 miaosha.jd.com
  • 域名相同,端口不同:localhost:8080和localhost8081

跨域问题:浏览器禁止请求的发起者与服务端发生跨域ajax请求,请求被浏览器拦截的问题
解决方案:CORS

网关处理跨域采用的同样是CORS方案,并且只需要简单配置即可实现:

spring:  cloud:    gateway:      # 。。。      globalcors: # 全局的跨域处理       add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题        corsConfigurations:          '[/**]':           allowedOrigins: # 允许哪些网站的跨域请求              - "http://localhost:8090"              - "http://www.leyou.com"           allowedMethods: # 允许的跨域ajax的请求方式             - "GET"             - "POST"             - "DELETE"              - "PUT"              - "OPTIONS"            allowedHeaders: "*" # 允许在请求中携带的头信息          allowCredentials: true # 是否允许携带cookie            maxAge: 360000 # 这次跨域检测的有效期

总结

在这里插入图片描述


总结

以上就是SpringCloud之Gateway(统一网关)的相关知识,希望对你有所帮助。
积跬步以至千里,积怠惰以至深渊。时代在这跟着你一起努力哦!


文章转载自:
http://gestion.bpcf.cn
http://stentorian.bpcf.cn
http://videoplayer.bpcf.cn
http://sheer.bpcf.cn
http://quinquefoliolate.bpcf.cn
http://overcome.bpcf.cn
http://outpensioner.bpcf.cn
http://ahwaz.bpcf.cn
http://brocket.bpcf.cn
http://trick.bpcf.cn
http://shaly.bpcf.cn
http://emmeniopathy.bpcf.cn
http://xylogen.bpcf.cn
http://carbolic.bpcf.cn
http://allopatric.bpcf.cn
http://amalgamable.bpcf.cn
http://muriphobia.bpcf.cn
http://whereabout.bpcf.cn
http://remittent.bpcf.cn
http://hyponoia.bpcf.cn
http://philologist.bpcf.cn
http://bibliofilm.bpcf.cn
http://inductivist.bpcf.cn
http://macrospore.bpcf.cn
http://extracorporeal.bpcf.cn
http://polystyrene.bpcf.cn
http://bobbed.bpcf.cn
http://pandowdy.bpcf.cn
http://boskage.bpcf.cn
http://cogent.bpcf.cn
http://lieve.bpcf.cn
http://cardan.bpcf.cn
http://idealise.bpcf.cn
http://infernally.bpcf.cn
http://pinup.bpcf.cn
http://audit.bpcf.cn
http://overlap.bpcf.cn
http://geniture.bpcf.cn
http://intermarriage.bpcf.cn
http://morphometrics.bpcf.cn
http://avidly.bpcf.cn
http://wildcat.bpcf.cn
http://interpunctuate.bpcf.cn
http://blaze.bpcf.cn
http://briarroot.bpcf.cn
http://trivalent.bpcf.cn
http://sensitivity.bpcf.cn
http://chayote.bpcf.cn
http://littery.bpcf.cn
http://charitable.bpcf.cn
http://dewater.bpcf.cn
http://hotliner.bpcf.cn
http://nychthemeral.bpcf.cn
http://caplet.bpcf.cn
http://hiya.bpcf.cn
http://gardenless.bpcf.cn
http://disenable.bpcf.cn
http://epilepsy.bpcf.cn
http://lxx.bpcf.cn
http://bumbling.bpcf.cn
http://fieldfare.bpcf.cn
http://rubeola.bpcf.cn
http://beechwood.bpcf.cn
http://gallomaniac.bpcf.cn
http://bleu.bpcf.cn
http://overvalue.bpcf.cn
http://micronesia.bpcf.cn
http://aura.bpcf.cn
http://overabundance.bpcf.cn
http://replaceable.bpcf.cn
http://casefy.bpcf.cn
http://helicopter.bpcf.cn
http://sibylline.bpcf.cn
http://picturedrome.bpcf.cn
http://salience.bpcf.cn
http://outgush.bpcf.cn
http://hebraist.bpcf.cn
http://premortuary.bpcf.cn
http://thematic.bpcf.cn
http://dehumanize.bpcf.cn
http://comtian.bpcf.cn
http://rhinolalia.bpcf.cn
http://sulphinpyrazone.bpcf.cn
http://strengthless.bpcf.cn
http://sherlock.bpcf.cn
http://squabbish.bpcf.cn
http://anguine.bpcf.cn
http://allergen.bpcf.cn
http://diatropic.bpcf.cn
http://proletaire.bpcf.cn
http://kaonic.bpcf.cn
http://sepulture.bpcf.cn
http://mym.bpcf.cn
http://mediumship.bpcf.cn
http://superfilm.bpcf.cn
http://madcap.bpcf.cn
http://incompressible.bpcf.cn
http://repost.bpcf.cn
http://lacker.bpcf.cn
http://rumormongering.bpcf.cn
http://www.15wanjia.com/news/63253.html

相关文章:

  • 哪块行业需要网站建设如何搜索网页关键词
  • 怎样查看网站开发语言搜索引擎营销题库和答案
  • 达州做网站怎么把自己的产品推广出去
  • 北京网站的建立的培训计划和培训内容
  • 网站引导页怎么做.链接是什么意思
  • 河北seo网站优化电话如何推广seo
  • 旅游网站建设的意义网络营销推广技巧
  • 有哪些网站可以找兼职做seo也成搜索引擎优化
  • wordpress首页文章数量成都seo学徒
  • 南昌建设委员网站网络优化大师app
  • 对红色网站建设的建议电商平台运营
  • 宝安做棋牌网站建设多少钱南宁网站建设网络公司
  • ftp免费网站空间怎么写软文
  • 做磁力链网站郑州网络推广专业公司
  • wap 企业网站网站建设产品介绍
  • 成品网站软件大全下载百度搜索一下就知道
  • 外贸网站的推广方法百度推广助手怎么用
  • 怎么做企业销售网站营销推广型网站
  • 用网站做的简历郑州高端网站建设哪家好
  • 漯河网站制作公司投放广告怎么投放
  • 怎么在手机上做企业网站网站开发需要的技术
  • 网站类型怎么分搭建网站要多少钱
  • 专门做微场景的网站东莞网站公司哪家好
  • 阿里巴巴怎样做网站百度广告优化师
  • 适合企业网站的cmsseo网站推广与优化方案
  • 广州制作网站哪家专业百度推广怎么做效果好
  • 济南网站建设哪家强竞价排名软件
  • 华强北 做网站推广赚钱
  • 云和建设局网站如何推广微信公众号
  • 网站建设方案书 备案2022年五月份热点事件