Java教程

sentinel(4)ahas

本文主要是介绍sentinel(4)ahas,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

ahas开通地址;ahas开通说明

注:要将控制台换成公网

http://img3.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已经支持

http://img2.sycdn.imooc.com/60f3cb0500012c8919080779.jpg

3、加配置

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

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

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







这篇关于sentinel(4)ahas的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!