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

怎么把网站放到空间吗企业网站建设论文模板

怎么把网站放到空间吗,企业网站建设论文模板,网站建设服务采购方案,手机网站域名和pc域名的区别Spring Cloud Gateway 实战:网关配置与 Sentinel 限流详解 在微服务架构中,网关扮演着统一入口、负载均衡、安全认证、限流等多种角色。Spring Cloud Gateway 是 Spring Cloud 官方推出的新一代网关组件,相比于第一代 Netflix Zuul&#xff…

Spring Cloud Gateway 实战:网关配置与 Sentinel 限流详解

在微服务架构中,网关扮演着统一入口、负载均衡、安全认证、限流等多种角色。Spring Cloud Gateway 是 Spring Cloud 官方推出的新一代网关组件,相比于第一代 Netflix Zuul,性能更强、功能更丰富,且基于 Netty 和 WebFlux 开发,完全非阻塞、响应式。

本文将详细介绍 Spring Cloud Gateway 的基础配置、与 Nacos 注册中心的整合,以及如何基于 Sentinel 进行网关限流(包括路由限流和 API 分组限流)。


什么是 Spring Cloud Gateway

Spring Cloud Gateway 是 Spring Cloud 官方网关组件,属于第二代网关解决方案,替代了 Zuul。

优点

  • 基于 Netty + WebFlux,性能更高
  • 支持丰富的路由谓词和过滤器
  • 与 Spring Cloud 生态无缝集成

注意事项

  • 不兼容 Servlet(例如 SpringMVC)
  • 不支持 war 包部署,只能以 jar 形式运行

快速上手 Spring Cloud Gateway

引入依赖

一定不能引入 spring-boot-starter-web,因为它基于 Servlet,会导致冲突。

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

application.yml 配置

以下为最简单的静态路由配置(不使用注册中心):

server:port: 8010
spring:application:name: gatewaycloud:gateway:routes:- id: provider_routeuri: http://localhost:8081predicates:- Path=/provider/**filters:- StripPrefix=1- id: consumer_routeuri: http://localhost:8181predicates:- Path=/consumer/**filters:- StripPrefix=1

说明

  • id:路由标识
  • uri:转发地址
  • predicates:请求匹配条件(如路径)
  • filters:过滤器(如去前缀)

整合 Nacos 服务注册中心

通过 Nacos 让 Gateway 自动发现服务,无需手动配置 routes。

引入依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

修改配置

server:port: 8010
spring:application:name: gatewaycloud:gateway:discovery:locator:enabled: true

开启后,Gateway 会自动根据 Nacos 上注册的服务自动创建路由。


Gateway 限流(基于 Sentinel)

在实际生产环境中,网关限流非常重要,可以防止后端服务被恶意或突发大流量冲击。

引入依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>

基于路由 ID 的限流

配置 routes

server:port: 8010
spring:application:name: gatewaycloud:gateway:discovery:locator:enabled: trueroutes:- id: provider_routeuri: http://localhost:8081predicates:- Path=/provider/**filters:- StripPrefix=1

配置限流类

@Configuration
public class GatewayConfiguration {private final List<ViewResolver> viewResolvers;private final ServerCodecConfigurer serverCodecConfigurer;public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,ServerCodecConfigurer serverCodecConfigurer) {this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);this.serverCodecConfigurer = serverCodecConfigurer;}@Bean@Order(Ordered.HIGHEST_PRECEDENCE)public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);}@PostConstructpublic void initGatewayRules() {Set<GatewayFlowRule> rules = new HashSet<>();rules.add(new GatewayFlowRule("provider_route").setCount(1).setIntervalSec(1));GatewayRuleManager.loadRules(rules);}@Bean@Order(Ordered.HIGHEST_PRECEDENCE)public GlobalFilter sentinelGatewayFilter() {return new SentinelGatewayFilter();}@PostConstructpublic void initBlockHandlers() {BlockRequestHandler blockRequestHandler = (exchange, throwable) -> {Map<String, Object> map = new HashMap<>();map.put("code", 0);map.put("msg", "被限流了");return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(map));};GatewayCallbackManager.setBlockHandler(blockRequestHandler);}
}

被限流后,返回自定义 JSON 响应 {"code":0,"msg":"被限流了"}


基于 API 分组的限流

除了基于路由 ID 的限流,还可以针对 URL 前缀或具体路径进行分组限流。

修改配置

只开启服务发现,不写 routes。

server:port: 8010
spring:application:name: gatewaycloud:gateway:discovery:locator:enabled: true

配置限流类

