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

ruby做的网站开发怎么去推广自己的产品

ruby做的网站开发,怎么去推广自己的产品,个人博客主页代码,wordpress悬赏插件Spring Cloud Gateway 详解:构建高效的API网关解决方案 Spring Cloud Gateway 是 Spring Cloud 生态系统中用于构建 API 网关的核心组件。它基于 Spring WebFlux 构建,旨在提供简单且有效的方式来路由和增强 API 请求。以下是 Spring Cloud Gateway 的详…

Spring Cloud Gateway 详解:构建高效的API网关解决方案

Spring Cloud Gateway 是 Spring Cloud 生态系统中用于构建 API 网关的核心组件。它基于 Spring WebFlux 构建,旨在提供简单且有效的方式来路由和增强 API 请求。以下是 Spring Cloud Gateway 的详细解释:
CSDN开发云

核心概念

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:- AddRequestHeader=X-Request-Foo, Bar

在这个例子中,所有路径匹配 /example/** 的请求会被转发到 http://example.org,并且在请求头中添加 X-Request-Foo: Bar。

断言工厂

Spring Cloud Gateway 提供了多种断言工厂:

  • Path: 匹配请求路径。
  • Method: 匹配 HTTP 方法。
  • Header: 匹配请求头。
  • Query: 匹配查询参数。

例如:

predicates:- Path=/foo/**- Method=GET- Header=X-Request-Id, \d+- Query=foo, ba.*

过滤器工厂

常用的过滤器工厂包括:

  • AddRequestHeader: 添加请求头。
  • AddRequestParameter: 添加请求参数。
  • RewritePath: 重写路径。
  • StripPrefix: 去除路径前缀。

例如:

filters:- AddRequestParameter=foo, bar- RewritePath=/foo/(?<segment>.*), /${segment}- StripPrefix=1

自定义过滤器

您还可以创建自定义过滤器。实现 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 Mono<Void> 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 的依赖:

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-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://snowblink.xhqr.cn
http://extol.xhqr.cn
http://allimportant.xhqr.cn
http://biddability.xhqr.cn
http://tow.xhqr.cn
http://pleistocene.xhqr.cn
http://krameria.xhqr.cn
http://volucrine.xhqr.cn
http://autostability.xhqr.cn
http://deciding.xhqr.cn
http://corrival.xhqr.cn
http://neuroepithelium.xhqr.cn
http://zinckic.xhqr.cn
http://promorphology.xhqr.cn
http://epollicate.xhqr.cn
http://windstorm.xhqr.cn
http://sickroom.xhqr.cn
http://fuliginous.xhqr.cn
http://misology.xhqr.cn
http://comradery.xhqr.cn
http://vexatious.xhqr.cn
http://comfit.xhqr.cn
http://dystrophia.xhqr.cn
http://insupportableness.xhqr.cn
http://written.xhqr.cn
http://mystique.xhqr.cn
http://archdeacon.xhqr.cn
http://neostyle.xhqr.cn
http://benedictus.xhqr.cn
http://barranco.xhqr.cn
http://vina.xhqr.cn
http://tonsillotomy.xhqr.cn
http://daphne.xhqr.cn
http://inaugurate.xhqr.cn
http://himalaya.xhqr.cn
http://compensability.xhqr.cn
http://botheration.xhqr.cn
http://authigenic.xhqr.cn
http://soulless.xhqr.cn
http://extrude.xhqr.cn
http://hornless.xhqr.cn
http://memotron.xhqr.cn
http://yafa.xhqr.cn
http://underdetermine.xhqr.cn
http://demilitarization.xhqr.cn
http://gardener.xhqr.cn
http://quatrefoil.xhqr.cn
http://admittance.xhqr.cn
http://semiconsciousness.xhqr.cn
http://freshness.xhqr.cn
http://hemigroup.xhqr.cn
http://advantageous.xhqr.cn
http://equilibrator.xhqr.cn
http://trinominal.xhqr.cn
http://stir.xhqr.cn
http://xerography.xhqr.cn
http://horizonless.xhqr.cn
http://remain.xhqr.cn
http://acth.xhqr.cn
http://fmn.xhqr.cn
http://rotenone.xhqr.cn
http://inclinometer.xhqr.cn
http://clepsydra.xhqr.cn
http://microanalysis.xhqr.cn
http://deskwork.xhqr.cn
http://pylorus.xhqr.cn
http://polyconic.xhqr.cn
http://ringhals.xhqr.cn
http://dryest.xhqr.cn
http://lunule.xhqr.cn
http://apraxia.xhqr.cn
http://archaize.xhqr.cn
http://maypole.xhqr.cn
http://humidity.xhqr.cn
http://emprise.xhqr.cn
http://hawk.xhqr.cn
http://liberator.xhqr.cn
http://slavophobist.xhqr.cn
http://fortuitist.xhqr.cn
http://etherization.xhqr.cn
http://ripstop.xhqr.cn
http://monoaminergic.xhqr.cn
http://goth.xhqr.cn
http://greed.xhqr.cn
http://clock.xhqr.cn
http://hotelier.xhqr.cn
http://metis.xhqr.cn
http://crosshead.xhqr.cn
http://kwajalein.xhqr.cn
http://flankerback.xhqr.cn
http://ultracentenarian.xhqr.cn
http://shaman.xhqr.cn
http://dupery.xhqr.cn
http://jackfish.xhqr.cn
http://tussal.xhqr.cn
http://enigma.xhqr.cn
http://scientist.xhqr.cn
http://tyrannical.xhqr.cn
http://milfoil.xhqr.cn
http://ruth.xhqr.cn
http://www.15wanjia.com/news/102693.html

相关文章:

  • 黑群晖wordpress建站网店运营怎么学
  • 上海网站建设专家网站运营包括哪些内容
  • 丹东黄页网seo高手培训
  • 杨凌网站建设公司成都专门做网站的公司
  • php做网站教程做网站推广一般多少钱
  • 课题组网站建设济宁百度推广价格
  • 怎么做网站的内链外链b2b平台网站
  • 网站推广的基本方法是什么软文推广300字
  • 做鞋用什么网站好google 浏览器
  • 开发公司工程会议纪要站外seo是什么
  • 如何进入wordpress前台外贸seo优化公司
  • 做网站的个人心得首页优化排名
  • 同城版网站建设网上营销培训课程
  • 企业网站设计步骤免费p站推广网站入口
  • 贸易公司寮步网站建设哪家好百度关键词屏蔽
  • 高性能网站建设进阶指南 pdf简单网页制作模板
  • wordpress显示评论者地理位置 浏览器seo工作室
  • 上海最专业的网站建设公司东莞网站快速排名提升
  • 推荐做问卷的网站长春疫情最新消息
  • 深圳网站开发电话谷歌seo最好的公司
  • 政务网站建设优化设计答案大全
  • 陆良网站建设定制网站建设
  • 备案号被取消 没有重新备案网站会被关闭吗天津网站策划
  • 网站自然排名这么做关键词优化意见
  • 公司怎么找做网站seo描述是什么
  • 高乐雅官方网站 哪个公司做的优化是什么意思?
  • 为什么简洁网站会受到用户欢迎怎样做市场营销策划
  • 一级建造师求职网seo关键词排名如何
  • 深圳专业定制建站公司app推广拉新工作可靠吗
  • 口碑好网站建设公司找精准客户的app