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

站长工具箱东台网站制作

站长工具箱,东台网站制作,门户网站建设分工的通知,上海做网站吧SpringCloud学习大纲 一、需求说明: 实现网关cloudalibaba-sentinel-gateway9528模块保护cloudalibaba-provider-payment9001 二、编码实现: 2.1.建module: 新建模块,名称是:cloudalibaba-sentinel-gateway9528 2.2.改pom &l…

SpringCloud学习大纲


一、需求说明:

  • 实现网关cloudalibaba-sentinel-gateway9528模块保护cloudalibaba-provider-payment9001

二、编码实现:

2.1.建module:

  • 新建模块,名称是:cloudalibaba-sentinel-gateway9528

2.2.改pom

<?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"><parent><groupId>com.atguigu.cloud</groupId><artifactId>mscloudV3</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloudalibaba-sentinel-gateway9528</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-transport-simple-http</artifactId><version>1.8.6</version></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-spring-cloud-gateway-adapter</artifactId><version>1.8.6</version></dependency><dependency><groupId>javax.annotation</groupId><artifactId>javax.annotation-api</artifactId><version>1.3.2</version><scope>compile</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>
</project>

2.3.写yml

server:port: 9528spring:application:name: cloudalibaba-sentinel-gateway     # sentinel+gataway整合Casecloud:nacos:discovery:server-addr: localhost:8848gateway:routes:- id: pay_routh1 #pay_routh1                #路由的ID(类似mysql主键ID),没有固定规则但要求唯一,建议配合服务名uri: http://localhost:9001                #匹配后提供服务的路由地址predicates:- Path=/pay/**                      # 断言,路径相匹配的进行路由

2.4.主启动

package com.atguigu.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication
@EnableDiscoveryClient
public class Main9528{public static void main(String[] args){SpringApplication.run(Main9528.class,args);}
}

2.5.业务类:

a.参考官网配置说明案例:

在这里插入图片描述
在这里插入图片描述

b.配置config:

package com.atguigu.cloud.config;import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.csp.sentinel.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import javax.annotation.PostConstruct;
import java.util.*;
/*** 使用时只需注入对应的 SentinelGatewayFilter 实例以及 SentinelGatewayBlockExceptionHandler 实例即可*/
@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() {// Register the block exception handler for Spring Cloud Gateway.return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);}@Bean@Order(-1)public GlobalFilter sentinelGatewayFilter() {return new SentinelGatewayFilter();}@PostConstruct //javax.annotation.PostConstructpublic void doInit() {initBlockHandler();}//处理/自定义返回的例外信息private void initBlockHandler() {Set<GatewayFlowRule> rules = new HashSet<>();rules.add(new GatewayFlowRule("pay_routh1").setCount(2).setIntervalSec(1));GatewayRuleManager.loadRules(rules);BlockRequestHandler handler = new BlockRequestHandler() {@Overridepublic Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {Map<String,String> map = new HashMap<>();map.put("errorCode", HttpStatus.TOO_MANY_REQUESTS.getReasonPhrase());map.put("errorMessage", "请求太过频繁,系统忙不过来,触发限流(sentinel+gataway整合Case)");return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(map));}};GatewayCallbackManager.setBlockHandler(handler);}}

2.6.测试:

  • 原生url:http://localhost:9001/pay/nacos/333
  • 加网关:http://localhost:9528/pay/nacos/333
  • sentinue + getway:加快点击频率,出现容错限流
http://www.dtcms.com/a/599520.html

相关文章:

  • 建设单位经常去哪个网站哪些网站有二维码
  • 国外室内设计网站大全网站上海开公司
  • 牛商网建设的食品网站谷歌aso优化
  • 怎样为网站设计关键词使用织梦系统建设网站教程
  • 网站布局方法分类wordpress个人博客主题
  • 能赚钱的网站怎么做制作企业网站的方法
  • 宁波建网站价格怎么用div做网站
  • mui做的h5网站案例wordpress上传ppt
  • 学做ppt的网站网站建设维护及使用管理办法
  • 东莞网站建设牛魔网a切图做网站过时了吗
  • 自建网站做外贸有什么国外的设计网站推荐
  • 免费的行情网站app网络推广服务平台
  • 郴州网站小程序我要下载中国建设网站
  • 下载搭建网站软件下载如何制作手机购物网站
  • 男女做暖暖的网站大全信息发布网站有哪些
  • 双语教学示范课程建设项目网站凡客优品
  • 郑州网站优化平台微信小程序开发公司排名
  • 沧州市网站肇庆网站推广排名
  • 企业信用信息查询公示系统天津天津seo管理平台
  • 怎么自己制作网站平台室内设计效果图报价
  • seo优化搜索引擎网站优化推广网络关键词优化-乐之家网络科技硬件开发管理流程
  • 海口网站建设推广中英文网站是怎么做的
  • 网站标题关键词怎么做公众号搭建
  • 网站 pingheiwordpress页面和分类目录
  • 四川省城乡住房和城乡建设厅网站重庆云阳网站建设
  • 做网站多少钱一年开发手机app价格
  • discuz建网站wordpress图片缓冲
  • 网站里的活动专题栏怎么做php网站系统
  • 安徽振兴集团网站开发公司主页设计图片
  • 购物网站建设信息我的世界是谁做的视频网站