sentinel(4)ahas
2021/7/19 13:04:51
本文主要是介绍sentinel(4)ahas,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
可以利用阿里云ahas用来处理sentinel
ahas开通地址;ahas开通说明
注:要将控制台换成公网
1、根据开通说明接入依赖
<!-- ahas--> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>spring-boot-starter-ahas-sentinel-client</artifactId> <version>1.9.4</version> </dependency>
注释推、拉模式依赖,避免影响
<!--<!– sentinel拉文件模式–>--> <!-- <dependency>--> <!-- <groupId>com.alibaba.csp</groupId>--> <!-- <artifactId>sentinel-datasource-extension</artifactId>--> <!-- </dependency>--> <!-- sentinel推文件模式--> <!-- <dependency>--> <!-- <groupId>com.alibaba.csp</groupId>--> <!-- <artifactId>sentinel-datasource-nacos</artifactId>--> <!-- </dependency>-->
在接入ahas后需要将alibaba的sentinel注销掉
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
2、不再需要添加埋点,spring-cloud已经支持
3、加配置
# ahas配置 ahas.namespace: default project.name: centent-center ahas.license: xxxxx
刷新访问就可以看到已经注册了应用
修改流控返回信息
package com.itmuch.usercenter.sentineltest; import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; import com.alibaba.csp.sentinel.slots.system.SystemBlockException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @Component public class MyUrlBlockHandler implements UrlBlockHandler { @Override public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException { ErrorMsg msg = null; if (e instanceof FlowException) { //限流异常 msg = ErrorMsg.builder() .code(100) .msg("限流") .build(); } else if (e instanceof DegradeException) { msg = ErrorMsg.builder() .code(101) .msg("降级") .build(); } else if (e instanceof ParamFlowException) { msg = ErrorMsg.builder() .code(102) .msg("热点参数限流") .build(); } else if (e instanceof SystemBlockException) { msg = ErrorMsg.builder() .code(103) .msg("系统规则不通过") .build(); } else if (e instanceof AuthorityException) { msg = ErrorMsg.builder() .code(104) .msg("授权不通过") .build(); } //http状态码 httpServletResponse.setStatus(500); httpServletResponse.setCharacterEncoding("utf-8"); httpServletResponse.setHeader("Content-Type","application/json;charset=utf-8"); httpServletResponse.setContentType("application/json;charset=utf-8"); //spring mvc 自带的json工具,Jackson new ObjectMapper().writeValue( httpServletResponse.getWriter(), msg ); } } @Data @Builder @AllArgsConstructor @NoArgsConstructor class ErrorMsg { private Integer code; private String msg; }
针对来源
package com.itmuch.usercenter.sentineltest; import com.alibaba.csp.sentinel.adapter.servlet.callback.RequestOriginParser; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component public class MyRequestOriginParser implements RequestOriginParser { @Override public String parseOrigin(HttpServletRequest httpServletRequest) { //从请求参数中获取名为origin的参数并返回 //如果获取不到origin参数,就会报异常 String origin = httpServletRequest.getParameter("origin"); if (StringUtils.isBlank(origin)){ throw new IllegalArgumentException("origin参数为空"); } return origin; } }
访问share/1的时候需要带上参数origin
http://localhost:8010/share/1?origin=fox
而fox就是所针对来源
在以后开发当中不要把针对来源放进访问参数中,应该放在head中,这里的针对来源与热点参数类似了
这篇关于sentinel(4)ahas的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-23线下车企门店如何实现线上线下融合?
- 2024-12-23鸿蒙Next ArkTS编程规范总结
- 2024-12-23物流团队冬至高效运转,哪款办公软件可助力风险评估?
- 2024-12-23优化库存,提升效率:医药企业如何借助看板软件实现仓库智能化
- 2024-12-23项目管理零负担!轻量化看板工具如何助力团队协作
- 2024-12-23电商活动复盘,为何是团队成长的核心环节?
- 2024-12-23鸿蒙Next ArkTS高性能编程实战
- 2024-12-23数据驱动:电商复盘从基础到进阶!
- 2024-12-23从数据到客户:跨境电商如何通过销售跟踪工具提升营销精准度?
- 2024-12-23汽车4S店运营效率提升的核心工具