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

网站开发前台无锡网站建设系统

网站开发前台,无锡网站建设系统,网站建设与维护是什么内容?,电商资源网站Spring Cloud Gateway 详解#xff1a;构建高效的API网关解决方案 Spring Cloud Gateway 是 Spring Cloud 生态系统中用于构建 API 网关的核心组件。它基于 Spring WebFlux 构建#xff0c;旨在提供简单且有效的方式来路由和增强 API 请求。以下是 Spring Cloud Gateway 的详…Spring Cloud Gateway 详解构建高效的API网关解决方案 Spring Cloud Gateway 是 Spring Cloud 生态系统中用于构建 API 网关的核心组件。它基于 Spring WebFlux 构建旨在提供简单且有效的方式来路由和增强 API 请求。以下是 Spring Cloud Gateway 的详细解释 核心概念 1. 路由Route 路由是 Spring Cloud Gateway 的基本构建块。每个路由包含一个 ID、一个目标 URI、一组断言和一组过滤器。路由的配置决定了哪些请求会被转发到哪个服务。 2. 断言Predicate 断言用于匹配进入网关的请求。Spring Cloud Gateway 提供了多种内置断言如路径断言、方法断言、头部断言等。例如Path 断言可以匹配 URL 路径。 3. 过滤器Filter 过滤器用于在请求和响应过程中对请求进行修改。过滤器有两类全局过滤器和局部过滤器。全局过滤器对所有路由生效局部过滤器只对特定路由生效。常见的过滤器包括修改请求头、修改响应头、重写路径等。 配置示例 路由配置 以下是一个基本的配置示例 spring:cloud:gateway:routes:- id: example_routeuri: http://example.orgpredicates:- Path/example/**filters:- AddRequestHeaderX-Request-Foo, Bar在这个例子中所有路径匹配 /example/** 的请求会被转发到 http://example.org并且在请求头中添加 X-Request-Foo: Bar。 断言工厂 Spring Cloud Gateway 提供了多种断言工厂 Path: 匹配请求路径。Method: 匹配 HTTP 方法。Header: 匹配请求头。Query: 匹配查询参数。 例如 predicates:- Path/foo/**- MethodGET- HeaderX-Request-Id, \d- Queryfoo, ba.*过滤器工厂 常用的过滤器工厂包括 AddRequestHeader: 添加请求头。AddRequestParameter: 添加请求参数。RewritePath: 重写路径。StripPrefix: 去除路径前缀。 例如 filters:- AddRequestParameterfoo, bar- RewritePath/foo/(?segment.*), /${segment}- StripPrefix1自定义过滤器 您还可以创建自定义过滤器。实现 GlobalFilter 接口并注入 Spring 容器即可。例如 import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.stereotype.Component; import reactor.core.publisher.Mono;Component public class CustomGlobalFilter implements GlobalFilter, Ordered {Overridepublic MonoVoid filter(ServerWebExchange exchange, GatewayFilterChain chain) {// 在此处添加您的逻辑return chain.filter(exchange);}Overridepublic int getOrder() {return -1;} }高级特性 负载均衡 Spring Cloud Gateway 可以与 Spring Cloud LoadBalancer 集成来实现负载均衡。例如 spring:cloud:gateway:routes:- id: lb_routeuri: lb://service-idpredicates:- Path/loadbalance/**熔断器 Spring Cloud Gateway 可以与 Resilience4j 集成来实现熔断器模式。例如 spring:cloud:gateway:routes:- id: circuitbreaker_routeuri: http://example.orgpredicates:- Path/circuitbreaker/**filters:- name: CircuitBreakerargs:name: myCircuitBreakerfallbackUri: forward:/fallback安全 Spring Cloud Gateway 可以与 Spring Security 集成来保护路由。例如 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity; import org.springframework.security.config.web.server.ServerHttpSecurity; import org.springframework.security.web.server.SecurityWebFilterChain;Configuration EnableWebFluxSecurity public class SecurityConfig {Beanpublic SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {http.authorizeExchange().pathMatchers(/secure/**).authenticated().anyExchange().permitAll().and().oauth2Login();return http.build();} }与 Sentinel 集成 引入依赖 在 pom.xml 文件中引入 Sentinel 的依赖 dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId /dependency配置 Sentinel 在 application.yml 中进行基本配置 spring:cloud:sentinel:transport:dashboard: localhost:8080port: 8719在网关路由中启用 Sentinel 通过配置文件 spring:cloud:gateway:routes:- id: example_routeuri: http://example.orgpredicates:- Path/example/**filters:- name: Sentinelargs:blockHandler: com.example.gateway.sentinel.CustomBlockHandler.handleException定义 BlockHandler 创建一个自定义的 BlockHandler 来处理被 Sentinel 限流或降级的请求 package com.example.gateway.sentinel;import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler; import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpResponse; import reactor.core.publisher.Mono;import javax.annotation.PostConstruct; import java.nio.charset.StandardCharsets;Configuration public class CustomBlockHandler {PostConstructpublic void init() {BlockRequestHandler blockRequestHandler (exchange, t) - {ServerHttpResponse response exchange.getResponse();response.setStatusCode(HttpStatus.TOO_MANY_REQUESTS);response.getHeaders().setContentType(MediaType.APPLICATION_JSON);String data {\code\:429,\message\:\Too Many Requests - Custom BlockHandler\};DataBuffer buffer response.bufferFactory().wrap(data.getBytes(StandardCharsets.UTF_8));return response.writeWith(Mono.just(buffer));};GatewayCallbackManager.setBlockHandler(blockRequestHandler);} }总结 Spring Cloud Gateway 是一个功能强大且灵活的 API 网关解决方案适用于微服务架构。它提供了丰富的内置功能和易于扩展的架构能够满足大多数企业应用的需求。通过断言和过滤器的组合开发者可以轻松实现复杂的路由和请求处理逻辑。同时通过与 Sentinel 等工具的集成可以进一步增强系统的稳定性和高可用性。
http://www.zqtcl.cn/news/185491/