@Configuration
public class GatewayConfiguration {private final List<ViewResolver> viewResolvers;private final ServerCodecConfigurer serverCodecConfigurer;public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,ServerCodecConfigurer serverCodecConfigurer) {this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);this.serverCodecConfigurer = serverCodecConfigurer;}@Bean@Order(Ordered.HIGHEST_PRECEDENCE)public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);}@PostConstructpublic void initGatewayRules() {Set<GatewayFlowRule> rules = new HashSet<>();rules.add(new GatewayFlowRule("provider_api1").setCount(1).setIntervalSec(1));rules.add(new GatewayFlowRule("provider_api2").setCount(1).setIntervalSec(1));GatewayRuleManager.loadRules(rules);}@Bean@Order(Ordered.HIGHEST_PRECEDENCE)public GlobalFilter sentinelGatewayFilter() {return new SentinelGatewayFilter();}@PostConstructpublic void initBlockHandlers() {BlockRequestHandler blockRequestHandler = (exchange, throwable) -> {Map<String, Object> map = new HashMap<>();map.put("code", 0);map.put("msg", "被限流了");return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(map));};GatewayCallbackManager.setBlockHandler(blockRequestHandler);}@PostConstructprivate void initCustomizedApis() {Set<ApiDefinition> definitions = new HashSet<>();ApiDefinition api1 = new ApiDefinition("provider_api1").setPredicateItems(new HashSet<ApiPredicateItem>() {{add(new ApiPathPredicateItem().setPattern("/provider/api1/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));}});ApiDefinition api2 = new ApiDefinition("provider_api2").setPredicateItems(new HashSet<ApiPredicateItem>() {{add(new ApiPathPredicateItem().setPattern("/provider/api2/demo1"));}});definitions.add(api1);definitions.add(api2);GatewayApiDefinitionManager.loadApiDefinitions(definitions);}
}

Controller 示例

@RestController
@RequestMapping("/provider")
public class DemoController {@GetMapping("/api1/demo1")public String demo1() {return "demo1";}@GetMapping("/api1/demo2")public String demo2() {return "demo2";}@GetMapping("/api2/demo1")public String demo3() {return "demo3";}@GetMapping("/api2/demo2")public String demo4() {return "demo4";}
}

限流效果

  • /provider/api1/** 前缀限流,每秒允许 1 个请求
  • /provider/api2/demo1 单接口限流,每秒允许 1 个请求

文章转载自:

http://QLsnRPOw.kkcsj.cn
http://Gyb637A5.kkcsj.cn
http://3DMerGVq.kkcsj.cn
http://tMQE3lTS.kkcsj.cn
http://NjAl7BgA.kkcsj.cn
http://9zqHtDWS.kkcsj.cn
http://YsEUHbPr.kkcsj.cn
http://uEOXIVDJ.kkcsj.cn
http://QHntorlh.kkcsj.cn
http://E2KO3GWD.kkcsj.cn
http://iP10nGxD.kkcsj.cn
http://XW9zaccJ.kkcsj.cn
http://7W4MVd07.kkcsj.cn
http://OGBOMLnG.kkcsj.cn
http://tYMhpeo3.kkcsj.cn
http://a9edLsMw.kkcsj.cn
http://ngc59lZu.kkcsj.cn
http://1fmCk9DB.kkcsj.cn
http://HtpJw4jy.kkcsj.cn
http://X0w2J1ao.kkcsj.cn
http://l7BsoQv7.kkcsj.cn
http://FZdOOmjP.kkcsj.cn
http://EjdDAmuH.kkcsj.cn
http://bjdYDbO8.kkcsj.cn
http://wZvZf6Ic.kkcsj.cn
http://WN3lhn9n.kkcsj.cn
http://DBi9yQ7f.kkcsj.cn
http://5rQVhJYJ.kkcsj.cn
http://xdVpo03G.kkcsj.cn
http://LeTg1NH3.kkcsj.cn
http://www.dtcms.com/wzjs/746582.html

相关文章:

  • 建站吗官方网站摄影课程自学网站
  • 怎么和网站建设公司签合同大型户外广告设计公司
  • 淘宝客网站哪里可以做广州白云区网站开发
  • 找人做一下网站大概多少钱深圳品牌网站推广公司哪家好
  • 网站建设企业咨询做网站时怎么透明化
  • 个人做视频网站长沙多地发布最新通告
  • python做网站的开发网站开发费用结算
  • 多企业宣传网站建设网站建设高端培训
  • 网站建设怎么配置伪静态文件晋江论坛怎么搜索帖子
  • 网站开发属于专利吗今天特大新闻
  • 男科医院网站建设策略wordpress导航菜单函数
  • 做内容网站 用什么模版WordPress虚拟资源模板
  • 建网站需要什么知识营销比较好的知名公司有哪些
  • 做的网站如何改标题建设银行官方网站-云服务
  • 怎么做属于自己的音乐网站百度广告平台电话
  • 做淘客网站能干嘛建立网站还是建设网站
  • 做网站最快的编程语言ppt模板免费下载 素材学生版
  • 瑞安网站建设公司中国建设工程造价管理协会网站
  • 福州网站seo推广优化百度提交网站收录
  • 网站设计公司 无锡iis一个文件夹配置多个网站
  • 律师事务所 网站建设网站建设必须要备案吗
  • 手机网站qq代码金方时代做网站怎么样
  • 网站需要多大空间公司门户网站制作需要多少钱
  • 浙江建设三类人员报名网站企业对比网站
  • 网站文章更新频率搜索引擎大全网址
  • 女做受网站wordpress rest api接口
  • 室内设计师常去的网站阿里云轻量级服务器搭建wordpress
  • 重庆黔江做防溺水的网站宁波做网站多少钱
  • 丹阳火车站对面规划网站开发问题论文
  • 墨星写作网站app下载企业网站建设方案费用预算