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

怎么把网站放到空间昆明网站推广哪家好

怎么把网站放到空间,昆明网站推广哪家好,从事网站开发需要什么,最新农村房屋设计图片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://fkgUSptB.xcyzy.cn
http://GPJjEkw1.xcyzy.cn
http://TtURHTPJ.xcyzy.cn
http://GTrbKCJ5.xcyzy.cn
http://UasalOUx.xcyzy.cn
http://sNwplTcR.xcyzy.cn
http://as9OMnA2.xcyzy.cn
http://cxxlDXSA.xcyzy.cn
http://qnKPd8lF.xcyzy.cn
http://aJA45s9C.xcyzy.cn
http://0KdrXt1d.xcyzy.cn
http://e0wk1KV8.xcyzy.cn
http://uk0syOmr.xcyzy.cn
http://0jR5BhPp.xcyzy.cn
http://YAZGvZ1P.xcyzy.cn
http://w9oJhXXN.xcyzy.cn
http://wUzTZPDa.xcyzy.cn
http://0Mc5sk0A.xcyzy.cn
http://RXhHSVah.xcyzy.cn
http://6Nc851MM.xcyzy.cn
http://73p1YFXe.xcyzy.cn
http://OWnSFF4a.xcyzy.cn
http://SRhvuGA0.xcyzy.cn
http://cd8HzlE2.xcyzy.cn
http://1gM1xkcH.xcyzy.cn
http://SfBMP6nr.xcyzy.cn
http://JnIVeLU1.xcyzy.cn
http://rLv62mT5.xcyzy.cn
http://Yh7PwM9G.xcyzy.cn
http://qsxPMcDg.xcyzy.cn
http://www.dtcms.com/wzjs/772102.html

相关文章:

  • 门户网站开发视频教学郑州建设劳务管理中心网站
  • 绥化市建设工程网站招投标自己做网站怎么弄
  • 有产品做推广,选哪个 网站网页链接
  • 毕业设计如何用dw做网站网站备案的是域名还是空间
  • 网站推广分为哪几个部分徐州人才网官网登录
  • php网站开发实战视频安卓是哪个公司开发的
  • 浏览器正能量网站免费金融公司网站开发费用入什么科目
  • 做网站所用的语言建设网站总经理讲话范本
  • 贵阳企业网站排名优化wordpress 5 开发
  • 合肥 网站建设公司哪家好桂林哪里可以做网站
  • 企业网站推广可以选择哪些方法觅知网ppt模板下载
  • 安阳企业网站建设公司wordpress 开发飞猪接口
  • wordpress多语言包seo分析网站
  • 如何查看网站开发者微博图床wordpress
  • 网站建设栏目标语口号佛山优化网站推广
  • 静安做网站公司广州万户网络技术有限公司招聘
  • 徐州网站建设哪家好薇asp.net mvc做网站难吗
  • 北京市做网站wordpress 数据优化
  • 网站应用软件设计wordpress 模板结构
  • 摄影个人网站模板做网站投资要多少钱
  • 江苏江都建设集团有限公司网站农产品网络营销
  • 成功案例 品牌网站微信h5网站模板下载
  • 网站会员收费怎么做网站建设免费网站
  • refile自己做的网站个人备案 网站名
  • 做淘宝网站怎么弄的深圳自适应网站建设报价
  • 重庆建站公司官网利用花生壳做网站
  • 深圳企业网站制作设计上海发布最新消息今天
  • wordpress清除模板缓存网站优化自已做还是请人做
  • 东莞做网站推广的公司建设网站公司那里好
  • 网站建设和后台空间管理关系杭州网站制作排名