相关文章:

  • 个人网站免费建站4399电脑版网页链接
  • 重庆开县网站建设公司推荐网站建设与维护高职
  • 关于网站开发的技术博客海口网站设计建设
  • xx市院门户网站建设方案做视频特技的网站
  • 肇庆seo公司咨询23火星seo 网站
  • 天元建设集团有限公司破产新手seo网站做什么类型好
  • spa.net网站开发二次开发需要什么
  • 如何做网站静态页面商丘网签查询
  • 网站建设好学么模版型网站是怎样的
  • 网站维护建设费应计入科目高端营销型网站制作
  • 推荐几个好的网站wordpress 加载数据库表格也卖弄
  • 承德网站开发找人做网站安全吗
  • 百度网站推广电话眼镜网站怎么做竞价
  • 邢台建设银行官方网站为什么建设网站很多公司没有
  • 闵行做网站费用湖南正规网络营销哪家便宜
  • 找个公司做网站需要注意什么wordpress用户名长度
  • 推荐几个没封的正能量网站营销技巧和营销方法视频
  • html mip 网站桂林市临桂区
  • 做网站如何月入10万建行app怎么注册登录
  • 建设一个旅游网站毕业设计建设网站的功能定位是什么原因
  • wordpress网站导航模板杭州建设网站的公司
  • 如何做视频解析网站wordpress 关闭评论
  • 安福网站建设微信开发者工具怎么下载
  • 网罗设计网站威海网页设计制作公司
  • 网站用cmswordpress插件怎么做
  • 如何办好公司网站元器件网站搭建
  • 建设领域行政处罚查询网站wordpress数据库发文章
  • 怎么做网页的多开器宿迁seo优化
  • 别人帮做的网站怎么修改病句店铺引流的30种方法
  • 网站备案幕布怎么申请绍兴cms建站模板