为了账号安全,请及时绑定邮箱和手机立即绑定

sentinel(4)ahas

标签:
Java

可以利用阿里云ahas用来处理sentinel

ahas开通地址ahas开通说明

注:要将控制台换成公网

https://img1.sycdn.imooc.com//60f3d6730001bbf609180211.jpg

1、根据开通说明接入依赖

<!--        ahas-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>spring-boot-starter-ahas-sentinel-client</artifactId>
            <version>1.9.4</version>
        </dependency>

注释推、拉模式依赖,避免影响

<!--&lt;!&ndash;        sentinel拉文件模式&ndash;&gt;-->
<!--        <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已经支持

https://img1.sycdn.imooc.com//60f3cb0500012c8919080779.jpg

3、加配置

# ahas配置
ahas.namespace: default
project.name: centent-center
ahas.license: xxxxx

刷新访问就可以看到已经注册了应用

https://img1.sycdn.imooc.com//60f3d6b700014e8115900861.jpg


修改流控返回信息

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中,这里的针对来源与热点参数类似了







点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消