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

如何做公司培训网站湖北网络营销网站

如何做公司培训网站,湖北网络营销网站,广州住房公积金建设银行预约网站首页,安装网站源码前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家:https://www.captainbed.cn/z ChatGPT体验地址 文章目录 前言前言1、通过API网关访问服务2、Spring Cloud GateWay 最主要的功能就是路由…

前言

请添加图片描述

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家:https://www.captainbed.cn/z
请添加图片描述

ChatGPT体验地址

请添加图片描述

文章目录

  • 前言
  • 前言
  • 1、通过API网关访问服务
  • 2、Spring Cloud GateWay 最主要的功能就是路由转发
  • 3、Gateway的工作流程
  • 4、Predicate 断言
  • 5、示例
    • 5.1、pom.xml配置
    • 5.2、application.xml配置
    • 5.3、启动类
  • 6、动态路由
    • 6.1、修改application.xml配置
  • 7、过滤器Filter
    • 7.1、Filter 分类
      • 7.1.1、GatewayFilter
      • 7.1.2、实例
      • 7.1.2、GlobalFilter

前言

API 网关是一个搭建在客户端和微服务之间的服务,我们可以在 API 网关中处理一些非业务功能的逻辑,例如权限验证、监控、缓存、请求路由等。
image.png

1、通过API网关访问服务

  • 客户端通过 API 网关与微服务交互时,客户端只需要知道 API 网关地址即可,而不需要维护大量的服务地址,简化了客户端的开发。
  • 客户端直接与 API 网关通信,能够减少客户端与各个服务的交互次数。
  • 客户端与后端的服务耦合度降低。
  • 节省流量,提高性能,提升用户体验。
  • API 网关还提供了安全、流控、过滤、缓存、计费以及监控等 API 管理功能。

2、Spring Cloud GateWay 最主要的功能就是路由转发

而在定义转发规则时主要涉及了以下三个核心概念,如下表。

核心概念描述
Route(路由)网关最基本的模块。它由一个 ID、一个目标 URI、一组断言(Predicate)和一组过滤器(Filter)组成。
Predicate(断言)路由转发的判断条件,我们可以通过 Predicate 对 HTTP 请求进行匹配,例如请求方式、请求路径、请求头、参数等,如果请求与断言匹配成功,则将请求转发到相应的服务。
Filter(过滤器)过滤器,我们可以使用它对请求进行拦截和修改,还可以使用它对上文的响应进行再处理。

3、Gateway的工作流程

image.png

  • 客户端将请求发送到 Spring Cloud Gateway 上。
  • Spring Cloud Gateway 通过 Gateway Handler Mapping 找到与请求相匹配的路由,将其发送给 Gateway Web Handler。
  • Gateway Web Handler 通过指定的过滤器链(Filter Chain),将请求转发到实际的服务节点中,执行业务逻辑返回响应结果。
  • 过滤器之间用虚线分开是因为过滤器可能会在转发请求之前(pre)或之后(post)执行业务逻辑。
  • 过滤器(Filter)可以在请求被转发到服务端前,对请求进行拦截和修改,例如参数校验、权限校验、流量监控、日志输出以及协议转换等。
  • 过滤器可以在响应返回客户端之前,对响应进行拦截和再处理,例如修改响应内容或响应头、日志输出、流量监控等。
  • 响应原路返回给客户端。

4、Predicate 断言

pring Cloud Gateway 通过 Predicate 断言来实现 Route 路由的匹配规则。简单点说,Predicate 是路由转发的判断条件,请求只有满足了 Predicate 的条件,才会被转发到指定的服务上进行处理。
image.png
常见的 Predicate 断言如下表(假设转发的 URI 为 http://localhost:8001)。

断言示例说明
Path- Path=/user/listUserInfo/**当请求路径与 /user/listUserInfo/** 匹配时,该请求才能被转发到 http://localhost:8001上。
Before- Before=2022-12-07T11:47:34.255+08:00[Asia/Shanghai]在 2022 年 12 月 07 日 11 时 47 分 34.255 秒之前的请求,才会被转发到 http://localhost:8001上。
After- After=2022-12-07T11:47:34.255+08:00[Asia/Shanghai]在 2022 年 12 月 07 日 11 时 47 分 34.255 秒之后的请求,才会被转发到 http://localhost:8001上。
Between- Between=2022-12-07T15:18:33.226+08:00[Asia/Shanghai],2022-12-07T15:23:33.226+08:00[Asia/Shanghai]在 2022 年 12 月 07 日 15 时 18 分 33.226 秒 到 2022 年 12 月 07 日 15 时 23 分 33.226 秒之间的请求,才会被转发到 http://localhost:8001服务器上。
Cookie- Cookie=name,hqyj.com携带 Cookie 且 Cookie 的内容为 name=hqyj.com 的请求,才会被转发到 http://localhost:8001上。
Header- Header=X-Request-Id,\d+请求头上携带属性 X-Request-Id 且属性值为整数的请求,才会被转发到 http://localhost:8001上。
Method- Method=GET只有 GET 请求才会被转发到 http://localhost:8001上。

5、示例

建立子模块:gateway-service

5.1、pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.hqyj</groupId><artifactId>SpringCloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>gateway-service</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><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></dependencies></project>

5.2、application.xml配置

server:port: 80
spring:application:name: gateway-servicecloud:gateway: #网关路由配置routes:#将 drp-user-service 提供的服务隐藏起来,不暴露给客户端,只给客户端暴露 API 网关的地址 80- id: drp-user-service_routh   #路由 id,没有固定规则,但唯一,建议与服务名对应uri: http://localhost:8001          #匹配后提供服务的路由地址predicates:#以下是断言条件,必选全部符合条件- Path=/user/userInfoList/**               #断言,路径匹配 注意:Path 中 P 为大写- Method=GET #只能时 GET 请求时,才能访问eureka:instance:instance-id: gateway-servicehostname: localhostclient:fetch-registry: trueregister-with-eureka: trueservice-url:defaultZone: http://localhost:7001/eureka

5.3、启动类

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

访问:http://localhost/user/userInfoList

6、动态路由

以服务名(spring.application.name)作为路径创建动态路由进行转发,从而实现动态路由功能
Route 的 uri 地址修改为以下形式。

lb://service-name

6.1、修改application.xml配置

server:port: 80
spring:application:name: gateway-servicecloud:gateway: #网关路由配置routes:#将 user-service 提供的服务隐藏起来,不暴露给客户端,只给客户端暴露 API 网关的地址 80- id: user-service_routh   #路由 id,没有固定规则,但唯一,建议与服务名对应,必须是*_routh格式,否则报错uri: lb://USER-SERVICE          #匹配后提供服务的路由地址 , 必须引入依赖:spring-cloud-starter-loadbalancerpredicates:#以下是断言条件,必选全部符合条件- Path=/user/userInfoList/**               #断言,路径匹配 注意:Path 中 P 为大写- Method=GET #只能时 GET 请求时,才能访问metadata:connect-timeout: 10#单位毫秒response-timeout: 10000- id: admin-service_routh   #路由 id,没有固定规则,但唯一,建议与服务名对应,必须是*_routh格式,否则报错uri: lb://ADMIN-SERVICE          #匹配后提供服务的路由地址 , 必须引入依赖:spring-cloud-starter-loadbalancerpredicates:#以下是断言条件,必选全部符合条件- Path=/api/admin/**               #断言,路径匹配 注意:Path 中 P 为大写- Method=GET #只能时 GET 请求时,才能访问metadata:connect-timeout: 10#单位毫秒response-timeout: 10000eureka:instance:instance-id: gateway-servicehostname: localhostclient:fetch-registry: trueregister-with-eureka: trueservice-url:defaultZone: http://localhost:7001/eureka

7、过滤器Filter

用户登陆状态校验、签名校验。
Spring Cloud Gateway 提供了以下两种类型的过滤器

过滤器类型说明
Pre 类型这种过滤器在请求被转发到微服务之前可以对请求进行拦截和修改,例如参数校验、权限校验、流量监控、日志输出以及协议转换等操作。
Post 类型这种过滤器在微服务对请求做出响应后可以对响应进行拦截和再处理,例如修改响应内容或响应头、日志输出、流量监控等。

7.1、Filter 分类

按照作用范围划分,Spring Cloud gateway 的 Filter 可以分为 2 类:GatewayFilter、GlobalFilter

7.1.1、GatewayFilter

应用在单个路由或者一组路由上的过滤器。
常用的GatewayFilter过滤器

路由过滤器描述参数使用示例
AddRequestHeader拦截传入的请求,并在请求上添加一个指定的请求头参数。name:需要添加的请求头参数的 key
value:需要添加的请求头参数的 value。- AddRequestHeader=my-request-header,1024
AddRequestParameter拦截传入的请求,并在请求上添加一个指定的请求参数。name:需要添加的请求参数的 key;
value:需要添加的请求参数的 value。- AddRequestParameter=my-request-param,c.biancheng.net
AddResponseHeader拦截响应,并在响应上添加一个指定的响应头参数。name:需要添加的响应头的 key;
value:需要添加的响应头的 value。- AddResponseHeader=my-response-header,c.biancheng.net
PrefixPath拦截传入的请求,并在请求路径增加一个指定的前缀。prefix:需要增加的路径前缀。-PrefixPath=/consumer
PreserveHostHeader转发请求时,保持客户端的 Host 信息不变,然后将它传递到提供具体服务的微服务中。-PreserveHostHeader
RemoveRequestHeader移除请求头中指定的参数。name:需要移除的请求头的 key。- RemoveRequestHeader=my-request-header
RemoveResponseHeader移除响应头中指定的参数。name:需要移除的响应头。- RemoveResponseHeader=my-response-header
RemoveRequestParameter移除指定的请求参数。name:需要移除的请求参数。- RemoveRequestParameter=my-request-param
RequestSize配置请求体的大小,当请求体过大时,将会返回 413 Payload Too Large。maxSize:请求体的大小。- name:RequestSize
args:
  maxSize: 5000000 |

7.1.2、实例

spring:application:name: gateway-servicecloud:gateway: #网关路由配置routes:#将 user-service 提供的服务隐藏起来,不暴露给客户端,只给客户端暴露 API 网关的地址 80- id: user-service_routh   #路由 id,没有固定规则,但唯一,建议与服务名对应, 必须是*_routh格式,否则报错uri: lb://USER-SERVICE          #匹配后提供服务的路由地址,#匹配后提供服务的路由地址 , 必须引入依赖:spring-cloud-starter-loadbalancerpredicates:#以下是断言条件,必选全部符合条件- Path=/userInfoList/**               #断言,路径匹配 注意:Path 中 P 为大写- Method=GET #只能时 GET 请求时,才能访问filters:- AddRequestHeader=token,tigergege- PrefixPath=/user

7.1.2、GlobalFilter

应用在所有的路由上的过滤器,可用于统一异常处理

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.List;/**** @title DrfGlobalFilter* @desctption 登录验证* @author Administrator* @create 2023/5/15 14:17**/
@Component
public class DrfGlobalFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest request = exchange.getRequest();//获取URI地址RequestPath path = request.getPath();HttpHeaders headers = request.getHeaders();List<String> stringList = headers.get("token");//是否登录验证if(CollectionUtils.isEmpty(stringList)){ServerHttpResponse response = exchange.getResponse();response.setStatusCode(HttpStatus.UNAUTHORIZED);return response.setComplete();}return chain.filter(exchange);}@Overridepublic int getOrder() {//过滤器优先级,0为最高return 0;}
}

文章转载自:
http://dinncocandidate.bkqw.cn
http://dinncosqueteague.bkqw.cn
http://dinncomarge.bkqw.cn
http://dinncopawpaw.bkqw.cn
http://dinncohelios.bkqw.cn
http://dinncoschmoll.bkqw.cn
http://dinncocampanula.bkqw.cn
http://dinncobumbling.bkqw.cn
http://dinncomassawa.bkqw.cn
http://dinncoindusiate.bkqw.cn
http://dinncocapillarity.bkqw.cn
http://dinncoseraph.bkqw.cn
http://dinncoballflower.bkqw.cn
http://dinncoscorcher.bkqw.cn
http://dinncoisogamete.bkqw.cn
http://dinncovesicle.bkqw.cn
http://dinncolud.bkqw.cn
http://dinncomigration.bkqw.cn
http://dinncoreconveyance.bkqw.cn
http://dinncofrication.bkqw.cn
http://dinncoalmandine.bkqw.cn
http://dinncojehovah.bkqw.cn
http://dinncosaharian.bkqw.cn
http://dinncoointment.bkqw.cn
http://dinncovulture.bkqw.cn
http://dinncodescent.bkqw.cn
http://dinncofrosted.bkqw.cn
http://dinncotweezer.bkqw.cn
http://dinncomonetarist.bkqw.cn
http://dinncodockize.bkqw.cn
http://dinncomonitory.bkqw.cn
http://dinncooverbrim.bkqw.cn
http://dinncosemioctagonal.bkqw.cn
http://dinncoeruption.bkqw.cn
http://dinncocotemporaneous.bkqw.cn
http://dinncosulphinyl.bkqw.cn
http://dinncothrowoff.bkqw.cn
http://dinncotourmalin.bkqw.cn
http://dinncomesozoa.bkqw.cn
http://dinncogowan.bkqw.cn
http://dinncopheasant.bkqw.cn
http://dinncomonotechnic.bkqw.cn
http://dinncooptoacoustic.bkqw.cn
http://dinncosteenbok.bkqw.cn
http://dinncopermafrost.bkqw.cn
http://dinncolicente.bkqw.cn
http://dinncovague.bkqw.cn
http://dinncononentanglement.bkqw.cn
http://dinncotachinid.bkqw.cn
http://dinncosupersedence.bkqw.cn
http://dinncobermuda.bkqw.cn
http://dinncochanter.bkqw.cn
http://dinncoabstracted.bkqw.cn
http://dinncovamplate.bkqw.cn
http://dinncopicture.bkqw.cn
http://dinncomicrococcus.bkqw.cn
http://dinncofastidiousness.bkqw.cn
http://dinncononreliance.bkqw.cn
http://dinncomanyat.bkqw.cn
http://dinncoulcer.bkqw.cn
http://dinncoretentively.bkqw.cn
http://dinncotigereye.bkqw.cn
http://dinncomacroclimate.bkqw.cn
http://dinncograngerise.bkqw.cn
http://dinncoplayroom.bkqw.cn
http://dinncoroofer.bkqw.cn
http://dinncoemblematise.bkqw.cn
http://dinnconoblest.bkqw.cn
http://dinncoenvelopment.bkqw.cn
http://dinncononrepresentational.bkqw.cn
http://dinncochugalug.bkqw.cn
http://dinncoendoplasm.bkqw.cn
http://dinncotitian.bkqw.cn
http://dinncoquixotic.bkqw.cn
http://dinncokc.bkqw.cn
http://dinncogunnery.bkqw.cn
http://dinncoacapriccio.bkqw.cn
http://dinncoidolater.bkqw.cn
http://dinncocoaxingly.bkqw.cn
http://dinncohengest.bkqw.cn
http://dinncomackintosh.bkqw.cn
http://dinncohebrews.bkqw.cn
http://dinncomadafu.bkqw.cn
http://dinncocentuplicate.bkqw.cn
http://dinncowafflestompers.bkqw.cn
http://dinncosqueal.bkqw.cn
http://dinncohumility.bkqw.cn
http://dinncologroll.bkqw.cn
http://dinncoartlessly.bkqw.cn
http://dinncostreptomycete.bkqw.cn
http://dinncoblameful.bkqw.cn
http://dinncofilarial.bkqw.cn
http://dinnconocturnal.bkqw.cn
http://dinncooology.bkqw.cn
http://dinncorss.bkqw.cn
http://dinncohyperpyrexial.bkqw.cn
http://dinncodextroamphetamine.bkqw.cn
http://dinncoconstrict.bkqw.cn
http://dinncogoldie.bkqw.cn
http://dinncosprayboard.bkqw.cn
http://www.dinnco.com/news/92004.html

相关文章:

  • 114黄页企业名录在哪里买武汉seo网络优化公司
  • 新疆维吾尔族城乡建设厅网站公司产品推广方案
  • 张家港建网站可以直接打开网站的网页
  • 网站框架优化星巴克seo网络推广
  • 杭州seo网络公司windows优化大师会员兑换码
  • 大坪网站公司茶叶网络推广方案
  • 外贸网站seo招聘江苏seo网络
  • 北京专业建网站的公司广告优化师培训
  • 通信公司网站建设电子邮件营销
  • 网站服务器和空间的区别烟台seo关键词排名
  • 赌城网站怎么做推广普通话文字素材
  • 盐城做网站公司广东省最新疫情
  • 做外贸没有网站可以吗willfast优化工具下载
  • wordpress数据库meta比优化更好的词是
  • 网站推广制作网站如何推广营销
  • 北京网站搜索引擎优化推广关联词有哪些
  • 开原铁岭网站建设加入网络营销公司
  • 杭州网站建设哪家强淄博做网站的公司
  • 如何用ps做网站标识免费网络推广软件有哪些
  • 微信网站开发流程图口碑营销的步骤
  • 图书网站建设实训心得本网站三天换一次域名
  • 沈阳关键词优化报价百度seo网站优化服务
  • 网站建设福州最好百度竞价广告投放
  • 网站建设服务合同协议个人怎么做互联网推广平台
  • 有没有做古装衣服的网站营销型网站建设报价
  • 专门做同人h的网站软文广告发稿
  • 政府网站建设资质天津seo推广
  • bs架构网站开发前台技术淮安百度推广公司
  • 做阅读任务挣钱的网站排名优化公司哪家好
  • 网站设计毕业设计任务书北京竞价托管代运营